Fan-out Problem

Difficulty: Advanced

Question

What is the fan-out problem in database and system design, and how would you design a news feed so that a celebrity post does not overload the system?

Answer

The fan-out problem appears when one small write has to be turned into a huge number of reads or writes, such as one celebrity post that must show up in the feeds of 50 million followers. The design question is where you pay that cost: at write time (push every post into every follower's feed) or at read time (pull posts from everyone a user follows when they open the app). Real systems use a hybrid of both.

Fan-out on write (push model)

When a user posts, a background worker looks up their followers and inserts the post ID into each follower's precomputed timeline, usually a capped list in Redis or a wide-column store like Cassandra.

  • Reads are very cheap: opening the feed is a single key lookup, already sorted.
  • Writes are expensive and proportional to follower count. A user with 200 followers costs 200 inserts; a cricketer with 50 million followers costs 50 million.
  • Work is wasted on inactive followers who will never open the app.
  • Delivery is delayed: the last follower in the queue may see the post minutes later.

Fan-out on read (pull model)

Nothing is precomputed. When a user opens the feed, the service fetches recent posts from every account they follow and merges them by time or rank.

  • Writes are cheap: a post is stored once.
  • Reads are expensive: following 1,000 accounts means 1,000 lookups (or a scatter-gather query) and an in-memory merge on every feed load.
  • Latency is harder to keep low under heavy read traffic, which is what feeds have.
AspectFan-out on writeFan-out on read
Cost paid atPost timeFeed load time
Feed read latencyVery lowHigher, grows with follows
Celebrity postMillions of writesOne write
Wasted workInactive followersRarely-read accounts

The hybrid approach most feeds use

Twitter's well known timeline design, and most feed systems since, combine the two:

  1. For normal users, fan out on write into followers' cached timelines.
  2. For accounts above a follower threshold (celebrities, brand pages), skip fan-out entirely.
  3. At read time, load the precomputed timeline and merge in recent posts from the few celebrities the user follows.
  4. Only maintain precomputed timelines for users active in the last N days; rebuild on demand for returning users.

This caps the worst-case write amplification while keeping reads close to a single lookup.

Fan-out beyond social feeds

The same shape shows up elsewhere: sending a push notification to every Swiggy user in a city, invalidating cache entries for every product in a category, or updating a denormalized counter on many rows. Whenever one event touches a large, unbounded set of rows, ask:

  • Can the work be done asynchronously through a queue (Kafka, SQS) with rate limiting?
  • Can it be deferred to read time and computed lazily?
  • Is there a hot key (one celebrity) that needs special handling?

Common mistakes

  • Doing fan-out synchronously inside the "create post" request, so a big account's post times out.
  • Forgetting deletes and edits: a deleted post must also be removed or filtered from millions of timelines, which is usually done by filtering at read time.
  • Storing full post bodies in every timeline instead of just post IDs, multiplying storage.
Hybrid feed fan-out. Normal posts are pushed into follower timelines; celebrity posts are pulled and merged when the feed is read.
  1. User publishes a post: Post stored once in the posts table.
  2. Check follower count: Above threshold means skip push fan-out.
  3. Queue fan-out workers: Insert post ID into each follower's Redis list.
  4. Reader opens feed: Load precomputed timeline in one lookup.
  5. Merge celebrity posts: Pull recent posts from followed celebrities and rank.

Code examples

Fan-out on write worker with a celebrity cutoff

CELEBRITY_THRESHOLD = 100_000
TIMELINE_LEN = 800

def on_post_created(post_id, author_id, redis, db):
    followers_count = db.get_follower_count(author_id)
    if followers_count > CELEBRITY_THRESHOLD:
        return  # pulled at read time instead

    # stream followers in pages to avoid loading millions into memory
    for batch in db.iter_follower_ids(author_id, page_size=1000):
        pipe = redis.pipeline()
        for fid in batch:
            key = f"timeline:{fid}"
            pipe.lpush(key, post_id)
            pipe.ltrim(key, 0, TIMELINE_LEN - 1)  # keep it capped
        pipe.execute()

def read_feed(user_id, redis, db):
    ids = redis.lrange(f"timeline:{user_id}", 0, 49)
    celeb_ids = db.followed_celebrities(user_id)
    celeb_posts = db.recent_posts_by(celeb_ids, limit=50)
    return merge_by_time(db.get_posts(ids), celeb_posts)[:50]

Normal authors are pushed into capped Redis lists using pipelined writes. Celebrity posts are skipped at write time and merged in when the feed is read.

Key points

  • Fan-out on write makes reads cheap and writes proportional to follower count.
  • Fan-out on read makes writes cheap and reads proportional to the number of accounts followed.
  • The hybrid model pushes for normal users and pulls for celebrities above a threshold.
  • Store post IDs in timelines, not full post bodies.
  • Run fan-out asynchronously through a queue, never inside the user's request.

Common follow-up questions

  • How would you handle a deleted or edited post that has already been fanned out?
  • How do you rank a feed instead of sorting it purely by time?
  • What happens to the timeline of a user who has been inactive for six months?
  • How would you shard the timeline store as the user base grows?

Interview tips

  • Name both models and the trade-off in one line before drawing anything; then introduce the hybrid as the answer to the celebrity case.
  • Do quick back-of-envelope math (followers times posts per day) to show why pure push breaks for big accounts.
  • Mention that only active users need precomputed timelines; it shows you think about wasted work.

Concepts covered

Fan-out on Write, Fan-out on Read, Hybrid Timeline, Celebrity Problem, Feed Generation

More Database Design questions