Theme

Interactive diagram · architecture

Design the data structures for a social network — interactive diagram

Design social graph data structures A client resolves DNS, then sends a friend search through a load balancer, web server, and Query API to User Graph Service. The service uses Lookup Service and sharded Person Servers for breadth-first search, and can reuse cached traversals. Design Social Graph Diagram after donnemartin/system-design-primer · CC BY 4.0 Client DNS Load Balancer Web Server Query API 1 · Route the search User GraphService LookupService PersonServer 2 · Locate the person 3 · Walk the friend graph Memory Cache 4 · Reuse cached traversals
✕

1 · Route the search

A friend search moves from the client through the public web path to Query API.

Flow

Client → DNS and Load Balancer → Web Server → Query API.

✕

2 · Locate the person

The graph service asks Lookup Service which Person Server holds the source person.

Flow

Query API → User Graph Service → Lookup Service → Person Server.

✕

3 · Walk the friend graph

User Graph Service runs breadth-first search and retrieves each adjacent person by ID.

Flow

User Graph Service → Lookup Service → Person Server, repeated for friend IDs.

✕

4 · Reuse cached traversals

Complete or partial BFS traversals can be stored to speed up later lookups.

Flow

User Graph Service → Memory Cache → later graph lookups.

✕

Person Server

Step 2 · Locate the person

What it is

A Person Server stores people by person ID.

Its job here

Lookup Service identifies this server. It returns the person and their friend_ids to User Graph Service.

Trade-off

User data must be sharded because all users and relationships do not fit on one machine.

In the primer

Database › Sharding →

✕

Memory Cache

Step 4 · Reuse cached traversals

What it is

A memory cache holds person data and saved BFS traversals.

Its job here

User Graph Service checks or stores complete or partial traversals here. Later searches can reuse them before more Person Server lookups.

Examples

  • Redis
  • Memcached

Trade-off

Cached traversals need an update strategy.

In the primer

Cache › When to update the cache →

←/→ step · Space play/pause · R replay · Home/End jump

Detail · follows playback, or click any box or step to pin it

Click any box for an explanation, or use Next or Play to walk through the four flows.

1 · Route the search

The client resolves DNS and sends the search through the load balancer and reverse proxy to Query API.

Query API receives the public friend-search request.

2 · Locate the person

User Graph Service asks Lookup Service which Person Server contains the source person.

The Person Server returns the person and their friend_ids.

3 · Walk the friend graph

User Graph Service runs BFS from the source person through friend IDs.

Each adjacent person can require another Lookup Service and Person Server retrieval.

4 · Reuse cached traversals

Complete or partial BFS traversals can be stored to speed up later searches.

Memory Cache reduces traffic to downstream services for repeated searches.