Loading...

A 20-minute mock system design interview. You drive the pace. Jump to the board when you are ready; skip capacity math if you want. The interviewer grades your thinking, not a template match.
The whole question turns on one object: the home timeline. Writes are rare (about 1,150 tweets a second); reads are enormous (about 325K tweet views a second, near 1M at peak). Where you pay for the join between "who I follow" and "what they posted" decides everything else. Then one account with 100 million followers tweets, and your answer gets tested.
Two shared surfaces (only one is on screen at a time; use the panel arrow to switch):
Build incrementally while you talk. Trace one tweet end to end through your diagram: Alice posts it, Carol sees it on her next refresh. Argue the tradeoff, not "industry standard."
Read this before you go in. These are the decisions, and why they matter.
The obvious first answer is a query: tweets where the author is someone I follow, newest first, limit 20. Say it, then kill it. A user follows about 200 accounts whose tweets live on many shards, so every timeline load becomes a scatter-gather and a merge-sort. At hundreds of thousands of reads a second, with a 200 ms budget, the database spends its life doing that join.
The fix is to precompute. When a tweet is posted, push its ID into each follower's home timeline, a per-user list kept in memory. A timeline read becomes one cache lookup. You moved the join from read time, where there are ~280 reads per tweet, to write time, where it happens once per tweet. Do it asynchronously: the post returns as soon as the tweet is stored, and a queue plus a worker fleet does the fan-out. The requirement allows a few seconds of delay; spend it here.
Fan-out on write (push) costs one timeline insert per follower. With an average of 200 followers, 100M tweets a day is 20B inserts, about 230K a second. Fine. Now an account with 100M followers tweets: 100M inserts for one tweet. Even at a dedicated million inserts a second that is 100 seconds, and during a live event the queue backs up for everyone.
Fan-out on read (pull) stores the tweet once and merges at read time. No write amplification, but it is the slow join again.
Hybrid is the sound default. Push for ordinary accounts. Above a follower threshold, skip fan-out; at read time, merge those few accounts' recent tweets into the precomputed list. Most people follow only a handful of such accounts, so the merge is small. Twitter ran exactly this. The threshold is a knob you tune on fan-out lag, and a normally small account that goes viral can be switched to pull. Also skip fan-out to inactive users (say, no login in 30 days) and rebuild their timeline when they return.
Copying full tweets into every follower's timeline multiplies memory by the follower count and turns every delete or edit into millions of writes. Store only IDs: TweetID, author ID, a few flag bits (retweet, reply). Cap the list (Twitter used 800). Hydrate at read time from a shared tweet cache that holds each tweet once, plus a user cache for names and avatars.
The sizing is worth doing: 800 entries × 20 bytes = 16 KB a user; 200M active users is about 3.2 TB of RAM per copy. Replicate it. It is derived data, so a lost shard can be rebuilt from the tweet store and the follow graph, but a rebuild storm is expensive.
By UserID: one user's tweets on one shard. Reading a user's profile is cheap, but a celebrity's shard runs hot and data grows unevenly. By random TweetID: writes and reads spread evenly, but "recent tweets from these 200 people" asks every shard and an aggregator merges. By creation time: latest tweets sit on a few shards, but every write lands on the newest shard and it melts while the rest idle.
The combined answer: make the TweetID itself time-ordered, with epoch time in the leading bits, and shard by it. The primary key doubles as the time index, so there is no secondary index to maintain and "newer than X" is a range scan. One catch the books gloss over: choose the shard by hashing the whole ID, not by its time bits, or you rebuild the creation-time hotspot. Time order is useful inside a shard, not for picking one.
Grokking sizes it: 50 years is ~1.58B seconds, so 31 bits of epoch seconds, plus a 17-bit per-second sequence (131,072 tweets a second) = 48 bits, from a pair of sequence servers issuing even and odd numbers. It works on average traffic. But Twitter's real peak in 2013 was 143,199 tweets a second, which overflows 17 bits, and the sequence servers are central. Snowflake, Twitter's actual generator, is 64 bits: 41 bits of milliseconds, 10 bits of datacenter and machine, 12 bits of sequence (4,096 per millisecond per machine). No coordination after startup; the thing to watch is clock drift. Design IDs for the peak. UUIDs are unique but not sortable, which breaks both the sharding trick and cursor pagination.
About 24 TB of new photos and video a day, and roughly 35 GB/s of egress, 22 GB/s of it video. Text egress is under 100 MB/s. So media never touches the timeline path: the client uploads to a Blob Store first, gets media IDs, attaches them to the tweet, and viewers load media from the CDN.
Because timelines hold IDs, a delete is a tombstone on one tweet. The hydration step drops it, so it vanishes from every timeline on the next read; purge the IDs lazily. Unfollows are filtered at read and cleaned up later. If a timeline cache node dies, serve from a replica, or rebuild by pull, rate-limited so the rebuild does not flatten the tweet store. Asking "must deletes be immediate?" during scoping is a strong signal; almost nobody does.
One complete design you can study, then rebuild in your own words. Do not recite it. In the interview, draw your graph and defend the choices.
Rough numbers are fine; the method is what gets graded. The win is using them: "three days of text fits in RAM; video forces a CDN; the 100M-follower tweet forces a hybrid."
How the graph works: clients hit a load balancer that splits two services. The write path stores the tweet under a time-ordered ID and drops a fan-out job on a log; workers read the author's followers and prepend the ID to each follower's timeline list in Redis. The read path reads that list, merges in any celebrity followees' recent tweets, hydrates the IDs from the tweet cache, and returns a page. Media goes around both, through the blob store and CDN.
Web Client, Mobile Client
→ Load Balancer
→ Service: Tweet Service (mark distributed)
→ Service: ID Service (Snowflake)
→ Cassandra: tweet store, sharded by hashed TweetID (mark distributed)
→ Memcached: tweet cache (recent tweets, hydrate source)
→ Kafka: fan-out log
→ Worker: fan-out workers (mark distributed)
→ MySQL: social graph (followers / following)
→ Redis: home timelines, ~800 IDs per user (mark distributed)
→ Redis: author recent-tweets (every author; the only write for celebrities)
→ Service: Timeline Service (mark distributed)
→ Redis: home timelines
→ Redis: author recent-tweets (celebrity merge at read time)
→ Memcached: tweet + user cache → Cassandra on miss
Web/Mobile Client → CDN → Blob Store (photos, video; uploaded before the tweet)
Components:
max_id for older, since_id for newer), which only works because IDs sort by time.media_id.POST /tweets with text and media_id. The Load Balancer routes to any Tweet Service instance.(tweet_id, author=alice) to the Kafka fan-out log.A 100M-follower account tweets: the worker sees the author is over the threshold and writes only to the author's recent-tweets list. No 100M-insert storm. Followers pick the tweet up at read time in step 6.
Carol was away for two months: she has no timeline in Redis. The Timeline Service rebuilds it once by pulling recent tweets from her followees, stores it, and serves it. The first load is slower; the rest are normal.
Alice deletes the tweet: a tombstone on the tweet in Cassandra and the cache. Step 7 drops it for every reader immediately. The IDs age out of timelines or get purged lazily.
A Redis timeline shard dies: serve from a replica. With no replica, rebuild affected timelines by pull on demand, rate-limited to protect the tweet store.
Tweet(tweet_id PK, user_id, content, media_ids, tweet_lat, tweet_long, created_at, num_favorites, deleted)
User(user_id PK, name, email, created_at, last_login)
Follow(follower_id, followee_id, created_at) — stored twice: by follower and by followee
Favorite(tweet_id, user_id, created_at)
HomeTimeline: user_id → list[(tweet_id, author_id, flags)], capped ~800 — Redis
AuthorRecent: author_id → list[tweet_id], newest first — Redis
tweet_id is a Snowflake-style 64-bit ID; its leading bits are time, so it is also the sort keytweet_id; three replicas, reads from secondariestweet_id and no contentPure pull with aggressive caching of each author's recent tweets, if you can show the merge stays inside 200 ms. Push to online users only, pull for everyone else. A different celebrity boundary, or a dynamic one. DynamoDB, MySQL shards or HBase instead of Cassandra; Memcached or Redis for the tweet cache; a graph database for the follow graph. Grokking's 48-bit ID if you size it for the peak. A ranked timeline instead of chronological, if you still precompute candidates. What matters is that you can argue the tradeoff.