TSTier SlateAll templates
  1. Home
  2. Templates
  3. The social graph

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.

  • System design
  • core
  • 8 step walkthrough
  • 5 tables
Own this board 400 creditsPractise as an interview

The first act plays here. Owned once with credits, never a subscription.

Share

  • Facebook
  • X

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.

Practise this as an interview

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

  1. Press Play and follow the walkthrough: WRITE (one intention) across the top left, then ADJACENCY on the right (the same edge, twice).
  2. Then EVENTS (what was intended) and READ along the bottom of the diagram - 300 reads for every write.
  3. 1 · Requirements - directed or mutual, consistency of a follow, blocks.
  4. 2 · Capacity estimation - the two read ratios - then 3 · Storage estimation.
  5. 4 · Availability, 5 · How it works, 6 · Deployment plan.
  6. 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

Preview of the The social graph system design board: 15 components joined by 17 connections. The labelled board opens in Tierslate.
Board preview

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
Open this board in TierslateBrowse every board

Related system design templates

Timelines, follows, fan-out - the read-heavy classics. 10 more social & feeds templates:

  • Social news feed system design - Fan-out on write against fan-out on read, and when each loses.
  • Media feed system design - Why the feed serves URLs and the edge serves bytes, and who actually pays for the pixels.
  • The professional graph system design - Why a degree badge on every name is a graph held in memory and searched from both ends, not a join.
  • Comment trees system design - How a whole comment tree comes back in one range scan, and why every vote rewrites what it cached.
  • Home timeline system design - Why one post is fanned out into hundreds of precomputed timelines, and why the celebrity cannot be.
  • Blog platform system design - Why a blog is a read-heavy system where the cache is the product, and what publishing has to invalidate.
  • Link previews system design - Why unfurling a pasted URL is a server-side request forgery problem first and a caching problem second.
  • Reviews and ratings system design - Why a star rating is an aggregate maintained on write, and how fake reviews are held back before they count.
  • Content moderation system design - Why moderation is a fast automatic gate plus a slow human queue, and what is shown while it decides.
  • Stories system design - Why expiry is a read-time filter plus a background sweep, and why the viewer list is the expensive part.
Open the The social graph board in TierslateAll social & feeds templatesAll 100 system design templates
Tierslate

tierslate.com

HomeTemplatesGuidesCompareAboutUI mockupsPrivacyTerms