Quick revision
The short answer
Fan-out on write copies each new post id into every follower's feed list, so reads are cheap but popular accounts make writes expensive; fan-out on read builds the feed at read time from the people a user follows, so writes are cheap but reads are slow. A good design uses a hybrid: fan-out on write for ordinary accounts through Kafka workers into Redis sorted sets of post ids, posts from very popular accounts merged in at read time, post content cached separately, and new items pushed to online clients over WebSocket or server-sent events.
What to remember
In this answer, we compared fan-out on write, which makes reads cheap and writes expensive for popular authors, with fan-out on read, which does the opposite. We designed a hybrid that pushes ordinary authors' post ids into Redis sorted sets through Kafka workers and merges popular authors' posts at read time, kept content and counts out of the feed lists, and delivered new items to online clients through WebSocket or SSE gateways with a broker to reach the right node.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Fan-out on write copies each new post id into every follower's feed list, so reads are cheap but popular accounts make writes expensive; fan-out on read builds the feed at read time from the people a user follows, so writes are cheap but reads are slow. A good design uses a hybrid: fan-out on write for ordinary accounts through Kafka workers into Redis sorted sets of post ids, posts from very popular accounts merged in at read time, post content cached separately, and new items pushed to online clients over WebSocket or server-sent events.
Why this question is asked
Almost every social or collaboration product has a feed, and it is one of the classic system design questions because one decision, when to do the work of assembling the feed, changes the cost of everything else. Interviewers want to hear the trade-off explained with numbers, the special case of accounts with millions of followers, and how a new post actually reaches a screen within seconds. In this answer, we will define the two fan-out strategies, put rough numbers on them, design the hybrid with Kafka and Redis, store ids rather than content, and finish with the delivery path to connected clients.
1What We Are Building
Users follow other users. When someone posts, everyone who follows them should see the post at the top of their feed within a few seconds, and opening the app should show the latest 50 items quickly, say within 200 milliseconds. Fan-out means distributing one post to many followers. The question is whether we do that distribution when the post is written or when a follower reads.
Let us take concrete numbers to reason with: 20 million daily users, 2 million posts a day, an average of 200 followers per author, and feed reads about 100 times as frequent as posts. A small number of accounts, maybe a few thousand, have more than a million followers each.
2Fan-Out on Write Versus Fan-Out on Read
With fan-out on write, also called push, the system writes the new post's id into the feed list of every follower at posting time. A good structure for that list is a Redis sorted set per user, where the score is the post time, so the newest items come back first with one ZREVRANGE call. Reading a feed is then a single cheap lookup. The cost moves to writing: 2 million posts times 200 followers is 400 million feed insertions a day, about 4,600 a second on average. That is fine. But one post from an account with 30 million followers means 30 million insertions for a single post, which takes minutes and delays everyone else's posts behind it.
With fan-out on read, also called pull, posting just stores the post. When a user opens the feed, the system fetches recent posts from each account they follow and merges them. Writes are trivial, but reads are now expensive: a user following 500 accounts triggers 500 lookups and a merge on every refresh, and reads are the frequent operation.
| Fan-out on write | Fan-out on read | |
|---|---|---|
| Work at post time | One insert per follower | One insert |
| Work at read time | One sorted-set read | Query every followed account and merge |
| Read latency | Low and steady | Grows with accounts followed |
| Weak spot | Accounts with millions of followers | Users who follow thousands |
| Storage | One id list per user | Only the posts |
3The Hybrid Most Large Feeds Use
The way out is to treat the two kinds of authors differently. For ordinary accounts, below a threshold of say 100,000 followers, we fan out on write. For the few very popular accounts, we do not fan out at all; we keep their recent posts in a small per-author list. When a user reads their feed, we take their precomputed sorted set and merge in the latest posts of the popular accounts they follow, which is usually only a handful of extra lookups that are themselves cached heavily, since millions of readers ask for the same celebrity's posts.
We also keep feed lists bounded, for example the latest 800 ids per user with ZREMRANGEBYRANK after each insert, and we skip fan-out to users who have not been active for 30 days; if they come back, we rebuild their feed on read once.
4Doing the Fan-Out Asynchronously With Kafka
The post request should not wait for hundreds of feed insertions. The Java post service stores the post, then publishes a PostCreated event to Kafka (through an outbox table, so the event is never lost if the service crashes after the commit). Fan-out workers, ordinary Spring Boot consumers, read the event, page through the author's followers and write the post id into each follower's sorted set in batches.
Notice that the worker is idempotent: adding the same post id to a sorted set twice leaves one entry, so redelivered Kafka messages do no harm. Partitioning the topic by author id keeps one author's posts in order, and adding consumer instances scales the fan-out throughput.
5Store Ids in Feeds, Content Elsewhere
The feed lists hold only post ids and timestamps, not the post text, images or like counts. A post can appear in millions of feeds, and an edit or a deletion would otherwise mean updating every copy. Instead, the read path takes the 50 ids from the feed, fetches the posts in one batch from a post cache (Redis or Caffeine in front of the post database) and drops any that were deleted or that the reader may no longer see. Counts such as likes change constantly and are fetched from their own store.
6Delivering New Posts to Connected Clients
Precomputed feeds make the next refresh fast, but the question asks for new posts to appear within seconds while the app is open. For that, clients hold a WebSocket or server-sent events connection to a gateway, built with Spring WebFlux or Spring MVC. When a fan-out worker inserts a post into a follower's feed, it also publishes a small message, new item for user 123, to Redis pub/sub or a Kafka topic. The gateway node holding user 123's connection receives it and pushes it down. Because a user is connected to only one node, we need that broker step, or a registry of which node holds which user, to reach the right node. Many products push only a hint, such as 3 new posts, and let the client fetch them through the normal read path, which keeps one code path for permissions and content.
- Post service stores the postand writes
PostCreatedto the outbox - Kafka topic post-createdpartitioned by author id
- Author has over 100k followers?YesSkip fan-out; merged at read timeNoFan-out workers run
- Workers add the post id to each follower's sorted setin batches of 1,000
- Publish 'new item' for online followersRedis pub/sub or Kafka
- Gateway node holding the connection pushes itWebSocket or SSE
- Client fetches post content by idfrom the post cache
7 steps in order. Play them, or step through them one at a time.
7Follow-up questions
How do you handle a user who follows a celebrity and also needs the post within seconds?
The celebrity's post is not fanned out to feeds, but it can still trigger a push. We publish one event to a topic for that author, and gateway nodes forward it to the online followers they hold, or we simply let clients that follow celebrities poll the small celebrity list every few seconds. The reader's next feed request merges the post in anyway.
What happens when someone unfollows or a post is deleted?
For deletion, the read path drops ids whose post no longer exists or is hidden, so we do not need to clean every feed immediately; a background job can remove them later. For unfollow, we filter the author out at read time and optionally remove their ids from that one feed, which is a single cheap operation.
Why Redis sorted sets rather than a database table for feeds?
Feed reads are the hottest path and need a range of newest items in under a few milliseconds. A sorted set gives exactly that with ZREVRANGE and keeps itself ordered on insert. Feeds can be rebuilt from the posts and the follow graph, so losing a Redis node is an inconvenience rather than data loss, which lets us treat it as a cache.
8Conclusion
In this answer, we compared fan-out on write, which makes reads cheap and writes expensive for popular authors, with fan-out on read, which does the opposite. We designed a hybrid that pushes ordinary authors' post ids into Redis sorted sets through Kafka workers and merges popular authors' posts at read time, kept content and counts out of the feed lists, and delivered new items to online clients through WebSocket or SSE gateways with a broker to reach the right node.
Open with
I would start with the trade-off. Fan-out on write puts each new post id into every follower's feed at posting time, for example in a Redis sorted set scored by time, so reading a feed is one fast range query.
Then, if they want more
The cost is on writes, and it explodes for accounts with millions of followers. Fan-out on read stores the post once and assembles the feed when someone opens it, which is cheap to write but slow to read, and reads are about a hundred times more frequent.
So I would use a hybrid: fan-out on write for ordinary accounts and, for accounts above a threshold like a hundred thousand followers, merge their recent posts in at read time.
The post service writes the post and publishes an event to Kafka through an outbox, and idempotent fan-out workers insert ids in batches and trim each feed to the newest few hundred. Feeds hold only ids; the read path fetches post content in one batch from a cache, so edits and deletes do not touch millions of copies.
For real-time delivery, clients keep a WebSocket or SSE connection to gateway nodes built with WebFlux, and the workers publish a small new-item message through Redis pub/sub or Kafka so the node holding that user's connection can push it.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Design a real-time activity feed for a social application in Java: users follow others...": https://scrivio-interview-library.netlify.app/architecture/backend-system-design/3bc3714599/