Design Exercise: News Feed (Twitter/Facebook)

Requirements

The Core Problem: Fan-Out

Fan-out on WRITE (push model): User posts → write to ALL followers' feeds immediately Pro: fast reads (feed is pre-computed) Con: celebrity with 10M followers = 10M writes per post (expensive!) Fan-out on READ (pull model): User requests feed → query all followed users' posts, merge, sort Pro: writes are cheap (just store the post) Con: slow reads (merge N users' posts at read time) HYBRID (what Twitter/Facebook actually do): Regular users: fan-out on write (push to followers' feeds) Celebrities (>10K followers): fan-out on read (merge at read time)

Architecture

Post Service ──► Kafka ──► Fan-out Service ──► Feed Cache (Redis) │ User requests feed ──► Feed Service ──► Feed Cache ─┘ │ └──► merge celebrity posts (on-read)

Storage