Performance and scale ·Build
The system is slow. Where, exactly?
A .NET API on PostgreSQL with four simulated causes
A page is slow, and everyone has their own suspicion about the cause. In production systems I have worked on, several fixes were applied at the same time, the page became faster, and nobody could say afterwards which fix had mattered, or which could have been left out. So I rebuilt the problem: an orders page on a .NET API and PostgreSQL, with four simulated causes. Then I changed one thing at a time and measured each change.
The page went from about a second to two milliseconds. Indexes did most of it. The second-largest cost was a regular expression nobody would have suspected, and the two defects every code review flags made no measurable difference.
- Problem
- a page is slow, everyone has their own suspicion about the cause, and several fixes are applied at the same time, so nobody can say which one mattered or which to keep.
- Deciding constraint
- causes combine, and what a fix is worth depends on what is already fixed, so only one change at a time, measured under load, tells them apart.
- Choice
- a baseline; each cause found with the tool that shows it; one change per step at 1, 10 and 50 users; then each flaw reintroduced in isolation into the fixed system.
- Result
- the page went from 1,053 ms to 2.0 ms at one user, and from 11,328 ms to 3.8 ms at ten, in nine steps. Indexes alone took it to 42 ms. The second-largest defect was one nobody would suspect, and two defects every code review flags made no measurable difference.
The missing indexes are left out of the chart: reintroduced in isolation, they made every request fail at 50 users.
The lab is public, with its tests and the command behind each of its figures: slow-endpoint-diagnosis.
If this is your situation
If users feel the system is slow but nobody can say where, start with a baseline and one look at the database's statement statistics, before anyone changes the suspected cause. In the lab, two of the seven flaws made no measurable difference when reintroduced in isolation; only measuring one change at a time shows which.
Performance and scalability → discuss a bottleneck
For your technical team How it was diagnosed, the options rejected and why, what went wrong, and the decision record.
Context and scope
An orders overview page, 25 orders with their lines and totals, filtered by territory and month, is slow, and slower the more people use it. The lab reproduces it: a .NET API with EF Core on PostgreSQL, over Microsoft's AdventureWorks sample data replicated twenty times, to 629,300 orders and 2.4 million order lines. Four causes are simulated behind switches, so each can be turned on or off alone: missing indexes, queries that fetch too much, service code, and database settings. The page returns the same answer in every combination; a test checks it.
In scope: finding which cause costs the time, proving a change helped, and the two concurrency cases that only appear under load. Out of scope: capacity planning, and front-end rendering. Assumption: the slowness can be reproduced outside production, on data of a realistic size.
Drivers
Constraints
- Several causes act at once, so the effect of one fix hides behind the others.
- What a fix is worth depends on what is already fixed.
- Averages hide what users feel under load; the tail matters as much as the median.
Scenarios
- When a cause is suspected, a tool shows it before anything is changed. Measured by the statement statistics, the plan and the CPU samples.
- When a change is made, the same load runs before and after. Measured by the median of three 30 s runs at 1, 10 and 50 users.
- When a gain is attributed to a fix, reintroducing only its flaw into the fixed system shows its contribution.
- When load rises, the connection pool and row locks are watched as well as the code. Measured by what the database connections waited on.
Options against those drivers
| Option | Tells you what mattered | Catches a regression | Time to first gain | Main drawback |
|---|---|---|---|---|
| Fix the most likely cause | No | No | Short | The next complaint |
| Add hardware | No | No | Shortest | The data grows and it returns |
| Measure, then one change at a time | Yes | Yes, the same day | Longer | Needs a reproducible case |
The decision and how it works
Four steps, in order: a baseline on the same machine and data each time; each cause found with the tool that shows it; one change per step, measured at 1, 10 and 50 users; and, at the end, each flaw reintroduced in isolation into the fixed system, because a fix's value depends on what is already fixed.
The database first. pg_stat_statements, under five users, put one statement, the lines of an
order, at 122,877 ms of the 127,000 ms the database spent. Its plan, from EXPLAIN (ANALYZE, BUFFERS), was a parallel sequential scan discarding 808,780 rows per worker; with an index on the
order id, the same lookup is an index scan of 0.09 ms instead of 45 ms. auto_explain wrote the
same plan from the server's side, which is how it would be detected in production.
Then the number of queries. The API counts its own statements: a page of 25 orders took 77, one for the page, three for each order and one for the totals. One query with joins took it to 2, and PostgreSQL's statistics counted the same 77 calls before.
Then the service code. dotnet-trace put 30.7% of the sampled thread time in regular-expression
frames: a compiled expression was built for every row. Built once, it disappeared from the profile.
Then the database settings, sized for the container.
| Step | Change | p50 at 1 user | p50 at 10 users |
|---|---|---|---|
| Baseline | all four causes active | 1,053 ms | 11,328 ms |
| + indexes | three indexes | 42.3 ms | 97.7 ms |
| + one query | joins instead of three queries per row | 14.7 ms | 35.7 ms |
| + columns | only what the page shows | 14.1 ms | 33.7 ms |
| + totals in SQL | count and sum in the database | 13.2 ms | 25.0 ms |
| + no blocking | async calls awaited | 13.1 ms | 24.5 ms |
| + one regex | compiled expression built once | 2.1 ms | 3.9 ms |
| + serializer | one shared options object | 2.1 ms | 3.9 ms |
| + settings | PostgreSQL sized for the container | 2.0 ms | 3.8 ms |
At 50 users the baseline answered in 55 s at 0.9 requests a second; the fixed system in 19 ms at 1,588.
Each cause by itself
The order of the fixes decides how much each one seems to contribute, so each flaw was then reintroduced in isolation into the fixed system (2.0 ms at one user, 18.8 ms at 50):
| Flaw reintroduced in isolation | p50 at 1 user | p50 at 50 users | Contribution |
|---|---|---|---|
| no indexes | 4,361 ms | every request failed | dominant |
| one query per row | 28.9 ms | 209 ms | large: 14x, 11x |
| whole entities, totals in memory | 3.2 ms | 61.6 ms | moderate alone, large under load (3.3x) |
| a compiled regex per row | 13.7 ms | 226 ms | large: 7x, 12x |
| blocking on async calls | 2.0 ms | 20.1 ms | none measured |
| new serializer options per response | 2.4 ms | 22.4 ms | within the noise |
| default database settings | 2.6 ms | 33.2 ms | small: 1.3x |
What only load shows
Two cases appear only when many requests run at once. Creating an order makes a 40 ms call to a pricing service; with a pool of ten connections and the call made inside the transaction, each connection sits idle for those 40 ms while 50 users queue for it. The arithmetic says ten connections held 40 ms each cannot serve much more than 250 requests a second, and the database agrees: its connections were waiting on the application, not working.
| Case, 50 users | p50 | p95 | Requests a second | What the connections waited on |
|---|---|---|---|---|
| Pricing call inside the transaction | 229 ms | 291 ms | 210 | the application (Client:ClientRead) |
| Pricing call before the transaction | 45.7 ms | 50.3 ms | 1,078 | |
| Customer balance row locked first | 391 ms | 1,213 ms | 102 | one row (Lock:tuple, Lock:transactionid) |
| Customer balance updated last | 47.9 ms | 72.7 ms | 969 |
Neither shows on one request on a quiet machine, and neither is a slow query: both are about how long something is held. When one tenant's heavy work slows the others, the same tail-first reading applies ([One customer slows everyone down: shared database, or one per customer?]).
How the change is made, and how it is undone
Each step is one switch or one migration, and the response is checked to be identical in every combination, so undoing a step is turning its switch back and running the same command. The order of the steps is a choice; the second table is what makes the attribution fair whatever the order.
How the decision is kept true
The measurement is code kept with the system: a load driver with seeded parameters, a script that sets each step and runs it, and reports that record the step, the settings, the machine and the versions with every figure. A change to the page runs the same matrix before it is accepted. The statement count per request is returned by the API itself, so a change that brings back one query per row shows up in the first run.
Where it went wrong, and the risks that remain
Two defects every review flags made no difference. Blocking on async calls grew the thread pool from 24 to 64 threads and raised lock contentions more than fivefold, and latency did not change while requests were fast. It showed only where requests were still slow: with only the indexes added, removing it took the median at ten users from 97.7 to 77.9 ms. A new serializer options object per response was within the noise. Both are still worth fixing; neither was the bottleneck.
The second-largest defect was one nobody would suspect. A regular expression compiled for every row cost 7x at one user and 12x at fifty, once the queries were fixed. Only a CPU profile found it.
A setting that was right for the normal case made the failure worse. With PostgreSQL sized for the container and the indexes removed, fifty concurrent sequential scans exceeded the container's 1.5 GB, the kernel killed a database process, and PostgreSQL restarted into recovery: every request failed. With the default settings the same case was only slow. Test the failure case with the settings you will run.
A gauge that was not there. The database driver's pool gauges could not be read from inside the process, so the evidence for the pool case is what the database's own sessions were waiting on. Know which tool shows each kind of wait before you need it.
Measuring first is also the lesson of an earlier manufacturing system where no improvement was measured before and after ([From customer order to raw materials: production planning for a manufacturing execution system]).
| Risk | Likelihood | Impact | Mitigation |
|---|---|---|---|
| No baseline, so gains and regressions cannot be proved | High | The same work is redone | Record the baseline before the first change |
| Several changes at once | High | No one knows which to keep | One change per step; each flaw reintroduced in isolation at the end |
| Measuring one request on a quiet machine | High | Pool and lock waits never appear | A concurrent load that reports p95, and the database's wait events |
| A baseline at one data size | Medium | A fix that held stops holding as data grows | Data at a realistic size; re-run at the larger size |
| Tuning for the normal case only | Medium | The failure case becomes worse | Run the failure case with the production settings |
Operations, cost and scale boundary
- To run: the load driver and the step script, each one command.
- To learn: reading a query plan, a CPU profile and the database's wait events.
- Cost: a full matrix of steps, users and repeats runs in hours, not minutes, so it runs before a release, not on every commit; the statement count runs on every build.
- Tested range: one laptop, PostgreSQL 17 in a container limited to 4 CPUs and 1.5 GB, 629,300 orders, 1, 10 and 50 users. The flaws were simulated, so a real system's mix will differ; the figures are about the method, not about any production system.
On a different stack
The order of work carries over, and the .NET tools are the same. The database tools differ: on SQL Server, Query Store holds the statement statistics, the actual execution plan shows the scan, and Extended Events record the waits. The lab tested PostgreSQL only.
Performance and scalability → discuss a bottleneck