Sub topic 11
Deep dive into the riskiest component
This chapter assumes you have a high-level architecture, rough capacity numbers, and a basic grasp of replication, partitioning, and caching. It covers how to choose one component to examine closely and how to examine it.
Why you pick one component
A high-level design has many boxes, and you cannot examine all of them in depth. A deep dive spends your limited time where a failure would hurt most. A design that is sound everywhere but never tested at its weakest point is still unproven.
Choose the component using evidence you already have:
- Load: the component that handles the highest QPS from your estimates.
- State: the component that holds data, because stateless services are easy to replicate.
- Requirements: the component that carries the hardest non-functional requirement, such as low latency or strong consistency.
- Novelty: the part you understand least, or the part the problem is specifically about.
Say your choice out loud and give the reason. For example: "The item store takes the writes and holds all the data, so I'll look at it first."
A repeatable process
Each step is concrete:
- State the job and the limits. Use your estimates. If peak writes are about 1,200 QPS and one database node handles roughly 500 comfortably, you need at least three nodes or a different approach.
- Find the first bottleneck. Check CPU, memory, disk, network, and any single point of contention, such as one hot key or one lock.
- Propose one mitigation. Typical choices are partitioning, replication, caching, batching, or queuing.
- Name its cost. Every fix adds something: complexity, staleness, or a new failure mode.
Worked example: a hot partition
Suppose the item store is partitioned by owner_id across four shards. Hashing spreads keys evenly only if requests spread evenly across keys. This script shows what happens when one key dominates.
import hashlib
import random
from collections import Counter
def shard_for(key: str, shard_count: int) -> int:
digest = hashlib.md5(key.encode("utf-8")).digest()
return int.from_bytes(digest[:8], "big") % shard_count
SHARDS = 4
requests = [f"user:{i}" for i in range(1000)] + ["user:42"] * 9000
plain = Counter(shard_for(k, SHARDS) for k in requests)
print("plain:", sorted(plain.items()))
def salted(key: str, hot_keys: set, fanout: int = 8) -> str:
if key in hot_keys:
return f"{key}#{random.randrange(fanout)}"
return key
spread = Counter(shard_for(salted(k, {"user:42"}), SHARDS) for k in requests)
print("salted:", sorted(spread.items()))In the first result, one shard receives about 9,000 of the 10,000 requests. Adding shards does not help, because the single key still maps to a single shard. Salting a known hot key spreads its writes across several shards.
The cost is now visible. Reads for that key must query every salted variant and merge the results. You also need a way to know which keys are hot. Stating this cost is the point of the deep dive.
Common errors
- Choosing the most interesting component instead of the riskiest one.
- Optimizing without numbers. Without a limit, you cannot say whether a bottleneck exists.
- Going deep on one fix and never returning to the overall design.
- Ignoring failure. Ask what happens to this component when a node dies or a dependency slows down.
Exercise
Your design has a stateless API tier at 3,000 peak QPS and a single primary database taking 1,200 peak writes per second. Which component do you deep-dive first, and what two reasons support the choice?
Show answer
The database. It holds state, so it cannot be replicated as simply as the stateless API tier, and a single primary is both a throughput limit and a single point of failure. Then check its write limit against the 1,200 QPS peak.