r/sre • u/Academic-Rush-2587 • 4h ago
How I stopped guessing during "the database is slow" pages (4-layer triage checklist + real outage patterns)
Every team has the same reflex at 3 AM: p99 spikes, dashboards turn red, and everyone immediately blames the database. Half the time the database is fine—and even when it isn't, staring at average CPU or slow-query logs wastes the first 20 minutes of an incident.
Over the last few years of running Postgres, MySQL, and ClickHouse on Linux and Kubernetes, I distilled my incident triage down to a strict 4-layer order so I stop guessing under pressure:
**1. Pin down where time is actually going (before touching a terminal)**
* **Split App timer vs. DB timer:** Compare what the application reports for a query against what the database logs internally. If the app sees 200ms and the DB sees 5ms, stop tuning SQL—your bottleneck is connection pool queuing, DNS (`ndots:5`), or TCP retransmits.
* **Check Little's Law (`L = λ × W`) before touching `max_connections`:** At 1,000 QPS and 20ms latency, you only hold 20 active connections. If a bad plan pushes latency to 200ms, you suddenly need 200 connections—and a 50-connection pool collapses instantly. Raising `max_connections` rarely fixes this; it usually just adds context-switching and lock contention.
* **Mitigate first, root-cause second:** Kill the runaway query, shed batch traffic, or roll back first—just write down what you changed so you don't erase forensic evidence.
**2. The Host (first 60 seconds)**
* **Load average 40 with 10% CPU = I/O wait:** Linux load includes `D`-state (uninterruptible I/O sleep) tasks. Run `iostat -xz 1` for `await` spikes and `dmesg` for storage/filesystem stalls.
* **Ignore `%util` on NVMe and cloud volumes:** Parallel device queues hit 100% utilization long before saturation. Watch `r_await` / `w_await` and `/proc/pressure/{cpu,memory,io}` (PSI) instead.
**3. Database Engine Traps**
* **Rank by total time (`calls × mean_time`), not single-query duration:** A 2ms lookup running 3M times/day burns 100 minutes of DB time (75x more than a 4-second report running 20 times).
* **Lock queues behind DDL:** In both Postgres (Joyent Manta 2015) and MySQL, an `ALTER` or `DROP` waiting on an autovacuum or an idle-in-transaction session blocks *every* subsequent `SELECT` behind it. Always set `lock_timeout` / `lock_wait_timeout` on production migrations.
* **Check the coordinator, not just the storage nodes:** On a replicated ClickHouse cluster I ran, inserts stalled and DDL hung even though disk and CPU looked fine—turned out Keeper had accumulated ~25M deduplication znodes under `/blocks`, pushing Keeper latency to ~700ms and blocking end-of-day billing behind it.
**4. Kubernetes & Cloud Platform Ceilings**
* **CFS CPU throttling (`nr_throttled`):** A 2-CPU limit gives a pod 200ms of CPU time per 100ms period. An 8-thread query burns that entire quota in 25ms and gets frozen by the kernel for the remaining 75ms—even on an idle node.
* **Page cache counts against container memory limits:** Disk-cached engines like Postgres and ClickHouse quietly lose OS page cache in containers and spill to disk far earlier than on bare metal.
* **Cloud volume speed limits:** Baseline disks like AWS `gp3` cap at 3,000 IOPS. Hitting 3,000 reads/sec with 24ms `await` isn't a broken disk—it's a disk hitting its provisioned ceiling.
---
Full disclosure: I put together a 31-minute animated walkthrough covering this full framework along with postmortems from Foursquare, Stack Overflow, GitHub, Cloudflare, and Roblox if anyone prefers a video breakdown:
https://www.youtube.com/watch?v=xhWyT1xBYAI
Curious what everyone else's "first 60 seconds" check is when an app team pages you swearing the database is dying?