Sub topic 1
Clarifying requirements and estimating load
Prerequisites
This chapter assumes you have:
- A working idea of client-server architecture and HTTP APIs (request, response, status codes).
- Some familiarity with relational databases and key-value stores, and why you would pick one over the other.
- A basic idea of caching and message queues.
- Comfort reading short Python and SQL snippets.
You do not need to know anything about feed design yet. This chapter is the starting point for the rest of the tutorial.
Why start with requirements
"Design a news feed" is a vague request. A feed for a 10,000-user company intranet can be one SQL query. A feed for a 500-million-user social network needs sharded caches and a hybrid fan-out strategy. The difference lies in the requirements and the numbers behind them, not in the idea of a feed.
This chapter has two jobs. The first is to turn a vague prompt into a short list of agreed behaviors and limits. The second is to turn that list into rough numbers: requests per second, writes per second, and bytes stored. Later chapters use these numbers to justify decisions. For example, whether to precompute timelines depends on the ratio of reads to writes. How large a timeline cache needs to be depends on the per-user entry size.
Functional requirements
Functional requirements say what the system does. For a news feed, a reasonable core set is:
- A user can publish a post (text, with optional media references).
- A user can follow and unfollow other users.
- A user can fetch their feed: a list of recent posts from the accounts they follow.
- The feed can be paged, so the client can scroll back through older posts.
Then confirm what is out of scope. Without agreement on this, the design grows without limit. Typical exclusions are media upload and transcoding, push notifications, search, ads, and comments. Say them out loud and write them down. You can add them later if the person who set the task asks.
Two questions change the design most:
- Is the feed chronological or ranked? A chronological feed sorts by time. A ranked feed scores each candidate post. This tutorial builds the chronological candidate feed first and adds ranking in a later chapter. Ranking changes which stage does the sorting, so decide it early.
- Who sees a post? If every post is public, access checks are simple. If there are private accounts, blocks, or close-friends lists, every read path needs a visibility check. The final chapter covers this. Note it now so the data model leaves room for it.
Non-functional requirements
Non-functional requirements set the limits. Each one should become a number you can test.
| Requirement | Example target | Why it matters |
|---|---|---|
| Feed read latency | p99 under 300 ms | Drives the choice between computing at read time and precomputing |
| Post visibility delay | Most followers see a new post within 5 seconds | Sets how much asynchronous work is acceptable |
| Availability | 99.95% for reads | Reads matter more than writes; a stale feed beats an error page |
| Consistency | Eventual for the feed, strong for follow and block changes | Different parts of the system can use different guarantees |
| Retention | Feed shows the last 800 items; posts kept for years | Bounds the cache size, but not the post store |
The p99 latency (the time that 99% of requests beat) is the number to quote, not the average. An average hides the slow tail that users notice.
The consistency row deserves attention. A feed that is a few seconds stale is usually acceptable. A blocked user who can still see your posts is not. Treat the two as separate problems from the start.
Questions to ask
In an interview or a design review, a short list of questions fills most of the gaps:
- How many monthly and daily active users are there?
- How often does a typical user open the feed, and how many posts does one request return?
- How many posts does a typical user publish per day?
- How many accounts does a user follow, and how many followers does an account have? What is the maximum?
- Is the feed ranked or chronological? Must it update live or only on refresh?
- Which regions must be served?
The follower question has the biggest consequence, because the average hides a heavy tail. A network might average 200 followers per account while a few accounts have 50 million. The design must survive the tail, not just the average. Chapter 5 covers this.
Estimating load
An estimate does not need to be exact. It needs to be right to within a factor of two or three, so you can tell whether a design needs one database or a hundred machines. Use round numbers, write down every assumption, and keep the arithmetic visible.
Assumptions for the running example
This tutorial uses these numbers throughout:
- 200 million daily active users (DAU).
- Each active user opens the feed 10 times per day.
- Each DAU publishes 0.5 posts per day on average, so 100 million posts per day.
- An average account has 200 followers.
- Peak traffic is 3 times the daily average.
- A post's metadata takes about 1 KB.
- A timeline entry stores a timestamp and a post ID, 16 bytes in total.
- Each user's cached timeline holds 800 entries.
A day has 86,400 seconds. Dividing by that figure turns daily counts into per-second rates.
Read and write rates
Feed reads per day: 200 million users times 10 opens is 2 billion. Divided by 86,400 that is about 23,000 requests per second on average, and about 69,000 at peak.
Posts per day: 100 million, or about 1,157 per second on average and about 3,500 at peak.
At the API level, reads outnumber writes about 20 to 1. This is the first number people reach for when justifying a precomputed feed: reads are far more frequent than writes, so do the work at write time.
The fan-out multiplier
The 20 to 1 ratio is not the whole story. If you push each new post into every follower's timeline, one post becomes 200 internal writes. That makes 100 million posts times 200 followers, or 20 billion timeline writes per day. That is about 231,000 per second on average and about 694,000 at peak.
Compare that to 2 billion reads per day. Once you count fan-out, internal writes are about 10 times more frequent than reads. Read-heavy at the API does not mean read-heavy inside the system. This is the central tension of feed design, and chapters 3 and 4 compare the two strategies in detail.
The worst case is much larger than the average. One account with 50 million followers publishing a single post creates 50 million timeline writes. At 100,000 writes per second of spare capacity, that takes 500 seconds, which is over eight minutes. This breaks the 5-second visibility target for those followers, and the writes compete with everyone else's traffic.
Storage
Post metadata: 100 million posts times 1 KB is 100 GB per day, or about 36.5 TB per year. This is easily handled by a sharded database. Media is stored separately and is out of scope.
Timeline cache: 200 million users times 800 entries times 16 bytes is about 2.56 TB. This is large for one machine and small for a cluster. Once you add the key and structure overhead of a real cache such as Redis, plan for two to three times that. Because the timeline stores only references, not full posts, the cache stays small. If it stored full 1 KB posts, it would be about 160 TB.
A small estimator
Keeping the arithmetic in code makes the assumptions explicit and easy to change. Change one input and every output updates.
SECONDS_PER_DAY = 86_400
assumptions = {
"dau": 200_000_000,
"feed_opens_per_user": 10,
"posts_per_user": 0.5,
"avg_followers": 200,
"peak_factor": 3,
"post_bytes": 1_000,
"entry_bytes": 16,
"feed_limit": 800,
}
def human_bytes(n):
for unit in ("B", "KB", "MB", "GB", "TB", "PB"):
if n < 1000:
return f"{n:,.1f} {unit}"
n /= 1000
return f"{n:,.1f} EB"
def estimate(a):
reads_day = a["dau"] * a["feed_opens_per_user"]
posts_day = a["dau"] * a["posts_per_user"]
fanout_day = posts_day * a["avg_followers"]
rates = {
"feed reads": reads_day,
"posts": posts_day,
"fan-out writes": fanout_day,
}
for name, per_day in rates.items():
avg = per_day / SECONDS_PER_DAY
peak = avg * a["peak_factor"]
print(f"{name:>15}: {avg:>10,.0f}/s avg {peak:>10,.0f}/s peak")
post_store_day = posts_day * a["post_bytes"]
timelines = a["dau"] * a["feed_limit"] * a["entry_bytes"]
print(f"{'post storage':>15}: {human_bytes(post_store_day)}/day, "
f"{human_bytes(post_store_day * 365)}/year")
print(f"{'timeline cache':>15}: {human_bytes(timelines)}")
estimate(assumptions)Running it prints:
feed reads: 23,148/s avg 69,444/s peak
posts: 1,157/s avg 3,472/s peak
fan-out writes: 231,481/s avg 694,444/s peak
post storage: 100.0 GB/day, 36.5 TB/year
timeline cache: 2.6 TBTry changing avg_followers to 20 or 2,000 and see which numbers move. Fan-out writes scale linearly with it, while reads and post storage do not move at all.
From numbers to decisions
The estimates are only useful if they change the design. This table shows how each number links to a later chapter.
| Number | What it tells you | Decision it informs |
|---|---|---|
| 23k reads/s average, 69k peak | A single database cannot serve every read by joining posts and follows | Fan-out and caching strategy |
| 231k fan-out writes/s | Pushing every post is expensive, and the cost scales with followers | Push versus pull, and the hybrid approach |
| 50 million followers on one account | The tail can break the average-case design | Special handling for high-follower accounts |
| 2.6 TB timeline cache | Fits in a cluster of in-memory nodes, not on one | Sharding and bounded timelines |
| 36.5 TB per year of posts | Needs horizontal partitioning of the post store | Data model and sharding key |
Common errors
- Using the average for everything. The mean follower count of 200 says nothing about the maximum. Always estimate the tail separately, and ask what the largest account looks like.
- Forgetting peak. Capacity must cover peak load, not the daily average. A factor of 2 to 5 is typical. Events such as sports finals or elections can exceed it for specific accounts.
- Ignoring fan-out in the write rate. A write rate of 1,157 posts per second sounds easy. The 231,000 timeline writes per second behind it do not.
- Mixing up bytes and entries. Storing a post ID and timestamp is cheap. Storing the full post in every follower's timeline multiplies storage by the follower count.
- Skipping units. Write "/s" or "/day" on every number. Most arithmetic slips in estimates are unit slips.
- Over-precision. Reporting 23,148 requests per second implies accuracy you do not have. In a design review, say "about 23k".
What carries forward
The next chapter models users, posts, and follow edges. The requirements and numbers from this chapter set the main constraints on that model. The post store must handle about 100 million new rows per day. The follow graph must answer "who follows this author?" quickly, because fan-out depends on it. It must also answer "whom does this user follow?", because a read-time feed depends on that. Keep the running example's numbers at hand, since later chapters reuse them.
Exercise
Using the running example (200 million DAU, 10 feed opens per user per day, 100 million posts per day, 200 average followers, peak factor 3), the product team now expects each user to open the feed 30 times a day instead of 10. Which of these estimates change, and what are the new average feed reads per second? Does the fan-out write rate change?
Show answer
Feed reads change; fan-out writes do not. Reads per day are 200 million times 30, which is 6 billion. Divided by 86,400, that is about 69,444 reads per second on average, and about 208,000 at peak with the factor of 3. The posts per day (100 million) and the fan-out writes (20 billion per day, about 231,000 per second) depend only on posts and followers, so they stay the same. Post storage and the timeline cache size also stay the same. The ratio of reads to fan-out writes shifts to about 3 to 1 reads over writes, which makes precomputed timelines even more attractive.
Exercise
An account with 30 million followers posts once. Your fan-out workers can perform 150,000 timeline writes per second in total, and a regular day already uses 100,000 of that capacity at the same moment. How long does it take to deliver this one post to every follower if you push it, and why does this matter for the design?
Show answer
Spare capacity is 150,000 minus 100,000, which is 50,000 writes per second. Delivering 30 million writes at that rate takes 30,000,000 divided by 50,000, which is 600 seconds, or 10 minutes. This is far beyond a 5-second visibility target, and the burst also delays other users' posts that share the same workers. It shows why high-follower accounts need different handling than the average account, which the hybrid approach in a later chapter addresses.