Capstone: Design a System End-to-End (System Design #18)
You have the parts. Now assemble them under pressure, the way an interview or a real launch does. This capstone walks a single prompt — “Design a social feed” — through the full method from Chapter 1: requirements, estimation, high-level design, deep dives, and trade-offs. Every chapter in this course shows up somewhere. Read it as a worked example, then repeat it on a different prompt yourself.
The point is not the specific design. The point is the motion: clarify, estimate, sketch, go deep where the numbers say, and justify every choice against a requirement.
Step 1 — Requirements
Functional: - Users post short text posts. - Users follow other users. - Users see a home timeline of posts from people they follow, newest-ish first. - Users can like and reply (counts matter more than exactness).
Non-functional: - Read-heavy: far more timeline reads than posts. - Timeline reads must be fast (< 200 ms p99) and highly available. - A post becoming visible a few seconds late is acceptable (feed is eventual), but a user’s own post should appear immediately (read-your-writes). - Global users; latency matters across regions. - Losing a post after “posted” is unacceptable; a like count being briefly off is fine.
The last two lines are the CAP/PACELC decisions (Chapters 3–4) made before any box is drawn: feed reads are AP/EL; post durability is CP/EC.
Step 2 — Estimation
Assume 100M daily users, 200M posts/day, and 100 timeline reads per user per day.
DAU = 100e6
posts_per_day = 200e6
reads_per_user = 100
write_qps = posts_per_day / 86_400 # ~2,315 posts/s avg
read_qps = DAU * reads_per_user / 86_400 # ~115,741 reads/s avg
print(f"write QPS={write_qps:,.0f} peak={write_qps*3:,.0f}")
print(f"read QPS={read_qps:,.0f} peak={read_qps*3:,.0f}")
print(f"read:write = {read_qps/write_qps:.0f}:1")
avg_post_bytes = 300
print(f"post storage/day = {posts_per_day*avg_post_bytes/1e9:.1f} GB/day")
print(f"post storage/3y = {posts_per_day*avg_post_bytes*365*3/1e12:.1f} TB")
Rough results: ~2.3K writes/s (peak ~7K), ~116K reads/s (peak ~350K), a ~50:1 read-to-write ratio, and a few hundred TB of posts over three years. Two conclusions fall out immediately:
- Reads dominate, so the timeline must be served from cache/precomputation, not computed by joining the graph on every request.
- The fan-out is enormous: a user following 500 accounts means each timeline read could touch 500 posts if computed naively. Precomputation (fan-out on write) is on the table.
Step 3 — High-level design
\begin{tikzpicture}[font=\rmfamily, >=stealth,
b/.style={draw, thick, rounded corners=3pt, minimum width=1.65cm, minimum height=0.68cm, align=center, font=\rmfamily\small},
a/.style={->, thick}]
\node[b, fill=gray!10] (c) at (-5.0,0) {client};
\node[b, fill=blue!10] (cdn) at (-3.4,0) {CDN};
\node[b, fill=orange!16] (lb) at (-1.6,0) {LB};
\node[b, fill=green!12] (app) at (0.4,0) {app};
\node[b, fill=cyan!15] (tl) at (2.6,1.2) {timeline cache};
\node[b, fill=yellow!22] (pg) at (2.6,-0.1) {post store (sharded)};
\node[b, fill=red!10] (q) at (2.6,-1.4) {queue $\to$ fan-out};
\draw[a] (c) -- (cdn); \draw[a] (cdn) -- (lb); \draw[a] (lb) -- (app);
\draw[a] (app) -- (tl); \draw[a] (app) -- (pg); \draw[a] (app) -- (q);
\end{tikzpicture}
- Writes: post → app → durable post store (sharded by
user_id) → publish apost.createdevent. - Fan-out workers: consume the event, append the post id to each follower’s precomputed timeline in a timeline store (Redis or a wide-column store).
- Reads:
GET /timelinereads the precomputed list of ids from cache, hydrates the posts (cache-aside), and returns. No graph join on the read path. - Follow graph: stored in a social-graph store, read mostly on write (to know whose timelines to update) and cached.
- Likes/replies: counters in a fast store, updated asynchronously; exactness not required.
Step 4 — Deep dives
Fan-out: write vs read
This is the central design decision.
- Fan-out on write (push). On post, append the id to every follower’s timeline. Reads are then a single fast lookup. Cost: a post by a user with 1M followers triggers 1M timeline writes — a fan-out storm.
- Fan-out on read (pull). Store posts by author; on read, fetch recent posts from everyone the user follows and merge. Writes are cheap, reads are expensive (hundreds of lookups per timeline) — unacceptable at 350K reads/s.
- Hybrid (the real answer). Fan-out on write for ordinary users; for celebrities (above a follower threshold), do not fan out — their followers pull the celebrity’s recent posts at read time and merge them with the precomputed timeline. This tames the hot-user problem while keeping reads fast for the majority.
\begin{tikzpicture}[font=\rmfamily, >=stealth,
b/.style={draw, thick, rounded corners=3pt, minimum width=2.0cm, minimum height=0.7cm, align=center, font=\rmfamily\small},
a/.style={->, thick}]
\node[b, fill=green!12] (post) at (-3.6,0.9) {post by ordinary user};
\node[b, fill=orange!16] (fan) at (-0.8,0.9) {append to followers' timelines};
\node[b, fill=red!12] (celeb) at (-3.6,-0.9) {post by celebrity};
\node[b, fill=blue!10] (pull) at (-0.8,-0.9) {followers pull at read time};
\draw[a] (post) -- (fan); \draw[a] (celeb) -- (pull);
\node[font=\rmfamily\small, text=black!60, anchor=north] at (-2.2,-1.6) {hybrid fan-out handles the hot-user tail};
\end{tikzpicture}
Storage and sharding
- Post store: shard by
author_id(all of an author’s posts on one shard for range reads). High write throughput suits a wide-column store; transactions are not needed. - Timeline store: keyed by
user_id, holding a bounded list (e.g. latest 800 post ids). Redis with persistence, or a wide-column store; bounded size keeps memory predictable. - Hot key: a celebrity’s timeline, or a viral post’s like counter, can saturate one shard. Mitigate with in-process caching, key splitting, and read replicas.
Caching and read-your-writes
- Timeline reads are cache-first (Chapter 12); the timeline is already precomputed, so it is largely a cache lookup.
- Hydrating post bodies uses cache-aside.
- Read-your-writes: a user’s own new post appears before fan-out completes by merging the user’s recent posts into their own timeline on read, and routing their own reads to a fresh source. This is the refinement that prevents “where is my post?” (Chapter 4).
Failure handling
- Fan-out workers are idempotent and retried; the queue level-loads spikes (Chapters 6, 13).
- Circuit breakers protect the app from a struggling fan-out service; a deeper timeline backlog does not take down posting (Chapter 17).
- Bulkheads: posting and reading use separate resource pools, so a read storm does not block writes.
- Degradation: if the timeline store is down, fall back to a slow pull-based read for a subset of users rather than failing entirely.
Step 5 — Trade-offs, stated plainly
- Eventual feed, strong post durability. We accepted a few seconds of feed lag (AP/EL) to keep reads fast and available, but made posting durable (CP/EC) so posts are not lost. (Chapters 3–4.)
- Hybrid fan-out. We chose push for most users to keep reads O(1), and a pull path for celebrities to avoid fan-out storms. Neither pure approach works. (Chapter 11.)
- Precomputation costs write work and storage. We traded cheap reads for more expensive writes and a bounded timeline store. Given a 50:1 read ratio, that is correct.
- Approximate counts. Like/reply counts are eventually consistent because exactness is not worth global coordination. (Chapter 4.)
- What would change the design: if reads became rare and writes dominant, pull would win; if strict chronological order were required globally, fan-out and merge would need a global clock or ordering scheme (Chapters 4, 14).
The repeatable method (use this on any prompt)
- Clarify functional and non-functional requirements; state the consistency stance per data type.
- Estimate QPS, storage, and the read/write ratio; let the numbers pick the hard problems.
- Sketch the five-to-seven box high-level design.
- Deep-dive the two or three components the estimate flagged (usually the database and the fan-out/hot path).
- State trade-offs and what would change them.
Practice prompts (do these yourself)
- Design a URL shortener with abuse protection.
- Design a ride-hailing service (matching, location updates, surge).
- Design a chat app (ordering, presence, multi-device sync).
- Design a rate limiter (distributed counters, fairness).
- Design a news feed — this capstone, redone from scratch.
For each, run the five steps and produce the same artifacts: requirements table, estimation code, high-level diagram, deep-dive on the bottleneck, and a trade-off list.
Self-assessment rubric
| Criterion | Passing looks like |
|---|---|
| Requirements | Functional + non-functional, consistency stated per data type |
| Estimation | QPS, storage, read/write ratio, with peak multiplier |
| High-level | 5–7 boxes, correct flow, no unexplained components |
| Deep dives | The actual bottleneck, with mechanism and numbers |
| Data | Store choices justified; sharding key and replication stated |
| Failure | Retries/idempotency, circuit breaker, degradation, HA |
| Trade-offs | Explicit alternatives and what would change the choice |
| Communication | Assumptions stated aloud; diagram matches the words |
Pitfalls
- Skipping estimation. Without numbers you cannot know whether the database, the fan-out, or the network is the problem.
- One consistency model everywhere. Feeds, counts, and payments need different guarantees. Choose per data type.
- Ignoring the hot path. Celebrities, viral posts, and hot keys break otherwise-uniform designs.
- No degradation plan. “The timeline store is down” must have an answer other than “the product is down.”
- A diagram without trade-offs. The design is the reasoning, not the boxes.
What are the five steps of the method, and why is the hybrid fan-out the right answer for a social feed? Which two data types in this design use opposite consistency stances?
ASolution
Clarify requirements, estimate scale, sketch high-level, deep-dive the flagged bottleneck, state trade-offs. Hybrid fan-out pushes posts to ordinary followers’ precomputed timelines (fast reads) while celebrities are pulled at read time, avoiding a fan-out storm at scale. Post durability uses strong consistency (CP/EC) while feed reads and like counts use eventual consistency (AP/EL).
That is the course. Pick a prompt, run the five steps, and design something measured.