Scalability, Performance, Latency and Throughput (System Design #2)
Two engineers argue about an outage. “The system is too slow,” says one. “No, it’s overloaded,” says the other. They are describing different failures: one is performance, the other is scalability. Fixing the wrong one wastes weeks.
This chapter separates the four words people use interchangeably — performance, scalability, latency, throughput — and shows how each is measured and improved. It also covers the two ways to add capacity (up and out), why statelessness is the hidden prerequisite, and how to read a latency distribution instead of an average.
Performance vs scalability
- Performance is how fast the system is for a given load. A user’s page loads in 120 ms. Performance is a property of the request.
- Scalability is how the system behaves as load grows. Doubling traffic keeps latency flat; that is scalability. Scalability is a property of the curve.
A system can be performant but not scalable (fast for 100 users, dies at 10,000) or scalable but not performant (handles millions, each request slow). You fix them differently: performance by optimizing the request path (caches, indexes, fewer round trips); scalability by adding resources and removing shared bottlenecks.
\[ \text{scalability} = \frac{d(\text{capacity})}{d(\text{resources})} \quad \text{— ideally } 1, \text{ often } < 1 \]
If doubling servers less than doubles throughput, something is shared: a single database, a hot key, a lock, one load balancer. Find the shared thing; that is the scaling limit.
Latency vs throughput
Latency is the time one request takes (often quoted as a percentile). Throughput is how many requests complete per unit time. They trade off, and the trade-off is not free:
- Batching increases throughput (fewer, fuller operations) but increases latency (wait to fill the batch).
- Adding parallel workers increases throughput and can increase latency under heavy load via queueing.
That last effect is worth internalizing. A server has finite capacity. As arrival rate approaches capacity, queueing theory says latency does not rise linearly — it explodes. This is why a service can be “fine at 60% load” and “catastrophic at 90%”.
\begin{tikzpicture}[font=\rmfamily, >=stealth,
a/.style={->, thick}]
\draw[a, black!45] (0,0) -- (7.4,0) node[below, font=\small] {offered load (\%)};
\draw[a, black!45] (0,0) -- (0,3.2) node[left, font=\small, align=right] {latency};
\draw[very thick, orange!80!black] (0,0.2) .. controls (5.2,0.4) and (6.4,1.0) .. (7.0,3.0);
\draw[dashed, black!45] (6.2,0) -- (6.2,3.2);
\node[font=\small, text=black!60, anchor=south] at (6.2,3.2) {capacity};
\node[font=\small, text=orange!70!black, anchor=west] at (2.2,1.1) {latency explodes near saturation};
\end{tikzpicture}
Percentiles, not averages
An average hides the users who are suffering. Report p50, p95, p99 (and p99.9 for critical paths):
| Metric | Meaning |
|---|---|
| p50 | half of requests are faster |
| p95 | 5% are slower — the “annoyed users” line |
| p99 | 1% are slower — the tail, often the heaviest users |
| p99.9 | the pain that pages you at 3 a.m. |
Tail latency compounds. If one page makes 10 backend calls and each has a 1% chance of being slow, the chance that at least one call is slow is \(1 - 0.99^{10} \approx 9.6\%\). The page’s p99 can be a backend’s p90. This is why fan-out systems obsess over the tail and why you hedge slow requests.
# tail amplification with fan-out
def page_slow_prob(n_calls, per_call_slow=0.01):
return 1 - (1 - per_call_slow) ** n_calls
for n in [1, 5, 10, 50]:
print(n, f"{page_slow_prob(n):.1%}")
# 1 -> 1.0% 5 -> 4.9% 10 -> 9.6% 50 -> 39.5%
The lesson: reduce fan-out, hedge, and measure the tail. Averages on dashboards are how you miss incidents.
Scaling up vs scaling out
Vertical scaling (scale up): buy a bigger box. Simple, no code changes, strong consistency stays easy. But it has a hard ceiling (the biggest machine exists), it is expensive at the top, and the single box is a single point of failure.
Horizontal scaling (scale out): add more boxes behind a distributor. No practical ceiling, tolerates failure, cheaper at scale — but the system must be built to distribute load and handle partial failure.
\begin{tikzpicture}[font=\rmfamily, >=stealth,
b/.style={draw, thick, rounded corners=3pt, minimum width=1.7cm, minimum height=0.75cm, align=center, font=\rmfamily\small},
a/.style={->, thick}]
\node[b, fill=blue!10] (app) at (-3.0,0) {app};
\node[b, fill=orange!16] (lb) at (0.4,1.0) {LB};
\node[b, fill=green!12] (s1) at (-1.2,0.0) {srv 1};
\node[b, fill=green!12] (s2) at (0.4,0.0) {srv 2};
\node[b, fill=green!12] (s3) at (2.0,0.0) {srv 3};
\draw[a] (app) -- (lb); \draw[a] (lb) -- (s1); \draw[a] (lb) -- (s2); \draw[a] (lb) -- (s3);
\node[font=\rmfamily\small, text=black!60, align=center] at (3.6,-0.05) {scale out:\\add boxes behind\\a distributor};
\end{tikzpicture}
In practice, do both: scale up until near the top, then scale out. Most systems are a mix.
Statelessness: the prerequisite
Horizontal scaling only works if any server can handle any request. That requires stateless app servers: no session data, no uploaded temp files, no in-memory counters that matter.
Move state to shared stores:
- Sessions → Redis or a signed cookie.
- Uploads → object storage (S3-style).
- Counters/IDs → a dedicated service or a database sequence.
- Cache → a shared cache, not per-server memory (unless you accept lower hit rates).
# bad: state on the server. Restart loses it; a second server disagrees.
sessions = {}
def login_bad(user): sessions[user.id] = True
# good: shared store. Any server can read it.
def login_good(user, redis):
redis.setex(f"session:{user.id}", 3600, "1")
Statelessness is what turns “a server” into “a pool of interchangeable servers”, which is what every later chapter assumes: load balancing (#8), autoscaling, rolling deploys, and fault tolerance all depend on it.
Where the ceiling actually is
When adding servers stops helping, the bottleneck has moved. The usual suspects, in order:
- The database — one primary accepts all writes. Replication helps reads; sharding helps writes (#11).
- Shared locks / hot keys — a single counter or popular row serializes everything.
- Stateful components — sessions, sticky connections, in-memory queues.
- The network / the load balancer — usually last, but a single LB is a single point of failure (#8).
- Amdahl’s Law — if 10% of the work cannot be parallelized, the maximum speedup is 10×, no matter how many servers you add.
\[ \text{speedup}(N) = \frac{1}{s + \frac{1-s}{N}} \]
where \(s\) is the serial fraction. Even 95% parallel work caps you at 20×. This is why a single shared write path defeats an otherwise scalable system.
Pitfalls
- Optimizing the average. Tail latency is what users and paging systems see. Track percentiles and fan-out amplification.
- Adding servers to a shared bottleneck. If the database is the ceiling, more app servers make it worse. Find the shared resource first.
- Holding state on app servers. Sticky sessions and local caches quietly prevent scaling and break rolling deploys. Go stateless first.
- Confusing throughput with scale. If you have to loosen correctness to gain throughput, you may be trading the wrong resource. Match the fix to the failure.
- Ignoring Amdahl. A 5% serial path caps speedup at 20×. The serial part is usually a lock or a single writer.
Why can tail latency be worse than the average suggests, and what does fan-out do to it? What does statelessness buy?
ASolution
Because a page often makes many backend calls and the slowest one determines the page’s latency; even a 1% per-call slow rate becomes ~10% at 10 calls. Fan-out amplifies the tail. Statelessness lets any server serve any request, which is the prerequisite for load balancing, autoscaling, and failing over without losing sessions.
Next: Availability, Consistency and CAP (System Design #3) — the theorem behind every distributed trade-off.