Skip to content
learn
  1. Learn
  2. High Level System Design
  3. Design Case Studies

System Design HLD-19.06

Design a news feed (Twitter/Instagram)

Design a social news feed: fan-out on write vs on read, a hybrid for celebrities, timeline caches, a ranking hook and cursor pagination.

Intermediate 25 min Last reviewed

The problem

Open a social app and you see a feed: recent posts from the accounts you follow, newest or most relevant first, loaded in a fraction of a second and scrolling smoothly into older posts. Behind that screen is one of the most-discussed problems in system design, because it combines a huge read rate, a write path whose cost depends on who is posting, and a social graph where a few accounts have a hundred million followers while most have a few hundred.

The central question is when to do the work of assembling a feed. Do it when someone posts, copying the post into every follower's feed, and reads are instant but one celebrity post becomes fifty million writes. Do it when someone reads, gathering posts from everyone they follow, and posting is free but every feed load becomes hundreds of queries. The answer real systems use is both, chosen per account.

Requirements

Functional

  • Post text with optional media; follow and unfollow accounts.
  • Home feed: posts from followed accounts, newest first, with a hook for ranking.
  • Infinite scroll: fetch the next page without duplicates or gaps.

Non-functional

  • Scale: 100 million daily active users, 0.5 posts per user per day, 10 feed loads per user per day.
  • Latency: feed load p99 under 200 ms.
  • Freshness: a new post appears in followers' feeds within a few seconds; minutes is acceptable for celebrity posts at peak.
  • Availability over consistency: a feed missing a post for a few seconds is fine; a feed that fails to load is not.

Out of scope

Estimates

  • Posts: 100M x 0.5 = 50 million a day, about 580 per second on average, a few thousand at peak.
  • Feed loads: 100M x 10 = 1 billion a day, about 11,600 per second on average and roughly 35,000 at a 3x peak. Reads outnumber posts by 20 to 1, before counting the fan-out.
  • Fan-out: if the average account has 200 followers, pushing every post means 580 x 200 = about 116,000 timeline inserts per second. That average hides the real problem: one post from an account with 50 million followers is 50 million inserts on its own.
  • Timeline cache: keeping the latest 800 post ids (8 bytes each) per user is 6.4 KB per user, or 640 GB for 100 million users, before overhead. A sharded in-memory cluster, so only for active users.
  • Post storage: at about 1 KB of text and metadata per post, 50 GB a day or about 18 TB a year; media lives in object storage.

API

POST /v1/posts                 { "text": "...", "media_ids": [...] }  -> 201 { "post_id": "..." }
POST /v1/follows/{account_id}  -> 204
GET  /v1/feed?limit=20&cursor=eyJpZCI6...
  200 { "posts": [ ...20 hydrated posts... ], "next_cursor": "eyJpZCI6..." }

The cursor encodes the position in the feed (for a chronological feed, the last post id seen), so new posts arriving at the top never shift the next page ( Pagination patterns ).

Data model

  • Posts, keyed by post_id (time-sortable, Design a unique ID generator ): author, text, media references, created_at. A sharded key-value or wide-column store, read by id.
  • Social graph, stored in both directions: followee to followers (fan-out needs it) and follower to followees (pull needs it).
  • Timeline cache, keyed by user_id: a list of the latest ~800 post ids, in memory, sharded by user.
  • Post cache, keyed by post_id: hydrated posts, so a feed page needs one batched round trip.

Timelines store ids, not posts. A post edited or deleted is changed once in the post store and cache, not in fifty million timelines, and an id is 8 bytes instead of a kilobyte.

High-level design

