Skip to the page

Indexes and query performance · Scenario

My orders API gets slower every week

The interviewer asks

Our 'My orders' API used to answer in 80 milliseconds. Over the last month its p99 has crept up to 4 seconds, and today the database CPU is at 90%. Nothing was deployed this week. What do you do?

Then

Follow-ups the interviewer asks next

  1. What do you ask and look at first?
  2. Users are complaining now. What can you do in the next 15 minutes?
  3. How do you find the exact query that is slow?
  4. You add an index, but p99 is still 1.5 seconds. What next?
  5. A sale next week brings 5 times the traffic. What must be in place?
  6. What's the proper long-term fix?
  7. How do you catch this before users notice next time?
  8. Who do you update, and how?
  9. Have you fixed a slow query in production? What was it?

The answer

What a strong answer covers

  • Clarifies: which endpoints, all users or heavy users, gradual or sudden. Gradual with no deploy points to data growth. Traces show time in the database, not the app.
  • Stops the damage: cancels runaway queries (pg_cancel_backend), caps page size, caches the first page for heavy users, adds the index with CONCURRENTLY.
  • Finds it: pg_stat_statements by total time, the slow query log (log_min_duration_statement, or MySQL's slow log), pg_stat_activity for what is running, then EXPLAIN (ANALYZE, BUFFERS).
  • Cause: the query filters by user_id and sorts by created_at with no index on both; fine at 2 million rows, slow at 41 million.
  • Twist: deep OFFSET pages for a reseller with 180,000 orders, and SELECT * pulling a large JSONB column the app never shows.
  • Long-term: (user_id, created_at DESC), keyset pagination, only the needed columns, archiving old orders.
  • Prevents it: alerts on p99 per endpoint and on top-query regressions, tests against production-sized data, EXPLAIN in code review for new queries.
  • Communicates: incident channel updates with an ETA, then a short postmortem.

The red pen

Common traps

Avoid:"add more CPU" first; restarting the database; indexing every column; CREATE INDEX without CONCURRENTLY on a busy table.

Practise this out loud

Say your answer out loud to an AI interviewer that follows up, and get a score for it.

Practise by voice

Practise questions like this out loud

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview