Notes
All notes

Performance Antipatterns and Monitoring (System Design #15)

The 3 a.m. page fires: “checkout latency p99 over 5s.” You open the dashboard. CPU is fine, memory is fine, error rate is normal — yet users are timing out. Twenty minutes later you find it: a nightly analytics query grabbed a connection pool, and a chatty service is making 400 small calls per checkout. Nothing you monitor told you why, because you were monitoring the machine, not the request.

This chapter is about the mistakes that cause these outages and the instrumentation that catches them. First the recurring performance antipatterns — named, because naming a failure makes it visible in design reviews. Then monitoring: what to measure, how to instrument, and how to alert without drowning in noise.

The performance antipatterns

These are the failure modes that show up again and again. Learn them as a checklist to run against any design.

Antipattern Symptom Fix
Busy database DB CPU pegged; app waits offload work, cache, indexes, read replicas
Busy frontend one slow page blocks everything async, isolate, lazy-load
Chatty I/O hundreds of small calls batch, aggregate, persist connections
Extraneous fetching fetching more data than needed project only needed fields
No caching identical reads hit the DB cache hot reads (Chapter 12)
Synchronous I/O threads blocked on slow calls async, queues, timeouts
Monolithic persistence one DB does everything split workloads by need (Chapter 10)
Noisy neighbor one tenant/query starves others isolate, rate-limit, partition
Retry storm retries amplify an outage backoff + jitter + circuit breaker
Improper instantiation new client/pool per request reuse connections/pools

Three deserve elaboration because they cause most incidents.

Busy database. The database is usually the bottleneck. It happens when trivial reads, analytics, and joins all share the same database as the transactional path. The cure is a sequence: index and tune, add caching, move analytics to a replica or a separate store. The antipattern is letting one database serve every purpose.

Chatty I/O. A checkout that makes one call per item, per discount, per inventory check, per tax line multiplies latency and failure probability. The fix is to batch — one call for all items — and to aggregate on the server rather than orchestrate in the client. Chapter 9’s fan-out and Chapter 14’s synchronous chains are the same problem.

Retry storm. When a dependency slows down, every caller retries, multiplying load on the already-struggling service until it dies. This is the deadliest antipattern because the recovery attempt causes the outage. The only defense is disciplined retries: capped, backed off with jitter, guarded by a circuit breaker, only on idempotent operations.

\begin{tikzpicture}[font=\rmfamily, >=stealth,
  b/.style={draw, thick, rounded corners=3pt, minimum width=1.9cm, minimum height=0.66cm, align=center, font=\rmfamily\small},
  a/.style={->, thick}]
  \node[b, fill=orange!16] (slow) at (-3.4,0) {dependency slows};
  \node[b, fill=red!12] (retry) at (-0.6,0) {callers retry $\times N$};
  \node[b, fill=red!20] (down) at (2.4,0) {cascading collapse};
  \draw[a] (slow) -- (retry); \draw[a] (retry) -- (down);
  \node[font=\rmfamily\small, text=black!60, anchor=north] at (0,-0.75) {the fix must include a circuit breaker, not just faster retries};
\end{tikzpicture}

What to monitor: the four golden signals

Every service exposes four signals. If you instrument only four things, instrument these:

# the four golden signals, recorded per request
def record(req, latency_ms, ok, saturation):
    metrics.inc("traffic.total", tags={"route": req.route})
    metrics.observe("latency_ms", latency_ms, tags={"route": req.route, "ok": ok})
    if not ok:
        metrics.inc("errors.total", tags={"route": req.route, "type": req.error})
    metrics.gauge("saturation", saturation, tags={"resource": "db_pool"})

Saturation is the one teams forget. A service at 60% saturation can look healthy while the p99 quietly climbs; at 90% it collapses suddenly. Alerting on saturation gives you time; alerting on errors tells you after the fact.

Types of monitoring

Different questions need different monitors:

They combine: infrastructure metrics say the system is fine, usage metrics say nobody can check out — the bug is in business logic, not the servers.

Instrumentation: metrics, logs, traces

Three complementary tools, often called the three pillars:

\begin{tikzpicture}[font=\rmfamily, >=stealth,
  b/.style={draw, thick, rounded corners=3pt, minimum width=1.95cm, minimum height=0.66cm, align=center, font=\rmfamily\small},
  a/.style={->, thick}]
  \node[b, fill=blue!10] (app) at (-3.4,0) {service};
  \node[b, fill=green!12] (m) at (-0.3,0.9) {metrics};
  \node[b, fill=orange!16] (l) at (-0.3,0) {logs};
  \node[b, fill=cyan!15] (t) at (-0.3,-0.9) {traces};
  \node[b, fill=yellow!22] (d) at (2.8,0) {dashboards + alerts};
  \draw[a] (app) -- (m); \draw[a] (app) -- (l); \draw[a] (app) -- (t);
  \draw[a] (m) -- (d); \draw[a] (l) -- (d); \draw[a] (t) -- (d);
\end{tikzpicture}

The discipline is correlation: stamp every log and trace with a request id and the same labels (service, route, tenant) so you can jump from an alert to the trace to the logs without guessing.

Visualization and alerting

A dashboard answers “is it healthy?”; an alert answers “should I wake up?” These are different and should not be the same threshold.

def slo_alert(error_rate, window_min, slo=0.999):
    # page when the multi-minute error rate clearly exceeds the SLO
    allowed = 1 - slo
    if error_rate > allowed * 14 and window_min >= 5:   # ~14x burn
        return "page"
    if error_rate > allowed * 3 and window_min >= 30:
        return "ticket"
    return "ok"

A fast burn rate (14× the allowed error budget over a short window) means you will exhaust the month’s budget in hours — that is a page. A slow burn is a ticket.

Putting it together

For every service, define: the SLO (Chapter 5’s nines), the four golden signals, health endpoints for the LB, structured logs with a correlation id, traces across hops, and a small set of symptom-based, multi-window alerts. Then run a weekly check against the antipattern table. Most outages are a known antipattern plus a monitoring gap.

Pitfalls

QQuestionSelf-check

What are the four golden signals, and which one predicts outages earliest? Why alert on symptoms rather than causes?

ASolution

Latency, traffic, errors, and saturation. Saturation leads — it rises before latency and errors degrade. Alert on symptoms because they reflect user impact and are actionable; cause-based alerts fire on conditions that are often harmless and create alert fatigue.

Next: Cloud Design Patterns (System Design #16) — a catalog of named solutions to the problems this course has raised.