performance · backend · ecowell · database · testing · security

Why Our API Was Slow Despite Low CPU Usage

23 November 2026 · 7 min read

Status of this article. The “before” numbers below are real measurements. The “after” section is deliberately empty: we have designed the optimisations but have not yet measured them, and we would rather publish a table with blanks than invent results. The template to fill is at the end.

A relaxed capybara beside an idle engine while requests wait in a long thin pipe to the database

Before: many calls took 6.7 seconds. After: one aggregated cached call took about 150 ms.

The symptom

A Node and Express API on a small instance (0.5 vCPU, 512 MB, one instance) in Singapore, talking to a managed Postgres service over HTTPS. Users reported that screens “felt slow”. Nothing looked busy: the instance was not pinned, nobody was waiting on a queue, and the database held only 34 MB.

What we measured

Client in India, service in Singapore, one run against the live environment. Sequential requests unless stated; p50 and p95 are per endpoint.

Request Samples p50 p95 Payload
GET /health (no database) 60 82 ms 169 ms 0.1 KB
GET /health/db (database ping) 40 348 ms 604 ms <0.1 KB
GET /api/me (authenticated) 30 346 ms 484 ms 0.8 KB
GET dashboard/branches 30 475 ms 675 ms 0.3 KB
inventory stock, 100 rows 30 733 ms 954 ms 34.6 KB
inventory batches, 100 rows 30 793 ms 928 ms 38.8 KB
inventory items, 100 rows 30 849 ms 1,070 ms 25.0 KB
barcode scan lookup 20 922 ms 1,115 ms 0.1 KB
branch-live feed, 50 events 20 1,234 ms 1,628 ms 8.3 KB

Concurrency, bounded (this is a shared environment, so we kept it small):

Test Requests Workers Throughput p50 / p95
/health 300 10 103 req/s 79 / 169 ms
/api/me 120 8 19.5 req/s 367 / 615 ms
inventory items list 60 5 4.6 req/s 1,063 / 1,401 ms
/health/db 100 5 5.0 req/s 993 / 1,269 ms

About 870 requests in total, all HTTP 200.

First clue: the baseline is the network

/health does no work and answers in about 82 ms. That is the round trip from the test machine to the service, so roughly 80 ms of every figure above is distance, not code. Subtracting it:

  • /api/me: about 265 ms of server time for returning 0.8 KB.
  • A 100-row list: about 650 to 770 ms.
  • Branch live: about 1.15 s.

A request that returns less than a kilobyte should not take a quarter of a second of server time on a machine that is not busy. Something is waiting.

Second clue: latency did not grow with concurrency

If the CPU were the bottleneck, eight simultaneous requests would queue behind each other and the median would rise sharply. It barely moved:

  • /api/me sequential p50: 346 ms. At 8 workers: 367 ms.

Throughput scaled almost linearly with workers (about 8 divided by 0.37 s, or roughly 21 req/s, observed 19.5). That is Little’s law: throughput equals concurrency divided by latency. It is the signature of a system that spends its time waiting on I/O, not computing. (Honest caveat: we inferred “not CPU-bound” from this behaviour; we did not export the instance’s CPU metric during the test. Adding that graph is step zero of any next experiment.)

The request path

What happens for one authenticated GET /api/v1/inventory/items:

browser -> proxy -> API instance
   1. verify the access token         -> HTTPS call to the auth service      (~60-150 ms)
   2. load the employee profile       -> HTTPS call to the database gateway  (~60-150 ms)
   3. resolve the allowed branches    -> HTTPS call (branch scope)           (~60-150 ms)
   4. run the actual query            -> HTTPS call                          (~60-150 ms)
   5. fetch related rows (stock etc.) -> HTTPS call(s)
   6. serialise and respond

Each step is cheap in CPU and each waits for a network round trip to a different host. Four or five of them in sequence is where 300 to 800 ms comes from. The API talks to the database through an HTTPS gateway (REST and RPC) rather than a pooled SQL connection, so every query pays a full request round trip, TLS and gateway overhead included.

The pattern generalises: slow with low CPU almost always means serial waiting, either on the network, on locks, or on a downstream service. CPU graphs will look healthy while users wait.

