AlgoViz

Design a news feed like Facebook's

hard 50 min

Show each person the best recent posts from everyone they follow, in a fifth of a second.

See it run

Every flow of the design, one hop at a time: open the feed, create a post, choose push or pull, rank, rebuild a lost feed and send hints.

Operations

Open the feed (read path)

Take the ready feed, merge big accounts, filter, rank once, and turn 20 IDs into posts.

Step 1 of 11

Ask for the feed

Our systemAppGatewayFeed serviceRedisready feedsBig accountsin memoryRankingPost cacheCassandraposts

request

page 1

The app sends GET /v1/feed. Photos will come from the CDN; this personal reply never does. Why: no two people share it.

Step 1: Ask for the feed

def open_feed(viewer):    ids = redis.newest(f"feed:{viewer}", 500)    ids |= big_accounts.recent(graph.following(viewer))    ids |= posts.recent_by(viewer)            # own posts    top = light_pass(ids, viewer)[:100]    seen = [h for h in posts.headers(top) if may_see(viewer, h)]    order = rank(viewer, seen, limit_ms=60)   # or newest first    sid = sessions.save(viewer, order, ttl=600)    return posts.full(order[:20]), cursor(sid, 20)
Latency
200 ms p99
Load
231,000/s
Peak (3x)
694,000/s
Posts looked up
4.6M/s

Page 2 repeats only the last line and the filter: the order is already stored.

In the real world

The same news feed doing real jobs. Pick one and step through it.

The spike: one post, a hundred million possible readers.

Step 1 of 4

No push

Kafkapost createdFan-out workersFollow graphRedisready feedsBig accountsin memoryFeed service

inserts

0 feeds

The account is far above the limit. One event, one entry in each feed server.

Step 1: No push

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Fan-out before answering

    critical

    Posting waits for hundreds of writes, and a failure in the middle delivers to only some friends.

    Wrong
    for f in followers: redis.add(f, pid)return 201
    Right
    db.store(pid, change=True)return 201                    # workers deliver later
  • Push for every author

    critical

    One post by a star becomes 100 million inserts and everybody else's posts wait.

    Wrong
    push_to_all(author)
    Right
    push(author) if followers <= 10_000 else big_accounts.add(post)
  • Trusting the cached feed for privacy

    critical

    IDs stay behind after blocks, deletions and audience changes.

    Wrong
    return posts.full(redis.feed(viewer))
    Right
    hs = posts.headers(ids)return [h for h in hs if may_see(viewer, h)]
  • Calling a pipeline atomic

    major

    A pipeline saves round trips. Some commands can succeed and others fail, so the batch must be safe to repeat.

    Wrong
    pipe.lpush(feed, pid)    # repeated twice after a retry
    Right
    pipe.zadd(feed, time, pid)  # the same ID again changes nothing
  • More workers than partitions

    major

    In a consumer group one partition has one consumer. The others are idle.

    Wrong
    partitions = 256; workers = 1000
    Right
    partitions = 1024; workers = 694

Interview tips

  • Lead with two products

    • Posts × followers = 2.3M a second.
    • Reads × follows = 116M a second.
    • Fifty times: push.
  • Then break it yourself

    • 100 million followers.
    • Keep one copy, 2 GB for all big accounts.
    • Merge, drop duplicates.
  • Find the hidden reads

    • 20 posts a page: 4.6M lookups a second.
    • A post cache at 99%.
    • Score 100, not 500.
  • Finish with trust

    • Filter at read.
    • Newest first when ranking fails.
    • Never an empty feed.