Design a Social Media News Feed (Twitter / X)
Sub-100ms personalized feeds at 500K RPS: Kafka dual consumer groups, hybrid fan-out (push for normal users, O(1) Redis pull for celebrities), CDC celeb detection, and buffered viral likes.
The whiteboard
Trace tweet & fan-out, <100ms feed read, and follow & celeb CDC below — composing Distributed Unique IDs, Sharding & Hotkeys, and Queues vs Event Logs.
whiteboard · press 1–4 to trace flows
Hybrid fan-out + shard alignment: push normal tweets on write, pull celebrity timelines on read, and shard timelines by userid vs bodies by twid.Avoids 50M-row celebrity write storms while keeping 500K RPS home feed reads under 100ms.
Scale & APIs
< 100 ms
p99 home feed latency
500K RPS
home feed reads
8 bytes
Snowflake time-sorted tweet ID
Constraints: < 100ms p99 reads · high availability (eventual fan-out) · 10M+ follower celebritiesEdge / IDs: gateway (auth, rate limit) | snowflake (64-bit k-sorted tweet_id)
POST /tweets -> { tweet_id, media_upload_url? } (S3 presigned)GET /tweets/:tid -> TweetGET /feed?cursor=:tid -> Tweet[]POST /follow/:uid -> 200 OKHow it works
1. Tweet ingestion & hybrid fan-out (post -> Kafka -> 2 consumer groups)
- Decoupled consumers:
post tweetmints a k-sorted 8-byte Snowflake ID and publishes to Kafka, splitting Consumer Group 1 (tweet writer) (persists body toTweet PGand appends totimeline:{celeb_uid}in Redis) from Consumer Group 2 (feed builder). - Normal vs celebrity split:
feed builderpushes(userid, tweetid, ts)into followers’Feed PG+ Redis for normal users, but skips fan-out completely for celebrities to avoid 50M-row write storms. - Read-your-own-writes: Write the new tweet synchronously to
Tweet PG(or write-through to Redistweet:{id}+ the author’sfeed:{uid}) before returning200 OKso the author never sees a missing post on refresh.
2. Social graph & celebrity CDC (follow svc -> Celeb Following)
- Compact celebrity index:
follow svcwrites toFollower PG, and CDC asynchronously updates follower counts inCelebplus maintainsCeleb Following PG— storing only the 5–20 celebrities each user follows instead of all 1,000+ accounts.
3. The <100ms feed read & viral likes
O(1)merge & hydrate:feed svcfetchesfeed:{uid}and each followed celebrity’s latest ~50 IDs from Redis (shard: userid), merges by Snowflake ID, hydrates bodies in one batchedMGETagainst Redis (shard: twid), and serves media viaS3 -> CDN.- Buffered viral likes: Never run
UPDATE tweets SET likes = likes + 1directly onTweet PG— 50K likes/sec locks one row and stalls connections; buffer increments in Redis (INCR) or Kafka and flush batched deltas to Postgres.
lesson
Sharding key alignment makes every read O(1): timelines (feed:{uid}, timeline:{celeb}) shard by userid, while tweet bodies (tweet:{twid}) shard by twid. Mixing both under one key forces scatter-gather reads.
Check yourself
interactive · stress-test this news feed design against classic Twitter/X probes
01 · Why keep a separate Celeb Following table?
<100ms feed read · 1,200 followed accounts · 12 celebs
A user follows 1,200 accounts, of which 12 are flagged as celebrities. Why does feed svc query Celeb Following PG instead of scanning Follower PG at read time?
Select an option to check the trade-off.
02 · Celebrity posts a viral tweet getting 50,000 likes/sec
50K likes/s · single row hot lock · Tweet PG
A celebrity tweet goes viral and receives 50,000 likes/sec. Why does running UPDATE tweets SET likes = likes + 1 WHERE id = :tid directly on Tweet PG cause an outage?
Select an option to check the trade-off.
03 · Author refreshes immediately after posting
async Kafka lag · read-your-own-writes · UX
A user clicks Post Tweet, gets 200 OK, and immediately refreshes their profile 50ms later. If Tweet PG is only written asynchronously by Kafka Consumer Group 1, what can go wrong?
Select an option to check the trade-off.
k-sortedordering
Roughly time-ordered: every item sits within k positions of its true global order, without requiring cross-node locks.