AlgoViz

Design a LinkedIn-style connection graph

hard 55 min

Store 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

0:00 / 3:55

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

  1. 1967

    Stanley Milgram publishes The Small World Problem: letters passed between acquaintances reach a stranger in a few steps.

  2. 1971

    Ira Pohl publishes Bi-directional Search: start from both ends and meet.

  3. 2011

    A study of the whole Facebook graph (arXiv 1111.4503) confirms the six degrees idea at global scale.

  4. 2013

    LinkedIn engineers publish a HotCloud paper on a partitioned graph that answers distance queries while a page loads.

  5. 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.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 3

Connect. A accepts B's invitation; one transaction records it

Understand it

Paper dolls holding hands
123Three hand holds apart

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 typeWhat differs
Friends on FacebookSame shape: mutual edges and friend suggestions
Followers on X or InstagramOne-way edges, and some members have millions of followers
Developers on GitHubOne-way follows, plus shared projects
Members of a Slack workspace or Discord serverGroups decide who can see whom, not hops
Payments on VenmoEdges appear from activity
Neighbours on NextdoorPlace 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

Members reach a gateway. Degree requests go to the degree service, which reads the in-memory graph of 20 shards and a cache of two-hop sets. Invitations go to the connection service and PostgreSQL. A change log in Kafka feeds the graph. The suggestion service reads a store filled by Spark, and checks privacy rules.
Blue is the read path, pink the write path, orange the asynchronous path. Dashed means asynchronous.

Which page owns each idea

IdeaRead
Splitting lists over machinesSharding
Feeding the graph from the databaseChange data capture
A read model built beside the write modelCQRS
The log that carries changesLog-based queues
Expiry and stale answersCache consistency
Asking many machines at onceFan-out and fan-in
Ranking suggestionsRecommendation 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