News Feed (Twitter / Instagram / Facebook)
Requirements: post, follow, view home timeline (reverse-chronological or ranked), low-latency feed reads; eventual consistency is fine.
Estimates: 200 M DAU, 2 posts/day → 5k writes/s; 20 feed loads/day → ~50k reads/s. Average 200 followers; celebrities have 100 M.
post: client -> post svc -> posts DB (sharded by post_id) + media -> object store/CDN
-> Kafka "new_post" -> fan-out workers
| for each follower (non-celebrity author)
v
feed cache: Redis list per user (post ids, capped ~800)
read: client -> feed svc -> Redis feed(user) ──merge── celebrity posts pulled at read time
-> hydrate ids via post cache -> rank -> page with cursor
Deep dives
- Fan-out on write (push): precompute each follower's feed → fast reads, expensive writes for big accounts.
- Fan-out on read (pull): build the feed at read time from followees → cheap writes, slow reads.
- Hybrid (the classic answer): push for normal users, pull for celebrities (and for inactive users, don't push at all).
- Pagination: cursor-based (
max_id) instead of offsets. - Ranking: candidate generation → feature lookup → ML scoring → re-ranking with diversity rules.
- Data: follow graph in a sharded store (
follower → followeesand reverse index); posts keyed bypost_id(Snowflake = time-sortable).