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.
See it run
Connect two people, measure the distance between them, and suggest the next connection, one hop at a time.
Operations
Send and accept an invitation
The database decides, and everything else follows from its log.
Step 1 of 11
Asha presses Connect
target
Lee
The app sends the target, an optional message and an idempotency key. Why: a retry on a bad connection must not create a second invitation.
Step 1: Asha presses Connect
def invite(sender, target, key): limiter.check(sender) # e.g. 100 a week low, high = sorted((sender, target)) if blocks.between(low, high): return pending_lookalike() row = db.insert_if_no_open(low, high, sender) # partial unique index if row.sender != sender: return accept(row) # both asked return rowdef accept(row): with db.transaction(): row.status = "accepted" db.insert_connection(row.low, row.high) db.outbox.add("connected", row.low, row.high)- Latency
- < 300 ms
- Load
- 580 accepts/s
- Peak (3x)
- 1,740/s
- Invitations
- 1,160/s
The connection and its outbox row are one transaction, so the graph can be late but never wrong for good.
In the real world
The same connection graph doing real jobs. Pick one and step through it.
The target has 30,000 connections.
Step 1 of 4
Two very different lists
- mine
- 500
- theirs
- 30,000
500 against 30,000.
Step 1: Two very different lists
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Walking outward
criticalTwo hops read 250,000 entries and three hops up to 125 million.
Wrongfor hop in range(3): frontier = all_friends(frontier)Rightif b in friends(a): return 1if overlap(friends(a), friends(b)): return 2Sampling a supernode
criticalA missing mutual friend turns a 2nd degree into a 3rd. Use the short list instead.
Wrongsample = random.sample(big, 1000)return any(x in mine for x in sample)Rightreturn any(x in big for x in small)Guarding one direction only
majorTwo people can invite each other at the same time and both rows are accepted by the constraint.
WrongUNIQUE (sender_id, receiver_id)RightCREATE UNIQUE INDEX one_openON invitations (low_id, high_id)WHERE status = 'pending';Caching each pair
majorPairs rarely repeat, and one accept would have to delete entries you cannot list.
Wrongcache.set(f"deg:{a}:{b}", 2, ttl=900)Rightcache.set(f"hop2:{viewer}", two_hop, ttl=600)Writing two stores in one request
criticalIf the second write fails, the copies differ for ever.
Wrongdb.insert(conn)graph.add(conn)Rightwith db.transaction(): db.insert(conn) db.outbox.add(conn)
Interview tips
Lead with numbers
- "All lists are 2 TB, so they fit in memory."
- 69,000 profile checks a second at peak.
- Three hops one way is 125 million people.
Explain trade-offs
- Hash shards: even load, more fan-out.
- Set per viewer: fast pages, ten minutes old.
- Memory copy: speed for a second store.
Anticipate failures
- Set cache lost: slower, never wrong.
- Log paused: distances freeze, then catch up.
- Privacy unreachable: hide.
Show care
- Blocks hold on the next request.
- Suggestions have allowed signals only.
- A hide button that is remembered.