I gave my Django app 13 times the CPU and it still held 10 users. The database was 15 ms away.
One slow write tripped the p95 rule, 15 ms database round trips capped throughput at 42 req/s, and the load found three bugs. After the fixes, 140 users.
In part 1 the first server topped out at about 10 users, and the reason was easy to see. The container had 0.15 of a CPU and was throttled 97% of the time. So I moved to a virtual machine with 2 ARM cores and 4 GB for the web container, gunicorn with 3 workers × 4 threads, Redis on the same machine, and Postgres as a managed database about 15 ms away. That’s about 13 times the CPU.
I copied that setup into the lab the same way as before, with Toxiproxy adding 15 ms to every database call, and ran the same breaking-point test, this time adding 10 users every 2 minutes.
The report said capacity 10 users again.
One endpoint, not the whole server
The number was the same, but the table under it looked completely different.
| Users | req/s | read p95 | write p95 | Result |
|---|---|---|---|---|
| 10 | 2.1 | 470 ms | 761 ms | pass |
| 20 | 5.1 | 488 ms | 4,700 ms | fail (writes) |
| 50 | 11.6 | 520 ms | 4,900 ms | fail (writes) |
| 100 | 23.1 | 570 ms | 1,900 ms | pass |
| 150 | 32.6 | 855 ms | 2,000 ms | pass |
| 160 | 34.7 | 1,300 ms | 2,300 ms | fail (reads and writes) |
| 200 | 40.8 | 1,350 ms | 2,750 ms | fail |
| 390 | 40.9 | 9,850 ms | 9,200 ms | fail, p99 > 10 s, stopped |
Reads stayed under 1 second all the way to 150 users. There were zero errors up to 390 users, with no OOM kills and no restarts. Across every run on this setup, memory never went above 863 MB of 4 GB. What failed at 20 users was the write p95, and the slowest-endpoint list made it obvious that one request was doing it. Saving an attendance sheet took 2.2 s at the median and 8.3 s at p95.
This is the strict rule from part 1 doing its job. Capacity is the last step before the first failure, so a single slow endpoint can hold the whole number down. I could have relaxed the rule and reported 150. I’d rather fix the endpoint, because the teacher saving that sheet is a real person waiting 8 seconds.
Why the attendance save was slow
The save went through the class one student at a time. For a new record it looked up the tenant and inserted the row. For a corrected record it saved the row, looked up the tenant again and wrote an audit row. That’s 2 to 3 round trips to Postgres per student.
With the database 1 ms away, nobody would notice. At 15 ms it adds up quickly. A class of 40 is 80 to 120 round trips, so 1.2 to 1.8 seconds of pure network waiting before Postgres does any real work. Put a few teachers on it at once and the p95 goes where you saw it.
The fix was boring. Collect the changes in memory and write each kind in one statement.
AttendanceRecord.objects.bulk_create(new_records)
AttendanceRecord.objects.bulk_update(
changed, ["status", "note", "updated_by", "updated_at"]
)
AuditLog.objects.bulk_create(audit_rows)
One gotcha is that bulk_update skips auto_now, so updated_at has to be set by hand. The tenant is now set on each object when it’s built, rather than fetched per row. A query-count test
pins the number of statements so it can’t creep back up for bigger classes.
The ceiling was waiting, not working
The breaking-point run had a second, more interesting result. From about 200 users on, throughput stopped at roughly 42 requests per second, whatever the load. If CPU were the limit, I’d expect the CPU to be pinned at that point. It wasn’t.
At the ceiling the web container used 40 to 67% of its 2 CPUs, with zero throttling. Postgres connections sat flat at 12, which is exactly the number of gunicorn threads (3 × 4). Every thread had a connection open and was waiting on it.
Little’s law makes it concrete. 12 threads at 42 requests per second means each request holds a thread for about 285 ms on average. Most of that is round trips at 15 ms each, because several pages ran dozens of queries. The server wasn’t busy. It was waiting for the network.
To check that, I ran the same breaking-point test with Postgres 1 ms away and nothing else changed.
Capacity went from about 150 (ignoring the attendance save) to 310 users, and the ceiling from 42 to about 70 requests per second. At that point the web CPU was at 85 to 91% and being throttled. That’s the bottleneck you want, because it means the machine is doing actual work. One honest caveat is that the lab’s database had its own CPU. On a real single box Postgres would share the same 2 cores, so the real number would be somewhat lower.
Three bugs that only showed up under load
The load test was meant to measure speed. It also found three bugs that were already in production. All three are fixed now.
Notifications for published results and certificates never arrived. In the results-day run, 2,394
in-app notification inserts failed and none succeeded. InstiOne keeps every institute’s rows apart with
Postgres row-level security. Each request sets its tenant with SET LOCAL, which lasts only until the
transaction ends. The notifications were written in a transaction.on_commit callback, which runs after
the commit. By then the tenant was gone, so the policy refused every insert. A fallback then retried each
notification one by one, which is also why publishing a test took about 95 seconds in a single request.
The fix captures the tenant when the callback is registered and sets it again for exactly as long as the
callback runs, then clears it so nothing leaks to the next request on that connection. Every post-commit
callback in the app now goes through that one wrapper, and a test fails if anyone calls
transaction.on_commit directly.
Two staff admitting students at once got a 500. With 20 front-desk staff in the admissions-day run, 7 of 103 admissions failed. The next student number was the highest existing one plus one, read without a lock. Two requests read the same highest number, both tried to insert it, and the second hit the unique constraint. The fix is a transaction-scoped advisory lock per branch, so admissions in the same branch take turns for that one read and insert, while other branches don’t wait at all.
SELECT pg_advisory_xact_lock(hashtext('student_code'), :branch_id);
Publishing a test emailed every student inside the request. Queueing each email cost about 7 queries, so after the first fix, publishing results for about 300 students still took 62 seconds. Now the request queues all the emails in a fixed number of queries, whatever the class size, and a job that runs every minute sends them. This change landed after the last run, so I don’t have a measured number for it yet.
After the fixes
Same profile, same seed, same breaking-point test.
- Capacity went from 10 to 140 users, with every step up to 140 passing.
- Admissions day passed with zero errors and a write p95 of 1.3 s. Before, it failed with 7 server errors.
- Results day had zero failed notification inserts, down from 2,394, and publishing dropped from about 95 s to 62 s before the email change.
- There were still zero errors all the way to 400 users.
The throughput ceiling didn’t move. It went from about 42 to about 43 requests per second, because the fixes removed slow paths but not the 15 ms on every query. Later I moved Postgres onto the same machine as the app, which is the 310-user setup from the chart, with backups to object storage every night.
One more caveat. The lab runs on x86 and production runs on ARM. Since the limit at 15 ms was network waiting, not CPU, I expect the 140 to hold. The 310 is CPU-bound, so I still want to confirm it against the real machine.
The CPU was never the whole story. Once it stopped being the limit, the cost was the distance to the database multiplied by how many times each request crossed it 🙂
Get new posts by email
Engineering and business notes from building software in India. Unsubscribe from any email.
We email you a link to confirm. See privacy.
Prefer WhatsApp? Follow our WhatsApp channel (opens in a new tab) for new posts and short tech updates.