Social & feeds
The social graph - one edge, written twice
Why every follow is written twice on two shards with no transaction between, and what that drift costs.
The first act plays here. Owned once with credits, never a subscription.
What this board gets wrong on purpose
The tension
A follow is two writes on two different shards with no transaction between them, and nothing on this board repairs a half-finished one. If the in-edge write fails after the out-edge succeeded, Alice follows Bob and Bob has no follower Alice: her timeline fills with his posts and his fan-out never sends him to her. The edge log is the only ordered record of what was intended and nothing consumes it to reconcile - it is drawn as a log, not as a repair loop, which is exactly the state most real systems are in. The counters are the same bug in miniature and visible to users: they are incremented off a queue, so a dropped message loses a follower forever, and they have never heard of blocks, which the read path filters out of the list at request time - so the number and the list disagree by design and there is no job that recomputes either.
Practise this as an interview
Take The social graph - one edge, written twice as a timed system design interview on a blank board - on your own, with the requirements to check yourself against, or with a candidate you invite.
Start here
Why this design
The social graph - who follows whom - sits under every feed, notification and privacy check in a social product, so it is read far more than anything else. Interviewers use it to see whether you can store a directed graph of 150 billion edges so that both "who does Alice follow" and "who follows Bob" are one cheap lookup, and whether you notice that this forces every follow to be written twice, on two shards, with no transaction between them. Graph teams spend their careers on exactly that gap.
What you will learn
- Why each edge is stored twice - following(A) and followers(B) - and why the two copies live on different shards.
- Why a follow is four writes (two edges, two counter deltas) and what happens when only some of them land.
- How to size a system whose reads are a 300 : 1 multiple of its writes: 694,000 "does A follow B" checks a second, 98% absorbed by an adjacency cache, leaving about 6 : 1 for the database.
- Why the edge log - the ordered record of what was intended - matters more than either copy, and what a repair job would consume from it.
- Why follow and friendship are different entrances to the same graph, and why follower counts drift from the real list.
How to read this board
- Press Play and follow the walkthrough: WRITE (one intention) across the top left, then ADJACENCY on the right (the same edge, twice).
- Then EVENTS (what was intended) and READ along the bottom of the diagram - 300 reads for every write.
- 1 · Requirements - directed or mutual, consistency of a follow, blocks.
- 2 · Capacity estimation - the two read ratios - then 3 · Storage estimation.
- 4 · Availability, 5 · How it works, 6 · Deployment plan.
- Finally THE DATA MODEL below the band, and then the argument this board leaves open on purpose: nothing repairs a follow whose second write failed, and the counters never hear about blocks.
1 · Requirements
Clarifying questions to ask first
- Follow or friend, or both? A follow is one directed edge. A friendship is a request, a state machine and two edges. They are different systems and deserve different tables, not one table with a status column that is meaningless in half its rows.
- Are follower counts exact? If "approximately 2.1M" is acceptable, counters can be maintained asynchronously. If they must be exact, this becomes a much harder and much slower system.
- What is the maximum in-degree? An account with 100 million followers cannot have its follower list read into memory by anything, ever. Every consumer of that list must be a cursor.
- Do blocks hide an existing edge or delete it? Hiding preserves the edge and requires a filter on every read. Deleting is simpler and loses information the user may want back.
- Who consumes edge changes? Fan-out, notifications, recommendations and search all want to know. That answer decides whether there is a log at all.
- Must unfollow be immediate? It is the one operation users check by refreshing.
Functional
- Follow and unfollow another account; effective for the actor immediately.
- Send, accept and reject a friend request; an accepted request is a mutual pair of edges.
- Block an account: hide both directions, and prevent re-following.
- Answer does A follow B in single-digit milliseconds.
- List following(A) and followers(B), paginated with a cursor.
- Report follower and following counts.
Non-functional
- Edge existence check p99 under 5 ms. It is called on every profile view and inside fan-out.
- Follow write p99 under 100 ms as seen by the actor.
- 99.99% availability on edge reads - almost every other system on the site depends on them, so this one's availability multiplies into everybody else's.
- Counts may be approximate and up to 60 seconds stale, stated as a requirement.
- A block must take effect on the next read, not on the next TTL.
Explicitly out of scope
The feed, notifications, recommendations and search. They are drawn as consumers because the shape of this system is decided by who reads it.
The rest of this board
Available on Tier Slate
This page publishes the question. The answer — 5 more written pages, an 8-step narrated walkthrough and a 5-table schema — is the board itself, and it opens in Tierslate.
- 5written pages
- 8walkthrough steps
- 5tables22 columns
5 pages behind this one
- Capacity estimation
- Storage estimation
- Availability
- How it works
- Deployment plan