Feed de Noticias
Timeline · Fan-out
Escrito por Darjus Kazakevic · revisado el
Precompute feeds for most users; merge celebrity posts at read time.
Users publish posts and follow other users. The home feed shows recent posts from everyone they follow, 20 at a time, with endless scrolling. Ranking can start as newest-first and become a scored ranking later.
The feed must load in under 200 ms at p99, and a new post should reach followers within a few seconds. Feed reads outnumber posts about 20 to 1. A few accounts have tens of millions of followers, and the design must survive them.
En esencia — A feed is a precomputed list of post IDs per user. Build it at write time for normal accounts, and at read time for the few with huge audiences.
Cuándo recurrir a él
Recurre a él cuando
- Many readers see a personal list built from what others publish
- Reads outnumber writes, and the list must load in under 200 ms
- A few seconds of delay before a new item appears is acceptable
Reconsidéralo cuando
- Every user sees the same list: cache one shared page instead
- Items must appear instantly and in strict order, as in a chat thread
- You have a few thousand users: a SQL join at read time is fast enough
The components
- Post service and store (uses database sharding)
- Saves each post once, keyed by post ID and sharded by author. Publishing returns as soon as the post is saved and an event is on the queue.
- Fan-out workers (uses a message queue)
- Consume "post created" events, read the author's follower list, and append the post ID to each follower's feed. Fan-out means one write becomes many copies.
- Feed cache (uses a distributed cache)
- One sorted set per user in Redis: the newest ~800 post IDs, scored by time. It holds IDs only, about 16 bytes each, never the post itself.
- Feed service (uses caching strategies)
- The read path. It takes the IDs pushed to the user, adds the latest posts of followed celebrities, and sorts them. Then it hydrates them: it fills in each post and author from their own caches.
- Social graph
- Who follows whom, stored both ways (followers of X, accounts X follows) and sharded by user ID. Fan-out reads one direction; the celebrity merge reads the other.
Key decisions
- Fan out on write, except for celebrities
- Writing a post ID into 200 feeds is cheap. Writing it into 50 million takes minutes and is mostly wasted. Above a threshold (say 10,000 followers), skip fan-out and merge those posts at read time.
- Store IDs in the feed, not posts
- A feed entry is a post ID and a time, about 16 bytes. Hydrate the post from its own cache when you read. An edit or a delete then changes one row, not a million copies.
- Make fan-out asynchronous
- Publishing returns once the post is saved. A queue feeds the fan-out workers, so a burst of posts delays delivery by seconds instead of slowing every publish.
- Cap each feed and skip inactive users
- Keep the newest ~800 IDs per user and serve older pages from the post store. Do not fan out to users who are away for 30 days; build their feed on read when they return.
Errores habituales
- Fan-out on write for every account. One celebrity post queues 50 million writes, and every other post waits behind it for minutes.
- Storing whole posts in each feed. An edit or a delete must touch millions of copies, and the cache grows about 50 times larger than it needs to be.
- Fanning out to inactive users. Most writes land in feeds that nobody opens. Build those feeds on read when the user comes back.
- Paging with an offset while new posts arrive. Every new post shifts the pages, so users see duplicates. Page with a cursor: the time and ID of the last post shown.