The Social Graph: How Twitter Stores Followers and Following
The Social Graph — How Twitter Stores Followers and Following
The fan-out service needs to answer one question on every tweet: "who follows this author?" That question is answered by the social graph store, historically called FlockDB (and its successor, Flock). Understanding how this is built explains both why fan-out is fast and where it hits limits.
What the social graph is
At its core, the social graph is a directed edge list. Each follow relationship is one directed edge:
user A follows user B → edge (A → B)
"who does A follow?" → outgoing edges from A (A's "following" list)
"who follows B?" → incoming edges into B (B's "followers" list)
Twitter needs to answer both directions efficiently and at enormous scale (~500M follow relationships in 2013, orders of magnitude more today).
FlockDB: a graph database optimized for adjacency lists
FlockDB is not a general-purpose graph database (it can't do multi-hop traversals like "friends of friends"). It's optimized for one thing: fast adjacency list lookups — give me all the edges in or out of a single node.
The data model is simple:
(source_user_id, graph_id, destination_user_id, position)
graph_id = 1 → "follows" edge
graph_id = 2 → "blocked" edge
... etc.
Stored in sharded MySQL, partitioned by source_user_id. To answer "who does user 42 follow?", you hit the shard for user 42 and read all rows with source_user_id=42, graph_id=1 — a single range scan.
Two directions, two copies
Because both "who does A follow?" and "who follows B?" are hot paths, FlockDB stores both forward and reverse edges:
Forward: (follower_id → followed_id) answers "who does A follow?"
Reverse: (followed_id → follower_id) answers "who follows B?"
A single follow action writes to both copies. This doubles write cost but makes both read directions O(1) range scans rather than full-table joins. Classic read-optimize-by-doubling-writes trade.
Sharding strategy
The graph is sharded by source_user_id (for forward edges) and separately by destination_user_id (for reverse edges). This means:
- Fan-out query ("who follows author X?") → hits the reverse-edge shard for X → single shard, sequential scan
- Timeline query ("who does viewer 42 follow?") → hits the forward-edge shard for 42 → single shard, sequential scan
No cross-shard joins needed for the common case. The shard containing a celebrity's reverse edges can be a hot shard (100M rows, frequently read during fan-out) — this is mitigated by read replicas and caching (more below).
Caching the follower list
For fan-out, the critical query is "who are the followers of author X?" This is read on every tweet by that author. For celebrities, the follower list is enormous (millions of entries) and queried at spike moments. Two strategies:
1. In-memory cache in the fan-out service
The follower list for high-traffic authors is kept in memory inside the fan-out workers. On a tweet, the worker uses the cached list directly rather than querying FlockDB. The cache is invalidated / updated when a follow/unfollow happens.
2. Paginated streaming for large lists
For a celebrity with 50M followers, you can't load the full list into memory at once. The fan-out worker streams the follower list page by page from FlockDB (e.g., 10K follower IDs per page), processing each page's ZADDs before fetching the next. This keeps memory bounded at the cost of more round trips to the graph.
fan-out worker:
page 1: fetch follower IDs 1–10,000 → ZADD to their timelines
page 2: fetch follower IDs 10,001–20,000 → ZADD to their timelines
...
page N: fetch final batch → ZADD to their timelines
This is also why large fan-outs take seconds — it's not just the ZADDs, it's the sequential page fetches from the graph.
What happens on follow / unfollow
A follow is a write to both edge directions in FlockDB, plus a cache invalidation:
user 42 follows user 7:
1. insert (42 → 7) into forward-edge shard for user 42
2. insert (7 → 42) into reverse-edge shard for user 7
3. invalidate / update cached follower list for user 7
4. (optionally) backfill: inject user 7's recent tweets into user 42's timeline
Step 4 — backfill — is the interesting one. When you follow someone, you expect to see their recent tweets immediately, not just their future ones. So the system fetches the followed user's recent tweets from the tweet store and ZADDs them into your timeline. This is a mini read-time fan-out triggered by the follow action.
Unfollow is the reverse: delete both edges, and prune the unfollowed user's tweet IDs from your timeline cache (or let them age out naturally given the ~800 cap).
Why this can't be a simple relational DB
A naive SELECT destination_id FROM follows WHERE source_id = 42 on a single MySQL table works fine at small scale, but breaks at Twitter's because:
- Row count — 500M+ edges in one table; even with indexes, range scans at this scale on a single DB are slow
- Write throughput — millions of follows/unfollows per day; a single writer can't keep up
- Hot rows — a celebrity's reverse-edge rows are read by every tweet they post; one DB can't serve this without read replicas and caching anyway
FlockDB solves this by being purpose-built: it does exactly two things (forward lookup, reverse lookup) on sharded storage with no joins, no transactions across shards, and aggressive caching — and nothing else.
The graph at a glance
Follow action
(user 42 → user 7)
│
┌─────────────┴────────────────┐
▼ ▼
Forward shard (42): Reverse shard (7):
42 → [7, 19, 88, ...] 7 ← [42, 55, 99, ...]
"who does 42 follow?" "who follows 7?"
(used at read time to (used at write time to
build pull half, Section 4) drive fan-out, Section 2)
Both directions are hot paths at different moments: the reverse edge is read on every write (fan-out); the forward edge is read on every read (building the pull half of the hybrid, and for the follow-backfill on a new follow).
Primary Sources
- The Architecture Twitter Uses to Deal with 150M Active Users — High Scalability:
highscalability.com/blog/2013/7/8/the-architecture-twitter-uses-to-deal-with-150m-active-users.html - Twitter's Recommendation Algorithm (open source) —
github.com/twitter/the-algorithm - The Engineering Behind Twitter's New Search Experience (Earlybird) —
blog.twitter.com/engineering/en_us/a/2011/the-engineering-behind-twitter-s-new-search-experience(see the full Earlybird companion guide for a staff-level deep dive)