Course map

Topic 104 / Core

Performance optimization and benchmarking

Chapter 6.5 · Performance Analysis

What and why

Finding a bottleneck (→ 103) is only half the job — fixing it well, and proving the fix actually helped, is the other half. Performance optimization is choosing and applying a fix to the biggest measured bottleneck; benchmarking is measuring honestly enough, before and after, that the claimed improvement is real and not an artifact of a cold cache, a tiny dataset, or a lucky run. Builds on: → 103 (profiling and bottleneck analysis).

Diagram

Benchmarking mistake What it hides
Cold cache on the "before" run only an unfairly slow baseline, making any fix look better than it is
Unrealistic (tiny or uniform) data skipped index scans, missing skew effects real traffic would hit
Too short a run percentiles (→ 100) that haven't stabilized; a lucky or unlucky window
Reporting only the mean the exact tail behavior (→ 100) the fix was often meant to improve

How it works

  1. Start from a measured bottleneck (→ 103), not a guess — optimize the resource or code path profiling actually identified as constrained.
  2. Apply the smallest fix that addresses the measured cause: an index (→ 64), a batched query (→ 103), a cache (→ 82), a pool resize (→ 77) — prefer the cheapest change that targets the actual bottleneck over a larger rewrite.
  3. Benchmark "before" and "after" under matching conditions: the same warmed-up state, the same realistic data size and distribution, the same duration, the same load pattern.
  4. Report percentiles (p50, p90, p99, → 100), not just the mean — a fix may move the tail and barely touch the median, or vice versa, and only percentiles show which.
  5. Re-run the USE method (→ 103) after the fix: the bottleneck often moves to a different resource once the first one is relieved — the signal to decide whether to optimize further or stop.

Practical example

(numbers illustrative). Before a fix, an endpoint's N+1 query pattern (→ 103's example) produces p50 = 40 ms, p99 = 400 ms, at 2,010 database queries/second for the endpoint. After batching the queries to 20/second, a benchmark run with the same data size and a matching duration under the same load shows p50 = 8 ms, p99 = 35 ms — a 5× improvement at p50 (40 ÷ 8) and roughly an 11.4× improvement at p99 (400 ÷ 35 ≈ 11.4). Reporting only "average latency improved" would have hidden that the tail improved even more than the median — exactly the fix's intended effect, since the N+1 pattern was disproportionately hurting the rows with the most related records.

When to use it

  • Use this discipline (measure, fix the biggest bottleneck, benchmark honestly, re-measure) for any performance work with a real cost to being wrong.
  • Avoid optimizing a path that profiling (→ 103) didn't actually flag — intuition about "the slow part" is frequently wrong, and effort spent there doesn't show up in any real metric.
  • Failure scenarios: benchmarking against a warm cache for the "after" run and a cold one for "before," manufacturing an improvement that isn't real; declaring victory on a mean-latency improvement while p99 stayed flat or got worse; optimizing repeatedly without re-profiling, missing that the bottleneck already moved elsewhere.
  • Simpler alternative: for a one-off, low-stakes slow path noticed informally, a quick fix and a rough before/after timing may be enough — full benchmarking rigor earns its cost once the decision has real consequences.

Practice

(process/mechanism kind — hands-on benchmarking, 20–30 min, lab: Postgres).

  1. Reuse the N+1-vs-batched query pair from topic 103's hands-on task; benchmark both under matching conditions and report p50/p90/p99 for each, not just the mean.
  2. Deliberately break the benchmark's fairness (warm the cache only for the "after" run) and show the resulting comparison overstates the improvement versus the fair one from step 1.
  3. Re-run the USE method (→ 103) on the "after" version; identify whether a new bottleneck has emerged and state what you'd optimize next.

Done when your fair before/after comparison reports percentiles, not just a mean, and the unfair version from step 2 is shown to overstate the improvement.

Shared lab setup

Key takeaway

Optimization without honest benchmarking is a guess with extra steps; fix the bottleneck profiling actually found, measure before and after under matching conditions, and report percentiles. Done when you can: produce a fair before/after benchmark for a real fix and explain what would make it unfair. Used next in Part 7 (messaging and asynchronous processing — the next lever once synchronous-path optimization is exhausted).

References

None — this topic makes no external claims.

Exercise: Size Project 13's read path: 10M DAU, 5 feed loads per user per day, p99 < 200 ms. Compute mean and peak QPS, cache memory for hot feeds, and instance count via Little's Law at 50 ms per request. Then find the serial fraction that caps further scaling.

Source directory

Search the guide