πŸ’¬
βœ‰οΈ
πŸ“Έ
▢️

Loading…

Design Twitter: feed fanout and timeline system design

TwitterGuided Mode

Pull Model (Fanout-on-Read)

The simplest approach: when @alice loads her feed, the API queries the Database for posts from every account she follows, merges, and sorts.

Request Flow

1

@bob posts a tweet

The tweet is written once to the Database. No fanout occurs at write time β€” fast writes, expensive reads.

POST /tweet { text: "Hello world" }
β†’ INSERT INTO tweets (author_id, text, ts)
β†’ 200 OK  // one DB write, done
2

@alice loads her timeline

The API issues one query per followed account to the Database, fetches their recent posts, then merges and sorts.

GET /timeline
β†’ SELECT * FROM tweets
   WHERE author_id IN (id1, id2, ... id500)
   ORDER BY ts DESC LIMIT 20
// 500 index lookups + in-memory merge sort
// Latency grows linearly with following count

Why We Need This

Pull is trivially correct β€” every read is always fresh. But it scales poorly: latency grows with following count, and popular accounts hit the DB from millions of followers' queries simultaneously.

Key Insight

Pull = O(following_count) queries per feed load. Works at low follow counts. Beyond ~50 follows, you need caching or pre-computation β€” or both.

Step 1 of 6