Hitting the backend directly held ten thousand users. The real path broke at 1,800
Testa LMS is an online learning platform I built for a school. Before launch I wanted to know how many people it could really hold, so I used a laptop as the server and a second computer to simulate ten thousand people pressing on it. The morning’s results looked great. In the evening I changed the path, and it broke at 1,800 users.
How I tested
| Server | Load source | |
|---|---|---|
| Machine | MacBook (M1, 8 cores) | Windows desktop (Ryzen 5 5600) |
| Memory | 16 GB, 8 GB available to containers | 16 GB |
| Network | Wi-Fi | Wired |
| Running | Nginx, Spring Boot, PostgreSQL, one container each | k6 |
All the data was generated: 200 classes of 50 students. The ten thousand simulated people did not all do the same thing. They were spread in proportion: browsing courses, checking grades, posting in forums, handing in assignments with a 200 KB file, sitting a 20-question exam, filling in surveys, opening the app again and again, plus 200 teachers looking at the gradebook. Each person clicks, pauses a few seconds, and clicks again, so ten thousand people works out to 1,200 to 1,500 requests per second.
The load must come from a different machine. If the same laptop is both the server and the traffic source, what you measure is the two sides fighting over the processor.
Morning: hitting the backend directly, all fine
In the first round I had k6 connect straight to the backend port, skipping the Nginx in front. The reason was that Nginx rate-limits by source address, and a load test all comes from one machine, so it would be blocked as a single person.
The result: 1.57 million requests, 6 failures, a median response of 1.0 second, and no errors or restarts on the backend. I very nearly took those numbers and wrote the report.
The problem is that nobody in production connects straight to the backend.
Evening: through Nginx, broken at 1,800
To take the real path I first had to deal with the rate limit. On the test copy of Nginx I trusted X-Forwarded-For and had k6 send a different address for each simulated person. That setting exists only in the test environment. In production you must never trust the header like this.
Then, as the number of users reached about 1,800, 502s began to pour out: more than eight thousand of fifty-six thousand requests. The backend log was clean. It had never received those requests. The Nginx error log had this line:
connect() to 192.168.155.4:8080 failed (99: Address not available)
Address not available does not mean the backend refused the connection. It means Nginx itself could not open a new one: the local ports had run out.
The cause is that by default Nginx opens a new connection to the backend for every request and closes it afterwards. A closed connection does not release its port at once. It has to wait a while. At several hundred requests per second, every available port is taken within tens of seconds.
The fix is to have Nginx keep connections and reuse them:
upstream backend {
server server:8080;
keepalive 64;
}
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
I also raised worker_connections from the default 1,024 to 8,192. Otherwise ten thousand user connections could not even be accepted.
Rerun: 1.31 million requests, 0 502s.
This problem would never appear in the test that hit the backend directly, because there is no Nginx on that path. If the test takes a different path from production, what you measure is a different system.
Next: slow at ten thousand, but slow where?
After the fix, ten thousand users held, but a whole page took two to three seconds. My instinct was that there were not enough database connections (10 by default), so I raised it to 30 and ran another round. Responses were a little faster, but the laptop’s processor was less than 1% idle. The connection count was not the limit. The processor was, so I set it back to 10.
Since it was the processor, I needed to know where it was spending its time. I turned on JFR (Java’s built-in sampling profiler) on the backend and ran a round. One item stood out: 7% of processor time went to parsing exam paper JSON.
Each exam’s paper is a JSON column. The course page, the gradebook and the home page all read exams, and every time one was read, the whole paper was parsed into objects, even when the page only wanted to show the exam’s name.
The fix was to leave the column as a string and parse it only when someone really needs the paper:
public ExamSnapshot getSnapshot() {
if (snapshot == null && snapshotJson != null) {
snapshot = JSON.readValue(snapshotJson, ExamSnapshot.class);
}
return snapshot;
}
The same profile found two more places: within one request, a course’s exam list was queried several times, and when looking up one person’s exam roster, more rows were fetched than needed. Both were changed to query once, and only for that course.
The three times I measured wrong
This part is more worth recording than the fixes themselves.
First: comparing times from different rounds. After fixing the paper parsing I reran ten thousand users and it was slower than before the fix. For a moment I thought I had broken it. In fact every round adds 200 exams and tens of thousands of rows to the database, so later rounds are slower anyway. Comparing round six with round two compares data volume.
So I changed how I measured: a fixed 150 requests per second for 60 seconds, reading the processor time the containers used and dividing by the number of requests. That number depends far less on data volume and on how long the queue is at that moment:
| Version | Backend | Database |
|---|---|---|
| Before the fixes | 2.43 ms | 1.72 ms |
| Paper parsed only when used | 2.03 ms | 1.74 ms |
| Plus the leaner queries | 1.83 ms | 1.45 ms |
Each request costs about 21% less. For the ten-thousand-user comparison I deliberately ran a pair: the version before the fixes first, then the one after. The later run has more data, so the measured difference can only be smaller than the real one.
Second: running the wrong version. For the comparison round I used a backend built from another branch, which lacked a few of the pages the test script calls. Of 1.2 million requests, 290,000 returned 404, and 404s come back very fast, so the median “improved” by twenty percent. I only noticed when I looked at the failure count. That whole round was thrown away and rerun.
Third: guessing the cause. Before profiling, I looked at the course page’s code and guessed it was “one database query per exam”. It was not. There was only one query, and the slow part was the parsing afterwards. Had I changed things according to my guess, I would have changed something that had no problem.
The final numbers
Ten thousand users, through Nginx, with more data in the round after the fixes than in the round before:
| Before | After | |
|---|---|---|
| Median response | 2.44 s | 2.19 s |
| Whole page | about 3.0 s | about 2.55 s |
| Failures | 8 | 7 |
| Server errors | 0 | 0 |
At 6,000 users the median response is 0.17 seconds and a whole page takes under half a second. In other words, the range where it is really smooth goes up to about 6,000 users. Above that it “does not break, but you wait”.
These are numbers from a laptop on Wi-Fi. They are not the system’s ceiling and cannot be used to predict a production host. On a real server everything has to be measured again, and settings such as connection counts retuned to the number of cores.
Still unsolved
At ten thousand users, 5 to 11 assignment uploads per round are cut off midway, about three to five in a thousand uploads. The server has no matching error. Hitting a quiet server alone with nearly 6,000 uploads in a row, not one failed. It may be the Wi-Fi on the server side dropping packets under many connections, or connections being dropped under full load. I have no evidence yet to tell the two apart, so it stays on the to-do list and is not written up as “solved”.
What I learned
- A test has to take the path users take. A test that bypasses the proxy is convenient, but it cannot test the proxy.
- Before comparing, confirm only one thing differs. Data volume, version, path: if any of them differs, the numbers cannot be compared.
- Read failures and response time together. Errors that return quickly make the average look good.
- Measure before tuning. The connection pool was the first thing I wanted to tune, and it was not the problem at all.
- If you do not know the cause, write that you do not know. For the interrupted uploads, “probably the network” would look better, but it would only be a guess.
— small R, 2026.11