TOUGH TONGUE AI
  • Home
    LogoTOUGH TONGUE AI

    RESOURCES

    Loading...

    HomeLibrarySD Mock · Design Twitter

    SD Mock · Design Twitter

    SD Mock · Design Twitter

    Overview

    Design Twitter's home timeline on a live architecture board: drag components, wire the fan-out, and defend your design to an expert interviewer who watches every move.

    Your Instructions

    Design Twitter

    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):

    • Notepad. Shared notes. Requirements, capacity math, schema, and end-of-session feedback live here. You can type in it too.
    • Architecture board. Your design canvas. Drag typed components, wire them, label edges, mark scaled tiers as distributed. If it is not on the board, it is not in your design.

    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."

    Design choices interviewers probe

    Read this before you go in. These are the decisions, and why they matter.

    Where the timeline gets built

    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.

    The celebrity problem: push, pull, hybrid

    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.

    What a timeline actually holds

    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.

    Sharding tweets

    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.

    Generating the TweetID

    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.

    Media, and everything that is not text

    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.

    Deletes, unfollows, and losing a cache

    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.

    Reference solution

    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.

    Requirements (lock these or take the defaults)

    • Post tweets (text up to 140 characters in the classic question; 280 today) with optional photos and video
    • Follow and unfollow users (asymmetric); favorite tweets
    • Home timeline: recent tweets from everyone you follow, reverse chronological, paginated
    • Scale: 1B users, 200M DAU, 100M tweets/day, ~200 follows per user, no ceiling on followers
    • Non-functional: highly available, 200 ms timeline reads, eventual consistency (a few seconds late is fine), read-heavy
    • Out of scope unless raised: search, replies, trends, notifications, recommendations

    Capacity

    • Writes: 100M ÷ 86,400 ≈ 1,150 tweets/s; a few thousand at peak
    • Reads: 200M DAU × (2 timeline + 5 profile visits) × 20 tweets = 28B tweet views/day ≈ 325K/s, near 1M/s at peak. Read-heavy by ~280×.
    • Text: 100M × (280 B + 30 B metadata) ≈ 30 GB/day, so three days ≈ 90 GB fits one big cache server
    • Media: 20M photos × 200 KB + 10M videos × 2 MB ≈ 24 TB/day; egress ≈ 35 GB/s, mostly video
    • Fan-out: 100M tweets × 200 followers = 20B inserts/day ≈ 230K/s; one 100M-follower tweet = 100M inserts
    • Timeline cache: 200M × 800 IDs × 20 B ≈ 3.2 TB per copy

    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."

    Components to put on the board

    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:

    • Web Client, Mobile Client. Upload media first, then post; read the timeline with cursor pagination (max_id for older, since_id for newer), which only works because IDs sort by time.
    • Load Balancer. Spreads traffic across stateless service instances. Round robin to start; load-aware once some hosts run slow.
    • Service (Tweet Service). Authenticates and rate-limits the author, gets an ID, writes the tweet, enqueues fan-out, returns. Keeping fan-out out of this request is what keeps posting fast.
    • Service (ID Service). Snowflake-style 64-bit IDs: unique, time-ordered, generated per machine with no central sequence.
    • Cassandra (tweet store). Durable home of every tweet, sharded by hashed TweetID, replicated, with read replicas for a read-heavy load. Twitter's own stores were MySQL-based and later Manhattan; any wide-column or sharded store works if you can defend it.
    • Memcached (tweet cache). Whole tweet and user objects, each stored once. Holds the last three days of tweets (~90 GB) plus LRU for older hot ones. Without it, hydration would send ~325K lookups a second to the store.
    • Kafka (fan-out log). Durable, so a worker crash replays deliveries instead of losing them. Decouples post latency from fan-out cost.
    • Worker (fan-out workers). Read the author's followers, skip inactive ones, skip authors above the follower threshold, prepend the ID to each follower's list and trim it. Inserts are idempotent.
    • MySQL (social graph). Followers and followees, indexed both ways (FlockDB at Twitter). Read by the workers on every fan-out and by the timeline service to find celebrity followees.
    • Redis (home timelines). The load-bearing box. One capped list of IDs per active user, replicated. A read is one range call.
    • Redis (author recent-tweets). Each author's latest tweet IDs, newest first. The pull half of the hybrid: the timeline service merges celebrity followees from here.
    • Service (Timeline Service). Reads, merges, hydrates, filters (deleted, muted, unfollowed), paginates. Rebuilds a missing timeline by pull.
    • Blob Store + CDN. Photos and video. 24 TB/day in, ~35 GB/s out; the CDN absorbs the egress.

    Walk one tweet: Alice posts, Carol sees it

    1. Alice's client uploads a photo to the Blob Store and gets a media_id.
    2. The client calls POST /tweets with text and media_id. The Load Balancer routes to any Tweet Service instance.
    3. The Tweet Service gets a time-ordered TweetID from the ID Service, writes the tweet to its Cassandra shard (chosen by hashing the ID) and to the tweet cache, and returns the tweet URL. Alice is done.
    4. It appends (tweet_id, author=alice) to the Kafka fan-out log.
    5. A fan-out worker sees Alice has 1,200 followers, under the threshold. It pages her followers from the social graph, skips anyone inactive for 30 days, and prepends the ID to each follower's Redis home timeline, trimming to 800.
    6. Carol refreshes. The Timeline Service reads her list from Redis (Alice's tweet is at the top), then merges the recent tweets of the two celebrity accounts she follows from the author recent-tweets cache, ordering by TweetID.
    7. It hydrates the top 20 IDs from the tweet cache (misses fall through to Cassandra), drops anything deleted or unfollowed, and returns the page with a cursor. Alice's photo loads from the CDN.

    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.

    Schema

    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 key
    • Tweets shard by a hash of tweet_id; three replicas, reads from secondaries
    • Retweets are Tweet rows that carry the original tweet_id and no content
    • Follow edges are written in both directions so "followers of X" and "who X follows" are both single-key reads

    Scaling and wrap-up

    • Tweet Service, Timeline Service and fan-out workers are stateless and scale horizontally; Kafka partitions scale the fan-out log
    • Redis timelines shard by user ID and replicate; Cassandra shards by hashed TweetID
    • First metrics: tweets per second and the daily peak, fan-out lag (post to last follower timeline), timeline p99 latency and cache hit rate
    • If time: ranked "For You" timeline (Twitter's pipeline scores ~1,500 candidates a request, about half from outside your network), trending topics (sliding-window hashtag counts), search, live-event peaks (~25× average), multi-region

    Equally defensible variants

    Pure 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.

    More in System Design Interview Prep: AI Coach Edition

    • SD Coach · Building Blocks, One Server to Millions (Slides)
    • SD Coach · The Data Layer
    • SD Coach · Unique IDs and the URL Shortener
    • SD Mock · Design Pastebin
    • SD Coach · News Feeds and Typeahead
    • SD Mock · Design Facebook News Feed
    • SD Mock · Design Typeahead Suggestion
    • SD Mock · Design Twitter Search
    • SD Coach · Rate Limiting
    • SD Mock · Design an API Rate Limiter
    • SD Coach · Event-Driven Systems and Notifications
    • SD Mock · Design a Notification System
    View the full course (43 sessions) →