Not all of the work behind an AI product is AI. The operator platform — a
multi-tenant NestJS backend — was buckling under load well before the model was
the issue. This case is a straight backend-performance diagnosis: find why
latency became unstable, identify the cause and correct the path.
The platform served creditors and operators: campaigns, subjects, negotiations,
conversation history and billing, all tenant-isolated. Under real traffic
(campaign indexing, dashboards refreshing) latency spiked and the service
felt unpredictable. The instinct to "add more instances" would have masked the
real cause and raised the bill.
Three compounding causes, none of them obvious from a single trace:
Connection-pool starvation. The ORM ran with a tiny default connection
limit while the runtime accepted far more concurrent requests. Most requests
spent their time waiting for a connection, not doing work. The math was silent
until load.
Missing indices. Status-filter queries scanned more data than the hot
path needed because the relevant columns were not indexed.
Repeated lookups. The same tenant UUID → internal ID resolution ran many
times per second across requests.
The three symptoms reinforced one another: requests waited for scarce
connections, then used those connections for avoidable scans and repeated
lookups. Scaling instances would have multiplied the contention rather than
removing it.
I diagnosed by comparing actual runtime concurrency to the configured pool size,
then read the slow queries instead of guessing. The fixes were small and
surgical:
Sized the pool to the workload and capped runtime concurrency to match, so
requests stop starving each other.
Added the missing indices on the status columns driving the hot filters,
turning sequential scans into index scans.
Cached the repeated tenant-ID resolution to cut redundant round-trips.
The correction came from configuration and indices — not a rewrite.
The most expensive production problems are often the cheapest to fix once you can
see them. Measuring the path — pool wait vs. query time vs. work — turned a vague
"it's slow" into three precise, small changes with a reviewable result.