What an interviewer expects you to nail down before drawing a single box.
POST /api/v1/tweets { text, media_ids? } → { tweet_id, created_at }GET /api/v1/home_timeline { cursor?, limit=50 } → { tweets[], next_cursor }GET /api/v1/users/{id}/tweets { cursor?, limit } → { tweets[], next_cursor }Pull up your Twitter home timeline and a fresh mix of tweets from everyone you follow is just there, in well under a second. That speed hides a hard problem. If the system rebuilt your timeline from scratch on every open, reads would crush it. If it instead pushed every new tweet to all of an author's followers, a single celebrity post would mean 100 million writes. Twitter dodges both extremes with a hybrid design that this system walks through.
For a normal user, the trick is to do the work ahead of time. When you tweet, the Tweet Service saves it durably to Cassandra and drops an event on Kafka, then returns immediately. A Fan-out Worker picks up that event, looks up your few thousand followers, and pushes the tweet id into each of their precomputed timelines in Redis. So when those followers open the app, their timeline is already sitting in cache, ready to read in well under a millisecond.
Celebrities break that math, so they get the opposite treatment. The system skips fan-out entirely for them and instead keeps a small cache of each celebrity's most recent tweets, keyed by the author. Think of it like a popular newspaper. You do not mail a fresh copy to every reader's house when a famous columnist writes. You print once and let readers grab it from the stand. One cached entry serves millions of followers instead of being copied into millions of timelines.
The Timeline Service is where the two halves come together. On each open it reads your precomputed list of regular-follow tweets from Redis, pulls the recent tweets of the celebrities you follow from their author cache, merges the two streams, ranks them, and hydrates the full tweet bodies from Cassandra. This system teaches the fan-out-on-write versus fan-out-on-read trade-off, why a hybrid takes the cheap side of each, and the hot-key risk that comes with caching the most-followed accounts.