News feed with fan-out on write and celebrity pull on read Write path: an author posts to the post service, which stores the post. Async path: the post service enqueues a fan-out job; fan-out workers read the author's followers from the social graph and insert the post id into each follower's timeline cache, unless the author is a celebrity. Read path: the feed service reads the user's precomputed timeline, pulls recent posts from celebrities the user follows, merges and ranks them, hydrates posts and returns a page. post store enqueue followers push id GET /feed my timeline celebrity posts Author Post service Posts store + post cache Social graph followers Fan-out queue Fan-out workers skip celebrities Timeline cache ids per user Reader Feed service Merge + rank hydrate 20 posts Figure 1. Ordinary posts are pushed into followers' timelines in the background; celebrity posts are stored once and pulled in when a follower reads. Step through the three paths.
  1. Write path The post service validates the post, gives it a time-sortable id and writes it to the posts store and post cache. The author gets a response right away; nothing else happens on the request path.
  2. Async fan-out A fan-out job goes on a queue. Workers page through the author's followers and append the post id to each active follower's timeline list, trimming it to 800 entries. Authors above the celebrity threshold are skipped entirely.
  3. Read path with celebrity pull The feed service reads the user's precomputed timeline, then fetches recent posts from the few celebrities the user follows, merges the two lists, applies ranking, hydrates 20 posts from the post cache and returns them with a cursor.

Deep dives

Deep dive 1: Fan-out on write, on read, or both

Fan-out on write (push). When someone posts, insert the post id into every follower's timeline. A feed read is one list read plus hydration: fast and predictable. The cost moves to writes and grows with follower count. For a celebrity it becomes tens of millions of inserts per post, delaying that post for everyone and starving the fan-out workers. It also wastes work on followers who will not open the app this week.

Fan-out on read (pull). Store each post once. At read time, fetch recent posts from every account the user follows and merge them. Writes are trivial, and inactive users cost nothing. But a user following 500 accounts triggers 500 lookups per feed load, at 35,000 loads per second, and the p99 is set by the slowest of those lookups ( Latency vs throughput, and why percentiles matter ).

Hybrid (chosen). Push for ordinary accounts, which is the vast majority of posts and fans out to a few hundred timelines each. Pull for accounts above a follower threshold, say 100,000: a post from them is stored once, and each follower's feed fetches it at read time. Because a typical user follows only a handful of such accounts, the read-time merge adds a few lookups, not hundreds, and those celebrity posts are among the most cached objects in the system.

Two refinements matter. Skip fan-out for users who have not been active for weeks; build their timeline on demand (by pull) when they return. And treat the threshold as a knob tuned from the cost numbers, not a fixed rule: the calculator below shows how it moves the load.

Deep dive 2: Timeline storage and hydration

Each active user's timeline is a list of post ids in an in-memory store (for example a Redis list or sorted set), sharded by user id, with a cap of about 800 entries: almost nobody scrolls further, and older pages can fall back to pull. Fan-out appends and trims in one command.

A feed page is two steps: read 20 ids (plus a few spare, for posts that were deleted) from the timeline, then hydrateTurning ids into full objects by fetching their data, usually with a batched multi-get from a cache. them with one batched multi-get from the post cache, along with author names and like counts. If the timeline cache loses a shard, those users' timelines are rebuilt by pull on their next request, which is slower but correct, so the cache is an optimization and never the source of truth.

Deep dive 3: Ranking and pagination

A chronological feed sorts by post id. A ranked feed uses the merged list as candidates: take a few hundred recent candidates, score each with a ranking model (relationship strength, engagement, freshness) and return the top 20. Keep ranking as a separate service behind a narrow interface ("score these candidates for this user") so models can change without touching fan-out, and give it a time budget: if it is slow, fall back to chronological order rather than failing the feed ( Load shedding and graceful degradation ).

Pagination depends on the feed type. For chronological feeds, the cursor is the last post id seen, and the next page is "ids older than that", which is stable as new posts arrive. Ranked feeds are not stable, since scores change between requests, so the service stores the ranked list for the session (or the ids already shown) and paginates through that snapshot.

Try it

This interactive needs JavaScript. The diagrams and text above cover the same idea.

Try this:

  1. With the heavy-tailed distribution, compare push and pull. Which one has the biggest single fan-out, and what would that mean for how quickly a celebrity's post reaches everyone?
  2. Move the hybrid threshold from 1K to 100M followers. Watch timeline writes and feed reads trade against each other. Where would you put it?
  3. Switch to the even distribution. Does the hybrid still help much? What does that tell you about when the celebrity problem exists?

Bottlenecks and failure modes

FailureImpactMitigation
Celebrity postMillions of inserts, delayed fan-outPull above a follower threshold
Fan-out backlog at peakPosts appear late in feedsQueue absorbs spikes; autoscale workers; skip inactive users
Timeline cache shard lostSlow feeds for its usersRebuild by pull on next read; replicas
Hot celebrity postPost cache shard overloadedReplicate hot posts, local caches in feed servers
Ranking service slowFeed latency breaks the SLOTime budget, fall back to chronological
Unfollow or deleted postStale ids in timelinesFilter during hydration; lazy cleanup

Trade-offs we made

OptionWins whenLoses when
Hybrid fan-out (chosen) Fast reads for everyone, bounded write cost per post Two code paths and a merge on every read
Pure push Simplest fast reads Celebrity posts cost tens of millions of writes
Pure pull Cheap writes, nothing wasted on inactive users Hundreds of lookups per feed load; poor tail latency
Ids in timelines, posts hydrated (chosen) Small timelines; edits and deletes in one place An extra batched lookup per page
Eventual consistency (chosen) Fan-out off the request path, high availability Posts appear in feeds after a few seconds

Extensions

  • Ranked feeds with candidate generation from beyond the graph (suggested posts), which turns the feed into a recommendation system.
  • Muting and blocking, applied as filters during hydration.
  • Multi-region: timelines built in each region from a replicated post stream.
  • Real-time "new posts" indicator pushed over a long-lived connection ( Real-time: polling, long polling, SSE and WebSockets ).

In the wild

Where you will see this: every social network and many products with an activity stream, such as code hosting platforms, professional networks and team chat "activity" tabs. The push, pull and hybrid vocabulary in this lesson is the standard way engineers describe those systems, and the celebrity problem appears wherever popularity is heavy-tailed, which in social graphs is almost always.

Check yourself

  1. Why is pure fan-out on write a problem for accounts with millions of followers?

    Show answer

    B. One post becomes millions of timeline inserts, delaying it and starving workers Push cost scales with follower count; at the top of a heavy-tailed distribution that is tens of millions of writes per post.

  2. In the hybrid design, what happens when a user follows three celebrities and loads the feed?

    Show answer

    B. The feed service merges the precomputed timeline with recent posts pulled from those three accounts Celebrity posts are pulled at read time; since a user follows few of them, this costs a few lookups.

  3. Why do timelines store post ids rather than full posts?

    Show answer

    B. Small entries, and an edit or delete changes the post in one place Copying full posts into millions of timelines would multiply storage and make edits and deletes expensive.

  4. A user has not opened the app for two months. What should fan-out do?

    Show answer

    B. Skip them, and build their timeline on demand when they return Pushing to inactive users is wasted work; a one-off pull on return is cheaper.

  5. The ranking service times out. What should the feed do?

    Show answer

    B. Fall back to chronological order within its time budget Ranking improves the feed but is not essential; graceful degradation keeps the feed loading.

Recap

  • Reads outnumber posts heavily, so precompute feeds where it is cheap.
  • Push ordinary posts into follower timelines; pull celebrity posts at read time.
  • Store post ids in capped per-user timelines and hydrate pages with a batched lookup.
  • Keep ranking behind a narrow interface with a fallback to chronological order.
  • Paginate with cursors, not offsets, so new posts never shift the next page.

Get new lessons by email

Get new posts on backend and AI internals. No spam, unsubscribe anytime.

Your email is only used to send new posts. Privacy

Next up