API latency doubled but errors stayed flat
One endpoint slowed after a release
Troubleshooting, Medium
You are on call for an orders API. Since 09:40 errors did not rise. At 10:00 you know:
- GET /customers/{id}/orders doubled: p50 120 to 240 ms, p95 400 to 800 ms. Other endpoints are unchanged.
- Database CPU rose from 30% to 75% at 09:40. The slow-query log shows one orders query at 95 ms, up from 4 ms.
- A release at 09:35 ran "migration: tidy orders indexes". The table has 8 million rows, thousands per busy customer, and an index on customer_id. Until the release it also had (customer_id, created_at).
Name your checks in order with their evidence. Then give the cause, a safe fix, the proof and the prevention.
Examples
EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM orders WHERE customer_id = $1 ORDER BY created_at DESC LIMIT 20 for one busy customerSELECT indexname FROM pg_indexes WHERE tablename = 'orders';Constraints
- Flat since 09:40: errors 0.2%, traffic about 900 requests per second, app CPU 35% on 6 instances, cache hit rate 92%. The client timeout is 30 seconds.
- You have read access to the metrics, logs and database, and you can run an index build in a quiet window. The table receives writes all day.
- There is no incident ticket yet. Customers have not complained, but a dashboard alert fires at p95 over 700 ms.
- Do not take the database offline.
Your answer must contain
- An ordered list of checks, and what each one shows
- The cause, tied to the facts above
- The fix, with a safe way to apply it on a busy table
- How you confirm it works and how you stop it coming back
A strong answer covers A strong answer scopes the slowdown, ties it to the 09:35 migration, reads the query plan, rebuilds the index without blocking writes, and adds a latency alert and an index review.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Only one endpoint is slow and the errors are flat. Start with what changed at 09:35, then ask which of load, code and the database moved.
Hint 2 the approach
Load, app CPU and cache hits are flat, so the database moved. Look at the slow query and ask how the database runs it now.
Hint 3 the key step
Compare the query plan with the indexes that exist. Then rebuild the missing index in a way that does not block writes on a busy table.
Walkthrough
Full walkthrough brute force to best
First minutes, in order
The checks, in order. 1. Scope: only the endpoint that reads orders is slow, so it is not the network, the load balancer or every instance. 2. Time line: the slowdown starts at 09:40, five minutes after the 09:35 release, which has a migration. A change at the same time is the first suspect. 3. Load: requests per second, app CPU and cache hit rate are flat, so it is not more traffic, slower code or more cache misses. 4. Dependencies: database CPU rose from 30 to 75 percent at 09:40, and the slow-query log shows one query going from 4 ms to 95 ms.
Narrow down the cause
5. Plan: run EXPLAIN (ANALYZE, BUFFERS) on that query. It shows an index scan on the customer_id index that reads thousands of rows for a busy customer, then a sort. Yesterday the (customer_id, created_at) index returned the newest 20 in order with no sort. The index list now shows only orders_pkey and orders_customer_id_idx, and the migration file shows that it dropped the other index.
Cause and fix
Cause: the migration removed the index that served this query, so each call reads and sorts all of one customer's orders. It reads one customer, not the whole table, which is why it takes 95 ms and not seconds. The calls are slower but finish before the 30 second timeout, so the error rate stays flat. Fix: if the migration is easy to revert, roll it back. Otherwise rebuild the index without blocking writes, with CREATE INDEX CONCURRENTLY on customer_id and created_at descending. It takes longer and cannot run in a transaction, but writes continue. Run it in a quiet window and watch disk and replica lag.
Read the evidence
Read the evidence. EXPLAIN for a busy customer shows an index scan on orders_customer_id_idx with 3,012 rows, then a top-N sort, with an execution time of 94 ms. The list of indexes shows only orders_pkey and orders_customer_id_idx. So the query reads about 3,000 rows and sorts them to find 20. The flat requests, CPU and cache hit rate show that load and code did not change, so they do not explain the slowdown.
Commands
EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM orders WHERE customer_id = $1 ORDER BY created_at DESC LIMIT 20;
SELECT indexname FROM pg_indexes WHERE tablename = 'orders';
CREATE INDEX CONCURRENTLY orders_customer_created_idx ON orders (customer_id, created_at DESC);Confirm and prevent
Confirm: EXPLAIN shows an index scan, the query returns to about 4 ms, database CPU returns to about 30 percent, and p50 and p95 return to 120 ms and 400 ms for a day. Prevent: review index changes with EXPLAIN on production-size data, block a migration that drops an index used in the last 30 days, alert on p95 latency as well as errors, and use a canary release.
Common mistakes
- Adding app instances. The app is not the bottleneck.
- Raising the timeout, or restarting the database.
- Saying that no errors means no problem.
- Running a plain CREATE INDEX on a busy table. It blocks writes.