Why this matters for capacity

With the measured latency, one instance sustains about 15 to 30 mixed requests per second, because each in-flight request spends most of its life parked. More concurrency helps until the instance runs out of CPU (our estimate: roughly 50 to 100 req/s for light reads on 0.5 vCPU). But latency per user stays the same however many users you add, and for interactive screens that is the number people feel.

The plan (and why we ordered it this way)

Ordered by expected gain per unit of risk:

  1. Cache identity and scope. The employee profile and the branch list change rarely. Cache them in-process for 30 to 60 seconds, keyed by user. This removes two of the four sequential calls on almost every request. Risk: a revoked user stays valid for up to the TTL; mitigate by invalidating on revoke and keeping the TTL short for privileged roles.
  2. Verify the JWT locally against the provider’s published keys instead of calling the auth service on every request. Removes another round trip. Risk: revocation latency again; same mitigation.
  3. Run independent calls in parallel. Where a handler fetches stock and batches and items separately, Promise.all them. Cost-free when the calls do not depend on each other.
  4. Batch lookups. Replace per-row follow-up queries with a single IN (...) query. (We already do this in places, for example when attaching stock holds to a page of jobs.)
  5. Compress responses. Lists are 25 to 39 KB of JSON that typically compresses 70 to 85 percent. Helps users on slow links more than server time.
  6. Move to pooled direct SQL in the same network as the database (the AWS plan). In-VPC round trips are 1 to 3 ms instead of 60 to 150 ms. This is the large structural win and the reason we expect the biggest gains from the migration itself, but it is also the largest change, so steps 1 to 5 come first and are measurable on today’s stack.
  7. Make rate limits shared. Our limiters are in memory per instance. Before running more than one instance, move them to a shared store, or the effective limit multiplies with instance count.

Projection (not a measurement): steps 1 to 3 should remove two or three of the four or five sequential round trips on typical requests, which, at 60 to 150 ms each, suggests light reads falling from about 270 ms to something like 100 to 150 ms of server time on the current stack. Direct in-VPC SQL should push light reads toward 30 to 60 ms and 100-row lists toward 60 to 120 ms. Treat every one of these numbers as a hypothesis until the table below is filled.

How we will measure it, so the result is believable

  1. Same client, same network path, same time of day, run before and after each change in isolation (one optimisation per experiment).
  2. Warm up, then at least 30 sequential samples per endpoint; report p50 and p95, never only the mean.
  3. A bounded concurrency run (for example 8 workers) to confirm that latency, not CPU, was the limit, and to find the next limit.
  4. Record CPU and memory of the instance during the run, so “low CPU” is shown rather than inferred.
  5. Count database round trips per request (log them) and report that alongside latency. If the count drops from 5 to 2 and latency drops accordingly, the story is proven.
  6. Publish failures too: errors, timeouts, and any change that did not help.

Results table to publish after measuring

Request Before p50 / p95 After caching (1+2) p50 / p95 After parallel + batch (3+4) After direct SQL (6) Round trips before / after
/api/me 346 / 484 ms to measure to measure to measure to log
inventory items (100) 849 / 1,070 ms to measure to measure to measure to log
inventory stock (100) 733 / 954 ms to measure to measure to measure to log
branch live (50) 1,234 / 1,628 ms to measure to measure to measure to log
/api/me at 8 workers 19.5 req/s to measure to measure to measure n/a

Takeaways

  • Low CPU plus high latency means waiting, not computing. Look at the request path, not the CPU graph.
  • Subtract the network baseline (a no-op endpoint) before blaming your code.
  • Latency that does not rise with concurrency is a strong sign of I/O waits, and Little’s law gives you a quick sanity check on throughput.
  • Count round trips. It is the cheapest metric that predicts the most.
  • Publish measurements, not guesses, including the empty cells for what you have not measured yet.

AI use: Written with AI assistance from my own notes and project facts, then reviewed by me. Images are AI-generated. How I use AI

Loading reactions…

Working on something similar? Let's talk.

Same problem, a question about this article, a collaboration, or an open-source project you would like me to contribute to. Pick whichever door you like: