Social feed
What we are building
Section titled “What we are building”A social feed is the page you see when you open the app: the newest posts of the accounts you follow. The service has three operations that matter. Write a post. Follow or unfollow an account. Open the feed. The first two are rare; the third is what the product is.
Functional requirements. Publish a post. Follow and unfollow accounts. Open a home feed of the newest posts of the accounts you follow, newest first, and page backwards through it. Ranking by relevance, media, replies, likes, search and notifications are real features and are left out, so that the lesson stays on how a feed is assembled.
Non-functional requirements. These are targets we choose, not figures from a real service. Opening a feed answers in under 200 ms at the 99th percentile. The feed is available 99.9% of the time. A new post appears in followers’ feeds within a few seconds, not instantly. A post that was accepted is never lost.
Scale. 20 million daily active users, each opening the feed 10 times a day, and 2 million new posts a day. Most accounts have about 200 followers, and a few have more than a million.
Mental model. A feed is a join between “who I follow” and “what they posted”, and it is read about a hundred times for every post written. The design question is only where to do the join: every time someone reads, or once when someone writes.
Estimate before you design
Section titled “Estimate before you design”The requests per second lesson gives the method. The assumptions are in the table, so that you can change them and see what moves.
| Quantity | Working | Result |
|---|---|---|
| Feed reads per day | 20 million users × 10 opens | 200 million |
| Feed reads per second, on average | 200 million ÷ 86,400 | about 2,300 |
| Posts per day | 1 in 10 users posts once | 2 million |
| Posts per second, on average | 2 million ÷ 86,400 | about 23 |
| Peak, assuming 3 times the average | 2,300 × 3, and 23 × 3 | about 7,000 reads and 70 posts per second |
| Fan-out writes per post, ordinary account | about 200 followers | 200 |
| Fan-out writes per post, averaged over all posts | 999 posts of 200, plus 1 post of 1,000,000, over 1,000 posts | about 1,200 |
| Timeline memory | 20 million users × 500 post identifiers × 8 bytes | 80 GB |
| Post storage | 2 million posts × about 1 KB | about 2 GB a day, 730 GB a year |
| Traffic out | 7,000 feeds a second × 20 posts × about 1 KB | about 140 MB/s, which is 1.1 Gbit/s |
The 3 for the peak, the 200 followers, the 1 post in 1,000 from an account with a million followers, the 500 identifiers and the 1 KB are assumptions, and so is the 100 to 1 ratio of reads to posts: they are teaching figures, not measurements. Three things follow whatever the exact values. Storage is modest: 730 GB a year is small (see the storage lesson). The read rate is large but ordinary. The number that deserves attention is the fan-out: if every post is copied to every follower, the 70 posts a second become about 84,000 writes a second, because the average post reaches 1,200 followers. The reason for 1,200 and not 200 is the single post in a thousand that reaches a million: it supplies 83% of all the copies.
The API and the data
Section titled “The API and the data”Three calls carry the traffic.
POST /posts {"text": "Hello"}201 Created {"postId": "1912…", "createdAt": "2026-10-11T09:00:00Z"}
GET /feed?limit=20&before=1912…200 OK {"posts": [ … 20 posts … ], "next": "1688…"}
PUT /follows/{accountId} DELETE /follows/{accountId}The feed is paged by the identifier of the last post seen, not by an offset: a new post arriving at the top would shift every offset and repeat posts. That works when post identifiers increase with time, which is why they are time-ordered.
| Data | Shape | Notes |
|---|---|---|
posts |
post_id, author_id, body, created_at |
The source of truth, read by post_id. |
follows |
follower_id, followee_id, indexed both ways |
“Who does X follow” is used to read; “who follows X” is used to fan out. |
accounts |
account_id, follower_count |
The count decides whether an account is pushed or pulled (see below). |
| timeline | one sorted set per user, timeline:{userId}, members are post_id |
A Redis sorted set keeps members unique and ordered by score, so adding the same post twice changes nothing, and the set is trimmed to the newest 500. |
A timeline is a copy of data that already exists in posts and follows. In the vocabulary of the normalisation lesson, it is denormalised, or a materialised view that the application keeps up to date: you pay at write time to make reads cheap, and you accept that the copy can be a little behind.
Five stages, each forced by a number
Section titled “Five stages, each forced by a number”The lab starts with the simplest design that works and adds one thing at a time. Each stage is designed for a load, and the slider raises the load to 3 times and 10 times that, which shows where the stage runs out. Every figure is model output with illustrative inputs, and the model notes under the lab list what it leaves out.
A service where people follow accounts and open a home feed of their newest posts. For every post written about a hundred feeds are read, and a few accounts have a million followers. Step through five stages, each adding the one thing the previous stage’s numbers called for, and raise the load to see what breaks.
Start simple: build the feed when it is opened
Design load of this stage: 400 req/s
- What we add
- One database holds the posts and the follows. To open a feed, the API servers ask it for the newest posts of the roughly 200 accounts the user follows and merge them (fan-out on read). A new post is a single row.
- Why: the number that forced it
- At 400 requests per second, 396 feed reads and 4 new posts, a database that answers a merge query in about 10 ms, 8 at a time, is at 50% of its 800 queries per second. Nothing else is close to its limit.
- What it costs
- Cheap and simple: posting is trivial and a feed is always fresh. The price is that every read gathers and merges the posts of 200 accounts, and reads outnumber posts 100 to 1.
- What breaks next
- At 3 times the load, 1,200 requests per second, the feed reads ask the database for 1,200 merges against its 800 per second: it is at 150% and a third of the requests fail.
Each dot stands for many requests. The numbers show the real rate.
No bottleneck: every component has headroom.
Figures at this load
- Busiest component
- Database (posts and follows)Busy50% of its capacity
- p95, whole request
- 31 ms
- p99, whole request
- 46 ms
- Requests served
- 100%400 req/s
- Requests failed
- 0%0 req/s
- Bottleneck
- none
| Component | State | Utilisation | Offered (req/s) | Served (req/s) | Rejected (req/s) | p50 | p95 | p99 |
|---|---|---|---|---|---|---|---|---|
| Browsers and apps | Healthy | <1% | 400 | 400 | 0 | 0 ms | 0 ms | 0 ms |
| API servers | Healthy | <1% | 400 | 400 | 0 | 0.52 ms | 2.2 ms | 3.5 ms |
| Database (posts and follows) | Busy | 50% | 400 | 400 | 0 | 7.1 ms | 30 ms | 46 ms |
- Traffic
- 400 req/s
- Successful
- 400 req/s
- Error rate
- 0%
- Mean latency
- 11 ms
All stages at a glance
Each cell is the engine’s steady state for that stage at that load: the busiest component, how much of its capacity it uses, and the share of requests that fail.
| Stage | Design load | Design load | 3× the design load | 10× the design load |
|---|---|---|---|---|
| 1. Start simple: build the feed when it is opened | 400 req/s | 400 req/sDatabase (posts and follows): 50%none fail | 1.2K req/sDatabase (posts and follows): 150%33% fail | 4K req/sDatabase (posts and follows): 500%80% fail |
| 2. Do the work at write time: a timeline for every user | 7K req/s | 7K req/sTimeline store: 57%none fail | 21K req/sTimeline store: 170%41% fail | 70K req/sTimeline store: 443%77% fail |
| 3. Do not push celebrities: merge their posts at read time | 7K req/s | 7K req/sTimeline store: 13%none fail | 21K req/sTimeline store: 39%none fail | 70K req/sTimeline store: 131%23% fail |
| 4. Split the timeline store over more nodes | 7K req/s | 7K req/sCelebrity posts store: 9%none fail | 21K req/sCelebrity posts store: 26%none fail | 70K req/sCelebrity posts store: 87%none fail |
| 5. Cache the celebrity posts in each API server | 7K req/s | 7K req/sAPI servers: 8%none fail | 21K req/sAPI servers: 25%none fail | 70K req/sAPI servers: 82%none fail |
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- Requests arrive at random, as a Poisson process, at a constant average rate.
- Each node is one queue, served first-come first-served by several identical slots (instances × concurrency).
- Service times are random with the stated mean; by default they are exponentially distributed.
- Each node is analysed separately, with the traffic it receives treated as Poisson.
- The mix is 100 feed reads for each post written: 99% of the requests open a feed and 1% publish a post. The ratio is a teaching figure, not a measurement of a real service.
- Most posts reach about 200 followers, and 1 post in 1,000 comes from an account with 1,000,000 followers, so a post reaches 1,200 followers on average (999 × 200 + 1,000,000 = 1,199,800 followers over 1,000 posts). The split is an assumption, not a measured distribution.
- Every number (service times, capacities, the 90% local hit ratio, the design loads) is illustrative model output, chosen to make the limit of each stage visible. None of them is a benchmark of a real database, cache or server.
Simplifications
- Above capacity there is no steady state. The model switches to a fluid approximation: the node serves at capacity, its queue grows until it reaches its limit, and further requests are rejected.
- Percentiles assume an exponential tail. A service-time variability other than exponential changes the mean wait but not the shape of the tail.
- A node frees its slot as soon as it has done its own work, before waiting on the services it calls. Real thread-per-request servers hold the slot, so a slow dependency exhausts them faster than this model shows.
- A request calls its dependencies one after another. Parallel fan-out is not modelled.
- When a caller times out and gives up, the callee still finishes the work, so abandoned requests keep consuming capacity.
- Retries add load. Their backoff and jitter decide when that load arrives, which only the per-request simulation shows.
- Retry feedback is solved from the healthy starting point, so it finds one stable outcome. Systems that stay stuck in overload after the cause is gone are not shown.
- A throughput limit (for example maximum writes per second) is modelled as slower service, not as a separate rate limiter.
- A caller’s timeout counts the mean time its dependencies take, not their full distribution.
- Latency percentiles for a whole request are estimated, over every request, including those that are rejected and fail quickly. The mean is exact, and the percentiles come from a gamma distribution that has that mean and the variance of the time spent in each component.
- Each stage is shown in a steady state: the load is held and the engine says where queues build. A burst, and the single post of an account with a million followers, are not modelled; the lesson works that post out by hand.
- The model counts operations on average. A timeline holds post identifiers; fetching the posts themselves, ranking the feed, media and notifications are not modelled.
- A store split over several nodes is modelled as identical instances that share the load evenly. A hot node, rebalancing and replication are not modelled.
- The 90% hit ratio of the local cache is fixed. A real one depends on how long entries stay and on how many celebrities there are.
Parameter values are illustrative. They are not benchmarks of any real product.
Stage 1, build the feed when it is opened. One database holds the posts and the follows. To open a feed it finds the newest posts of the 200 or so accounts you follow and merges them. At 400 requests per second the database is at 50% of its 800 queries per second. Raise the load to 3 times, 1,200 requests per second, and it is at 150%: a third of the requests fail. Every read does the work of a merge, and reads are 99% of the requests.
Stage 2, write every post into every follower’s timeline. A timeline store keeps, for each user, the identifiers of the newest posts, and fan-out workers copy each new post into the timeline of every follower. Opening a feed is now one lookup. The stage is designed for 7,000 requests per second. A post reaches 1,200 followers on average, so the 70 posts per second become 84,000 timeline writes per second. Together with the 6,930 feed reads that is 90,930 operations per second, 57% of the store’s 160,000. Reading with stage 1’s design would have meant 6,930 merges per second against 800. At 3 times the load the store is asked for 272,790 operations per second and is at 170%: 41% of the requests fail.
Stage 3, do not push celebrities. In the stage 2 numbers, the 1 post in 1,000 from an account with a million followers makes 83% of the writes. Stage 3 stops pushing those accounts. Their recent posts go into one small store, and a feed read looks up the timeline and the celebrity store and merges the two. A post now reaches 200 followers: the writes fall from 84,000 to 14,000 per second and the timeline store from 57% to 13% of its capacity. The price is one more lookup per read, 6,930 per second, which is 9% of the celebrity store’s 80,000. At 10 times the load, 70,000 requests per second, the timeline store has to carry 209,300 operations per second against 160,000 and is at 131%: 23% of the requests fail.
Stage 4, split the timeline store. Four nodes instead of 2, each holding the timelines of a share of the users, give 320,000 operations per second, and the store is at 65% at 10 times the load. The next limit is the celebrity store: every feed read asks it, 69,300 lookups per second at 10 times, which is 87% of its 80,000. The API servers are at 82%.
Stage 5, cache the celebrity posts in each API server. Those posts are a small set that everybody reads and that changes rarely, so each API server keeps them in memory for a few seconds. With 90% of the lookups answered there, the celebrity store sees 6,930 lookups per second: 9% instead of 87%. At 10 times the load the API servers are the first to saturate, at 82%, which is the ordinary problem of adding servers.
Fan-out on write, fan-out on read, and the hybrid
Section titled “Fan-out on write, fan-out on read, and the hybrid”The two ends of the design have names. Fan-out on read (a pull) assembles the feed when it is requested. Fan-out on write (a push) assembles it when a post is written, and keeps the result.
| Fan-out on read | Fan-out on write | |
|---|---|---|
| Cost of opening a feed | A merge over about 200 accounts, every time | One lookup |
| Cost of a post | One row | One write per follower: 200 on average, a million for a celebrity |
| Storage | The posts only | Plus an identifier per post per follower: 80 GB |
| Freshness | Always current | As late as the fan-out queue |
| Good when | Reads are rare, or the graph is small | Reads dominate and followers per account are modest |
With reads 100 times more frequent than posts, doing the work at write time pays in total even if each post costs 200 writes. It stops paying for an account where one post costs a million. Take the stage 2 store at its design load. It has 160,000 operations per second and uses 90,930, so it has 69,070 per second to spare, and a million writes need 1,000,000 ÷ 69,070, about 14.5 seconds of everything it has left. During that time the store is busy with one person’s post, and the fan-out of the 70 posts per second behind it waits. In the lab, one fan-out worker slot spends 0.125 ms per write (150 ms for 1,200 writes), so a single worker doing a million serially would take 125 seconds.
The hybrid decides per account. An account above a follower threshold is not pushed. When you open your feed, the service looks up your timeline, looks up the recent posts of the few celebrities you follow, and merges the two by post identifier. The threshold is a tuning choice: with 100,000 followers a post costs 100,000 writes, and in stage 3 the store has 139,070 operations per second to spare, so it takes about 0.7 seconds. Anything above the threshold is read at read time and costs a lookup for everyone instead of a write for each follower.
Two refinements are common and the lab does not model them. Accounts that have not opened the app for weeks need not be pushed to at all: build their timeline on their next visit. And a user who follows thousands of celebrities needs a bounded merge, for example only the celebrities with a recent post.
What happens if the fan-out falls behind
Section titled “What happens if the fan-out falls behind”The fan-out workers read from a queue (see queues and publish/subscribe). A burst of posts, or a slow timeline store, makes the queue grow: consumer lag. Nothing errors. Followers just see an older feed, and the age of the oldest message in the queue is the number to watch. The post itself is already saved, so nothing is lost. The producer needs to save the post and queue the work together, which is the problem of the transactional outbox. Adding the same post to the same timeline twice must be harmless, and with a sorted set it is. One thing users do notice is not seeing their own post, so the service merges the author’s latest posts into their own feed at read time, the same idea as read-your-writes in stale reads.
What happens if the timeline store loses data
Section titled “What happens if the timeline store loses data”The timelines are derived data, so a lost timeline can be rebuilt from follows and posts with the fan-out on read path. The danger is the rebuild itself. If the whole store is lost, every feed read is a rebuild: 6,930 merges per second against the 800 that stage 1’s database can do, which is 866%. This is the cascade of when the cache fails, and the cures are the same: rebuild only the timelines that are asked for, limit how many rebuilds run at once, show a shorter or slightly older feed while it happens, and keep replicas of the timeline store so a lost node is not a lost store.
What happens if an account crosses the threshold
Section titled “What happens if an account crosses the threshold”An account that grows past the threshold stops being pushed. Its older posts are already in many timelines, which is harmless: the merge de-duplicates by post identifier. An account that drops below the threshold is the awkward case, because its recent posts were never pushed. The simple answer is to leave it on the pull path until it has been below the threshold for a while. A deleted post is a second case: timelines hold identifiers, and the posts are fetched when the feed is built, so a deleted post is filtered out at that point instead of being removed from a million timelines.
Trade-offs
Section titled “Trade-offs”Each row is a decision in the lab, written with what it buys, what it costs and when the other choice is better.
| Decision | What you gain | What you pay | When to choose otherwise |
|---|---|---|---|
| Write timelines in advance (push) | A feed open is one lookup: 57% of the store at design load instead of 6,930 merges per second | 84,000 writes per second, 80 GB, and a feed that lags | When reads are not far more frequent than posts, or the store cannot hold a copy per follower |
| Skip the push for accounts above a threshold | 83% fewer writes, and no million-write post | Two paths to keep correct, a tuning knob, one more lookup per read | When no account is much larger than the rest: pure push is simpler. When the product is small, pure pull is simpler still |
| Split the timeline store over nodes | 320,000 operations per second instead of 160,000 | A node with busy users runs hotter, and adding nodes moves data | When one node carries the load with room to spare |
| Cache celebrity posts in each server | The celebrity store drops from 87% to 9% | A few seconds of staleness, memory on every server | When a new or deleted post must show up at once |
Defend your design
Section titled “Defend your design”An interviewer asks for this system in 45 minutes. A good answer starts with the numbers (reads dominate), says the two ways to assemble a feed, picks one on those numbers, and then finds its weak point before the interviewer does. Write your answer before you read the strong one.
Interview question
Writing and reviewing an answer needs JavaScript.
The interviewer asks
Design the home feed of a social network. Users follow accounts and open a feed of their newest posts. How do you build the feed, and what do you do about accounts with millions of followers?
Assume 20 million daily active users who each open their feed 10 times a day, and 2 million new posts a day. Most accounts have about 200 followers; a few have more than a million.
Write your answer first. The strong answer unlocks once you have written a real attempt.
0 of 60 characters written to unlock the strong answer
- 20 million users opening the feed 10 times a day make about 2,300 reads per second on average, 7,000 at the peak, against about 70 posts per second: reads outnumber posts 100 to 1.
- Building the feed on read is simple and fresh, and it puts a merge on 99% of the requests: the stage 1 database is at 50% at 400 requests per second and at 150% at 1,200.
- Writing each post into each follower’s timeline moves the work to the 1% of requests that write. With 1,200 followers on average that is 84,000 writes per second, 57% of a 160,000 store.
- One post in 1,000 from a million-follower account makes 83% of those writes. Not pushing those accounts cuts the writes to 14,000 per second and puts 6,930 lookups per second on a small celebrity store.
- A lagging fan-out gives an older feed, not errors. A lost timeline store turns every read into a merge: 866% of what the stage 1 database can do.
Next: E-commerce, with a catalog, carts, stock reservations and a checkout that spans services.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.