Opening your learning path
Progress is saved as you study.
Opening your learning path
Progress is saved as you study.
Opening your learning path
Progress is saved as you study.
Latency & Capacity Planning is a depth lesson in the Foundations family of the Thita system design course.
The 45-minute interview framework, estimation with modern numbers, and CAP theorem.
It sits on the Deep Dive path — worth reading once the Interview Soon lessons are solid, or when a role names this area explicitly.
A system can meet average latency targets and still miss user-facing SLOs. Users notice slow requests. Interviewers ask about latency and capacity planning to see whether you can turn vague traffic into machine counts, queue limits, and failure-mode decisions.
Average latency hides slow requests. If 99 requests finish in 10 ms and 1 request takes 2 seconds, the average looks acceptable. One user still waited 2 seconds.
Use p99 latency as the default interview metric for user-facing systems. p99 means 99% of requests finish at or below that latency. The remaining 1% are slower. Large systems create many requests per user action, so that 1% becomes visible fast.
Example:
| Request group | Count | Latency |
|---|---|---|
| Fast requests | 99 | 10 ms |
| Slow request | 1 | 2,000 ms |
| Average | 100 | ~30 ms |
| p99 | 100 | ~2,000 ms |
The average says the service is fine. The p99 shows that some users wait two seconds.
A good latency statement:
1Target: p99 API latency under 200 ms.
2
3Budget:
4- 20 ms at load balancer and gateway
5- 40 ms service compute
6- 20 ms cache
7- 50 ms database
8- 70 ms buffer for retries, network jitter, and queueingThat answer gives the system a measurable target. It also leaves room for load spikes and dependency variance.
Tail latency amplification happens when one user request waits on many downstream calls. Each extra dependency adds another chance to hit a slow request.
A feed page often fetches a user profile, permissions, timeline items, media metadata, ads, ranking features, and notification state. The full request waits for the slowest required dependency.
If each downstream call has a 1% chance of being slow, one call is low risk. Ten independent calls create a larger risk.
1Probability all 10 calls are fast:
20.99^10 = 0.904
3
4Probability at least one call is slow:
51 - 0.904 = 9.6%A p99 dependency can produce p90 user experience when the parent request fans out to enough services. A design with 12 synchronous service calls can be logically correct and still miss the latency target.
Use this table when reasoning about fan-out:
| Fan-out count | If each call is fast 99% of the time | Chance at least one call is slow |
|---|---|---|
| 1 | 99.0% | 1.0% |
| 5 | 95.1% | 4.9% |
| 10 | 90.4% | 9.6% |
| 20 | 81.8% | 18.2% |
Keep synchronous fan-out small on the critical path. If the page can render without a dependency, make that dependency asynchronous, cached, precomputed, or best-effort.
A clear interview answer:
1The user-facing request should avoid 15 synchronous service calls.
2I would keep the critical path to auth, primary read, and one aggregation layer.
3Secondary data like recommendations, counters, and ads can be cached, stale, or loaded after first render.That answer protects the latency budget.
A feed page that waits on six services is slow 5.9% of the time if each is slow 1%; moving ads and counters off the critical path leaves four services and 3.9%.
Little’s Law connects QPS, latency, and required concurrency:
1concurrent_requests = arrival_rate × average_latencyUse seconds for latency. If a service receives 2,000 requests per second and average latency is 100 ms:
1concurrent_requests = 2,000 × 0.100 = 200The service has about 200 in-flight requests at steady state. In-flight requests consume memory, CPU scheduling, file descriptors, database connections, goroutines, threads, or event loop slots.
Little’s Law estimates steady-state concurrency with average latency. p99 protects user experience. Use Little’s Law for capacity and p99 for SLOs.
A useful sequence:
1Average QPS = DAU × actions/user/day ÷ 86,400
2Peak QPS = average QPS × 2 or 3
3Concurrency = peak QPS × average latency secondsExample:
110M DAU
220 read actions per user per day
3
4Average QPS = 10M × 20 ÷ 86,400 ≈ 2,315 QPS
5Peak QPS = 2,315 × 3 ≈ 7,000 QPS
6
7If avg latency = 80 ms:
8Concurrency = 7,000 × 0.080 = 560 in-flight requestsThat estimate helps size service instances, connection pools, database access, and batching.
QPS alone hides risk. A service at 5,000 QPS with 10 ms latency has about 50 in-flight requests. A service at 5,000 QPS with 500 ms latency has about 2,500 in-flight requests. Same QPS, different operating risk.
Queues turn clean estimates into incidents. When a server is lightly loaded, requests start quickly. When utilization approaches 100%, each new request waits behind existing work. Latency climbs faster than most people expect.
Keep user-facing services around 60-70% utilization at peak. That leaves headroom for bursts, GC pauses, noisy neighbors, slow downstreams, retries, deploy imbalance, and uneven load. Running at 90% can look efficient on a dashboard while p99 latency is already fragile.
A simple mental model:
| Utilization | What happens | Interview recommendation |
|---|---|---|
| 30-50% | Plenty of headroom, low queueing | Safe; may be overprovisioned |
| 60-70% | Efficient with burst tolerance | Good default target |
| 80-90% | Queues grow during small spikes | Risky for user-facing traffic |
| 95-100% | Latency collapse, retries amplify load | Treat as overloaded |
Feedback loops cause the worst failures. High utilization increases latency. Higher latency increases concurrent requests through Little’s Law. More concurrency consumes more resources. Aggressive retries add traffic while the system is already overloaded.
Retry policy belongs in capacity planning. A timeout without jittered backoff can turn a slow database into a full outage. In interviews, mention retries with limits: short timeout, bounded retries, exponential backoff, jitter, and circuit breaking for optional dependencies.
A compact overload explanation:
1At 70% utilization, a traffic spike has room to clear.
2At 95%, the same spike becomes a queue.
3The queue increases latency.
4Higher latency increases in-flight requests.
5Retries add more work.
6The service falls behind even if the original QPS was survivable.Spend two to three minutes on estimates. Use the result to choose the architecture.
Use this order:
A clean estimate:
1DAU = 50M
2Actions/user/day = 10
3Average QPS = 50M × 10 ÷ 86,400 ≈ 5,800
4Peak QPS = 3 × average ≈ 17,500
5Response size = 20 KB
6Peak bandwidth = 17,500 × 20 KB ≈ 350 MB/sNow connect the estimate to a decision. For 17,500 simple indexed read QPS, use a service fleet with Postgres replicas or a cache tier before sharding.
Storage estimate:
1Writes/day = 100M
2Bytes/write = 1 KB
3Retention = 365 days
4
5Storage = 100M × 1 KB × 365 ≈ 36.5 TB/yearAt tens of TB, pick one Postgres primary when write rate, index count, vacuum pressure, and operations are still manageable. Add replicas for reads. Use table partitioning for large time-series or tenant-scoped data. Shard when the write rate, storage growth, or maintenance load exceeds one primary’s safe operating range. Do not shard at 100 GB.
A latency budget turns a vague target into engineering limits. If the product needs p99 under 300 ms, every synchronous dependency must fit inside that budget. A new 100 ms remote call can break the target when only 40 ms remains.
Example p99 budget for a read API:
| Component | Budget |
|---|---|
| Client to edge | 30 ms |
| Edge/gateway | 20 ms |
| Service logic | 40 ms |
| Cache read | 5 ms |
| Database fallback | 50 ms |
| Serialization | 10 ms |
| Buffer for queueing/retries | 145 ms |
| Total | 300 ms |
The buffer protects against queueing, network jitter, GC, lock contention, and dependency variance. Designs without buffer pass whiteboard math and fail under real traffic.
When a dependency does not fit, pick one of these:
| Problem | Better design | Why |
|---|---|---|
| Slow optional data | Load asynchronously | Protects critical path |
| Expensive repeated read | Cache or precompute | Converts runtime work into earlier work |
| Many downstream calls | Aggregate behind one service | Controls fan-out and timeout policy |
| Remote cross-region call | Replicate data locally | Removes 70-250 ms network penalty |
| Large response | Paginate or compress | Reduces bandwidth and serialization time |
Make the first response useful. A page that renders core data in 200 ms and fills secondary data later usually beats a complete page that appears after 2 seconds.
Use this decision tree when an interviewer raises the scale:
1Is the bottleneck CPU?
2 Add service instances and keep utilization near 60-70%.
3
4Is the bottleneck repeated reads?
5 Add cache only if DB read load or latency justifies it.
6
7Is the bottleneck write throughput?
8 Batch, queue, partition by key, or reduce indexes before sharding.
9
10Is the bottleneck storage size?
11 Check retention, compression, cold storage, and table partitioning.
12
13Is the bottleneck cross-region latency?
14 Move reads closer to users; avoid synchronous global writes.Each tool should map to a measured bottleneck. If you cannot name the bottleneck, you are adding complexity as decoration.
For most interview systems, start simple:
| Scale signal | Default move |
|---|---|
| Thousands of QPS | Stateless services behind a load balancer |
| Read-heavy with repeated objects | Cache hot keys |
| Tens of TB relational data | Single Postgres primary with replicas and partitioning before sharding |
| High write burst | Queue writes if user response does not need immediate processing |
| Global users | CDN and regional read paths before multi-region writes |
Scale the design when the numbers force it.
State p99 latency targets. Estimate peak QPS. Use Little’s Law to calculate in-flight requests. Keep user-facing services at 60-70% peak utilization. Treat synchronous fan-out as a latency risk. Start with a single well-sized database, indexed reads, and replicas. Add caching when the numbers show read pressure or latency pressure.
| Category | Metric | 2026 number to use |
|---|---|---|
| Machine capacity | General-purpose RAM | 512 GiB |
| Machine capacity | Memory-optimized RAM | 4 TB |
| Machine capacity | High-memory instance RAM | 24 TB |
| Machine capacity | General-purpose CPU | 128 vCPUs |
| Machine capacity | Local SSD | 60 TB |
| Machine capacity | Local HDD | 336 TB |
| Network | Within datacenter | 25-100 Gbps |
| Latency | Same AZ | < 1 ms |
| Latency | Cross-AZ | 1-2 ms |
| Latency | US-EU cross-region | 70-90 ms |
| Latency | US-India | 150-250 ms |
| Latency | Redis GET | < 1 ms |
| Latency | Postgres indexed query | 1-5 ms |
| Latency | S3 first byte | 50-100 ms |
| Latency | CDN edge | 5-30 ms |
| Throughput | Redis instance | 100K-500K ops/sec |
| Throughput | Postgres single primary | 10K-50K TPS |
| Throughput | Postgres size before sharding | Tens of TB |
| Throughput | Kafka cluster | 1M+ messages/sec |
| Throughput | Kafka latency | Single-digit ms |
| Throughput | Elasticsearch cluster | 10K-50K queries/sec |
| Estimation | QPS from DAU | DAU × actions/user ÷ 86,400 |
| Estimation | Peak QPS | 2-3× average QPS |
| Estimation | Storage | writes/day × bytes/write × retention days |
| Estimation | Bandwidth | QPS × average response size |
| Capacity target | Service utilization | 60-70% at peak |