ENGINEERING JOURNAL

Debugging a Slow API Endpoint: A Repeatable Method

A repeatable method for debugging a slow API endpoint: measure, check the database, time the parts, and confirm the fix.

THE ARTICLE 01 — 02

A slow API endpoint is one of the most common production complaints, and one of the easiest to chase in circles. The user reports "the dashboard is slow", someone restarts the server, it feels faster for ten minutes, then it is slow again. The fix is not a faster server. It is a repeatable method: measure where the time actually goes, fix the biggest slice first, then measure again. Here is the method I use, in the order I use it.

Step 1: Reproduce it and get a real number

Never debug from a feeling. Hit the endpoint yourself and time it. Curl gives you the breakdown for free:

curl -o /dev/null -s -w "total: %{time_total}s | connect: %{time_connect}s | starttransfer: %{time_starttransfer}s\n" https://example.com/api/orders

If time_starttransfer is close to time_total, the server spent the time working, not waiting on the network. That tells you the problem is in your code, your database, or a downstream call. Log the number. Without a baseline you cannot tell whether a fix worked.

Step 2: Check the database first, because it usually is the database

In my experience, most slow endpoints turn out to be slow queries. The usual suspects are N+1 queries, a missing index on a filtered column, or a query that loads far more rows than the response needs. Turn on Laravel query logging for the request, or watch the slow query log, and count the queries. Ten queries is fine. Two hundred queries for one list endpoint is the bug.

Once you have the worst query, read its execution plan before guessing. I wrote a separate post on reading PostgreSQL EXPLAIN ANALYZE to find the slow query, and the same habit applies here: the plan tells you whether the database is scanning a whole table or using an index. Fix the query, add the index, or paginate the result. Then re-time the endpoint.

Step 3: Time the parts, not just the whole

If the database is fast and the endpoint is still slow, you need timings inside the request. Add temporary timing around the main stages: auth, database, external HTTP calls, and response building. In Laravel, a few microtime(true) checkpoints logged to a file are enough for a one-off investigation:

$start = microtime(true);
// ... run the suspected slow block ...
Log::info('orders export took '.round(microtime(true) - $start, 3).'s');

Do not leave debug logging in production permanently, but do not be afraid to add it temporarily either. Guessing which part is slow wastes more time than the ten minutes it takes to instrument the request. In one case I worked on, the "slow database" turned out to be a single HTTP call to a third-party service with a 30 second timeout and no timeout configured on our side.

Step 4: Look for work that should not happen in the request at all

Some endpoints are slow because they do work that belongs somewhere else. Sending emails, calling a webhook, generating a PDF, or re-indexing search data inside the HTTP request makes the user wait for work they do not need yet. Move that work to a queue job and return the response. Laravel queues make this a small change:

SendOrderConfirmation::dispatch($order); // runs after the response, not during it

The same applies to work repeated on every request that could be cached: a config lookup, a reference list, or a count that changes rarely. Cache it with a short TTL and a clear invalidation point. If you cannot say when the cached value should change, that is a sign the cache will cause a stale-data bug later. Prefer a short TTL over a clever invalidation scheme you will not maintain.

Step 5: Confirm the fix with the same measurement

Run the same curl command from Step 1 against the same endpoint with the same parameters. Compare the number to your baseline. If it did not move, your fix addressed the wrong slice, and the timings from Step 3 will tell you which slice is still big. Repeat until the number is acceptable, then remove the temporary logging and deploy.

Write down what you found, even briefly. The next time a similar endpoint is slow, the first thing you will check is the thing that bit you last time. A short note in the repo or the incident channel turns a one-off debug session into team knowledge.

The takeaway

Debugging a slow API endpoint is a process, not a talent: reproduce and measure, check the database, time the parts, move non-essential work out of the request, and confirm with the same measurement. The method matters more than any single tool, because it stops you from fixing the wrong thing. Slow endpoints rarely have one cause, but they always have one biggest cause. Find it first.

Muhammad Aqib ← ALL ARTICLES
KEEP EXPLORING

More ideas.
Better engineering.

Explore the journal ↗