skip to content
jaeaeichsysdes

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

WRITE & HYBRID FAN-OUT<100MS FEED READ (DUAL REDIS SHARDS)SOCIAL GRAPH & CELEB CDCPOST /tweetspush IDswrite body/feedO(1) IDsMGETceleb IDsmedia URLPOST /followCDCClientpost / feed500K RPS readpost tweetSnowflake IDS3 presigned URLKafka2 consumer grpswriter + feed buildernormal: push feed:{uid}celeb: skip fan-outfeed svcmerge & hydrate<100ms p99Redis + Feed PGfeed:{uid} · timeline:{celeb}sharded by useridRedis + Tweet PGtweet:{twid} MGETsharded by twidfollow svcFollower PGCDC streamCeleb Following5-20 celebs / userflags >1M followersS3 -> CDNmedia edge cachebuffered viral likes

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 celebrities
Edge / IDs: gateway (auth, rate limit) | snowflake (64-bit k-sorted tweet_id)
POST /tweets -> { tweet_id, media_upload_url? } (S3 presigned)
GET /tweets/:tid -> Tweet
GET /feed?cursor=:tid -> Tweet[]
POST /follow/:uid -> 200 OK

How it works

1. Tweet ingestion & hybrid fan-out (post -> Kafka -> 2 consumer groups)

  • Decoupled consumers: post tweet mints a k-sorted 8-byte Snowflake ID and publishes to Kafka, splitting Consumer Group 1 (tweet writer) (persists body to Tweet PG and appends to timeline:{celeb_uid} in Redis) from Consumer Group 2 (feed builder).
  • Normal vs celebrity split: feed builder pushes (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 Redis tweet:{id} + the author’s feed:{uid}) before returning 200 OK so the author never sees a missing post on refresh.

2. Social graph & celebrity CDC (follow svc -> Celeb Following)

  • Compact celebrity index: follow svc writes to Follower PG, and CDC asynchronously updates follower counts in Celeb plus maintains Celeb 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 svc fetches feed:{uid} and each followed celebrity’s latest ~50 IDs from Redis (shard: userid), merges by Snowflake ID, hydrates bodies in one batched MGET against Redis (shard: twid), and serves media via S3 -> CDN.
  • Buffered viral likes: Never run UPDATE tweets SET likes = likes + 1 directly on Tweet 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?

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?

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?

esc
⌘B sidebar[ ] prev/next1–5 flowsc copyt theme