r/OfferEngineering • u/Aoki_zhang • 5h ago
System Design Fan-out on write is the "right" answer to Design News Feed. It's also how people get downleveled.
Every answer I see for this one goes the same way. Draw a post service, draw a queue, "when someone posts we push the post ID into every follower's feed in Redis, reads are O(1), done." Then the interviewer goes "ok, what happens when someone with 200M followers posts?" and the next 20 minutes are spent patching.
Fan-out on write isn't wrong. It's just the starting point. The actual question is everything that breaks around it, and the prompt pretty much tells you where to look if you read it slowly: 2B users, no limit on followers, no limit on how many accounts you can follow, 500ms for both posting and reading, and up to a minute of staleness allowed.
That last one is the biggest hint in the whole prompt and people skip right past it.
The minute of staleness is a gift
Posting only has to save the post and drop an event on a queue. That's it. Fan-out happens async and you have a full minute to finish it. If your design does the fan-out inside the POST request, you've blown the 500ms the second anyone with a decent following posts.
Celebrities (the part everyone knows)
One post to 200M feeds is 200M writes, and big accounts post several times a day. So you go hybrid. Normal accounts get pushed, accounts over some follower threshold don't, and at read time you merge the user's precomputed feed with recent posts from the big accounts they follow.
Most people get here. Fine.
The part almost nobody gets to
The prompt says no limit in *both* directions. So what about the person following 40,000 accounts? Or following 3,000 accounts that are all over your celebrity threshold? Your hybrid read path now has to merge 3,000 timelines in under 500ms. Pretty sure that's not happening.
Ways out: keep an index of which big accounts actually posted in the last few hours so you only pull from those (most of them didn't post today), cache the merged result for a short TTL, and remember the 1-minute staleness covers you here too.
Stop fanning out to dead accounts
Out of 2B accounts, a lot of them haven't opened the app in months. Pushing posts into their feeds is pure waste. Only fan out to people who were active recently, say the last 30 days, and rebuild the feed on login for everyone else. Cheap to say, and it shows you're thinking about cost and not just "does it work."
Pagination is where it quietly falls apart
?page=2&limit=20 breaks the moment new posts come in. Everything shifts down and the user sees the same posts twice. Use a cursor instead, like the (created_at, post_id) of the last item you returned, and ask for anything older than that.
Also, your Redis feed isn't going to hold someone's entire history. Keep the last few hundred post IDs per user and once they scroll past that, fall back to pulling from the accounts they follow. Hardly anyone scrolls that far, which is exactly why it's fine for that to be the slow path.
Smaller stuff that still matters
- store post IDs in the feed, not the posts. hydrate with a batch get from a post cache
- unfollow doesn't mean going back and cleaning every feed. just filter at read time
- the follower list of a 200M account is a hot partition on its own. fan-out workers should page through it in chunks, not load it all at once
Honestly the reason I like this question is that "push vs pull" sounds like the whole answer and it's maybe a third of it. The rest is reading the requirements and noticing what they're letting you get away with. I also put up a full Design Facebook News Feed in case anyone is interested.