Design a LinkedIn-style connection graph
hard 55 minStore who is connected to whom for a billion people, and say in a blink whether a stranger is a 1st, 2nd or 3rd degree connection.
Video lesson · Chapter 1 of 5
Connect two people without exposing private edges
Theory
Read or listen first. About 1 min.
What it is
A connection graph records people as points and mutual connections as lines between them. On a professional network like LinkedIn, a connection goes both ways: if you are connected to a colleague, the colleague is connected to you.
The degree of another member is the number of lines on the shortest path. One line is 1st degree, two is 2nd degree (a friend of a friend), three is 3rd degree. Beyond that we simply say out of network.
Why it exists
Distance is a trust signal. A message from someone who shares fifteen contacts with you reads differently from a message out of nowhere, so profiles, search results and introductions all show it.
It is a good interview problem because the obvious method explodes. With 500 connections each, two hops can reach 250,000 people and three hops 125 million. You must answer without walking there.
A short history
1967
Stanley Milgram publishes The Small World Problem: letters passed between acquaintances reach a stranger in a few steps.
1971
Ira Pohl publishes Bi-directional Search: start from both ends and meet.
2011
A study of the whole Facebook graph (arXiv 1111.4503) confirms the six degrees idea at global scale.
2013
LinkedIn engineers publish a HotCloud paper on a partitioned graph that answers distance queries while a page loads.
2020
LinkedIn describes LIquid, its own in-memory, log-structured graph database.
Words to know
- Edge
- One connection between two members.
- Adjacency list
- All the connections of one member, stored together.
- Degree of separation
- The number of connections on the shortest path between two members.
- Mutual connections
- Members who are connected to both people. The overlap of two lists.
- Frontier
- The people reached so far at the edge of a search.
- Supernode
- A member with very many connections, far above the average.
- PYMK
- People You May Know: suggestions of new connections.
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
How it works
Step through the idea, or press Play.
Connect. A accepts B's invitation; one transaction records it
Understand it
The same picture explains degree, mutual connections, supernodes and stale caches.
Scope
What are we building?
The part of a professional network that knows who is connected to whom. It sends and accepts invitations, shows your degree to anyone you look at, lists mutual connections, and suggests people you may know. The feed, messages and jobs use this graph but are separate systems.
Connect
Invite, accept, decline, withdraw, remove, block.
Measure
1st, 2nd, 3rd or out of network, with mutual connections.
Suggest
People You May Know, ranked and filtered for privacy.
Similar problems
| Product type | What differs |
|---|---|
| Friends on Facebook | Same shape: mutual edges and friend suggestions |
| Followers on X or Instagram | One-way edges, and some members have millions of followers |
| Developers on GitHub | One-way follows, plus shared projects |
| Members of a Slack workspace or Discord server | Groups decide who can see whom, not hops |
| Payments on Venmo | Edges appear from activity |
| Neighbours on Nextdoor | Place decides, not hops |
The finished system
Where the design ends up
This is the last version, not the first. The Design tab starts with one service and one database, and names the number that forces each extra box.
The whole system
Which page owns each idea
| Idea | Read |
|---|---|
| Splitting lists over machines | Sharding |
| Feeding the graph from the database | Change data capture |
| A read model built beside the write model | CQRS |
| The log that carries changes | Log-based queues |
| Expiry and stale answers | Cache consistency |
| Asking many machines at once | Fan-out and fan-in |
| Ranking suggestions | Recommendation system |
Summary
The whole topic in one minute.
A connection graph is simple to store and hard to walk. Walking out from one person reaches 250,000 people in two hops, so we compare two friend lists instead and only then look one hop further.
Our design starts as one service and one relational database. Memory-held friend lists, a change log, a cached two-hop set for each active viewer and an offline suggestion pipeline are added one at a time, each because a number asked for it.
- Store each connection twice, once in each person's list, so one read gives all friends.
- Direct lookup finds 1st degree, list overlap finds 2nd, one extra hop from the shorter side finds 3rd.
- With 4-byte member numbers the whole graph is 2 TB, which fits in memory on a modest cluster.
- The relational database holds the truth; the graph is a copy that can be rebuilt from the log.
- Blocks and privacy are checked at read time against fresh data, never against a stale cache.
- Suggestions are computed ahead of time and filtered again when shown.
Sources
- Wang, Conrad, Shah (LinkedIn): Using Set Cover to Optimize a Large-Scale Low Latency Distributed Graph, HotCloud 2013
- LinkedIn Engineering: LIquid, the soul of a new graph database
- LinkedIn Help: your network and degrees of connection
- TAO: Facebook's Distributed Data Store for the Social Graph, USENIX ATC 2013
- Ugander et al.: The Anatomy of the Facebook Social Graph
- Neo4j Cypher manual: shortest paths
- Redis: HyperLogLog