Script Valley
Interview Prep: System Design Rounds
Designing Real SystemsLesson 5.4

How to design a news feed system like Twitter or Instagram

fanout on write, fanout on read, hybrid fanout, celebrity problem, feed ranking, timeline storage, Redis sorted sets

The Feed Problem

A news feed must show the right posts in the right order to millions of users โ€” with sub-second load times. The core tension is between write cost and read cost.

Fanout on Write

When a user posts, immediately push the post ID to every follower's feed. Reads are instant (pre-built feed). Con: writing to 10M follower accounts per post is expensive and slow.

Fanout on Read

Store posts once. At read time, fetch posts from all accounts the user follows and merge. Simple writes. Con: reading from 1000 followed accounts per feed load is slow.

Hybrid Approach (Production Reality)

# Fanout on write for normal users (<10K followers)
def on_post(post, author):
    if author.follower_count < 10000:
        for follower_id in get_followers(author.id):
            redis.zadd(f'feed:{follower_id}', {post.id: post.timestamp})

# Fanout on read for celebrities (>10K followers)
def get_feed(user_id):
    feed = redis.zrevrange(f'feed:{user_id}', 0, 20)  # pre-built portion
    celebrities = get_followed_celebrities(user_id)
    celebrity_posts = db.get_recent_posts(celebrities, limit=5)
    return merge_and_rank(feed, celebrity_posts)

Feed Storage

Store feed as a Redis sorted set: score = timestamp, member = post_id. Trim to 1000 most recent items per user to bound memory usage.

Up next

How to design a distributed message queue like Kafka

Sign in to track progress

How to design a news feed system like Twitter or Instagram โ€” Designing Real Systems โ€” Interview Prep: System Design Rounds โ€” Script Valley โ€” Script Valley