← Whiteboard questions

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

Input EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM orders WHERE customer_id = $1 ORDER BY created_at DESC LIMIT 20 for one busy customer
Output Index Scan using orders_customer_id_idx (rows=3,012), then Sort (top-N heapsort), Execution Time: 94 ms.
Why Shows how the database runs the query and where the time goes. Compare it with what the query asks for.
Input SELECT indexname FROM pg_indexes WHERE tablename = 'orders';
Output orders_pkey, orders_customer_id_idx
Why Shows which indexes exist now. Compare the list with the release notes and with the query.
Input Decoy: requests per second, application CPU and cache hit rate before and after 09:40
Output All three are flat.
Why Load and application code did not change. This does not say which part of the database work grew.

Constraints

Your answer must contain

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.

Related problems

API latency doubled but errors stayed flat walkthrough