System Design: News Feed Cache Layer
On this page
- The problem: design a news feed cache layer
- Estimating news feed traffic
- Cache feed lists and posts separately
- Filling feed lists: fan-out on write vs fan-out on read
- Cache strategies, eviction and TTLs
- Invalidating edits, deletes and unfollows
- Protecting hot posts and surviving cache failures
- The outer layers: CDN and client
- What a strong news feed cache design includes
- Check yourself
The problem: design a news feed cache layer
A social app has 50 million daily users who open their feed about 10 times a day. Each user follows a few hundred accounts; some accounts have millions of followers. The feed must load in under 200 ms. Posts can be edited or deleted. Design the cache layer. Decide what to cache, how it gets there, how it stays correct, and how it survives a celebrity post.
Estimating news feed traffic
Feed reads 50M × 10 ÷ 86,400 ≈ 6,000 per second average, ~15,000 at peakReads dominate writes many times over, and every feed read touches dozens of posts. Without caching, that's hundreds of thousands of post lookups per second against the database. Caching is the core of the design, not an add-on.
Cache feed lists and posts separately
- Feed lists: for each user, an ordered list of recent post ids, kept in a Redis sorted set scored by time and capped at a few hundred entries.
- Post objects: content, author, media links and counts, keyed by post id.
Separating them is the key decision. A viral post appears in millions of feeds but is stored once. Editing it changes one cache entry, not millions.
Filling feed lists: fan-out on write vs fan-out on read
Fan-out on write for most accounts: when someone posts, push the post id into each follower's cached feed list. Reads are then one sorted-set range plus a batch of post lookups.
Fan-out on read for accounts with huge follower counts: don't push their posts; merge their recent posts into a follower's feed when it's loaded. That prevents one celebrity post from triggering millions of writes. (Day 7 of the System Design Interview course covers this hybrid in depth.)
Users who haven't opened the app in weeks get no fan-out; their feed list is rebuilt from the database when they return, so cache memory goes to active users.
Check that it fits in memory (Day 7). A Redis sorted set of a few hundred entries costs roughly tens of bytes per entry once you count Redis's own overhead; assume about 60. Then:
All 50M users × 300 ids × 60 bytes ≈ 900 GB
20M active users × 300 ids × 60 bytes ≈ 360 GBEither way that's a Redis Cluster of many nodes, not one server, and it's why the cap on list length and the skip for inactive users matter. Measure the real per-entry cost with MEMORY USAGE on a sample before you size the cluster.
Cache strategies, eviction and TTLs
- Post objects: cache-aside, a TTL of hours with jitter, LRU eviction. Most traffic goes to recent posts, which LRU keeps.
- Like and comment counts: write-behind. Increment in Redis, flush to the database periodically. A count that's a second behind is fine.
- Feed lists: built on write, trimmed to a fixed length, and given a TTL of a few days so inactive users' lists age out.
Invalidating edits, deletes and unfollows
- Edit: update the database, delete
post:{id}, publish apost.updatedevent so every region's cache drops it. - Delete: mark the post deleted and invalidate its cache entry. Feed lists may still contain the id; filter deleted posts when rendering instead of scrubbing millions of lists.
- Unfollow: filter at read time, and lazily remove that author's posts from the follower's list.
Protecting hot posts and surviving cache failures
A celebrity post is read tens of thousands of times a second. Protect it with single-flight refills, early refresh before expiry, and a short-lived copy in each app server's local memory so hot posts don't even hit Redis for every read.
Plan for losing part of the cache too. If a Redis node fails, its users' feed lists are gone, and rebuilding millions of them from the database at once is its own stampede. Rebuild lazily as each user returns, cap concurrent rebuilds, and while a feed is rebuilding, show a cheaper fallback such as the latest posts from the user's top accounts.
The outer layers: CDN and client
Images and videos come from the CDN with versioned URLs. The feed API response itself is personal, so it's private and never cached at the CDN. The app keeps already-loaded posts in memory while the user scrolls.
What a strong news feed cache design includes
- Feed lists and post objects cached separately, with the reason.
- A fan-out choice that handles huge accounts.
- A strategy, TTL and eviction policy per cached thing.
- Edits and deletes handled without rewriting millions of feeds.
- Explicit protection for hot keys.
- A memory estimate for the feed lists, and a plan for losing a cache node.
- Personal responses kept out of shared caches.
Check yourself
A post appears in many feeds. Storing it once keeps memory small and means an edit or delete invalidates one entry instead of millions.
Invalidate the cached post and mark it deleted, then filter deleted ids when feeds are read, rather than editing every feed list.
They change constantly, and being briefly off by a few is harmless, so batching updates to the database saves enormous write load.
Keep lists only for active users and rebuild others on return, and cap each list at fewer entries. Both shrink memory without changing what active users see.
For accounts with millions of followers. Writing each of their posts into millions of feeds is slow and expensive, so their posts are merged into followers' feeds at read time instead. Most designs use a hybrid: fan-out on write for normal users, on read for celebrities.