<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Engineering — InstiOne Blogs</title><description>How we build and run our products: Django, Postgres, React, Flutter and Cloudflare, with the code and the numbers.</description><link>https://blogs.instione.com/</link><language>en-in</language><item><title>I gave my Django app 13 times the CPU and it still held 10 users. The database was 15 ms away.</title><link>https://blogs.instione.com/load-testing-database-round-trips/</link><guid isPermaLink="true">https://blogs.instione.com/load-testing-database-round-trips/</guid><description>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.</description><pubDate>Tue, 03 Nov 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;The report said &lt;strong&gt;capacity 10 users&lt;/strong&gt; again.&lt;/p&gt;
&lt;h2 id=&quot;one-endpoint-not-the-whole-server&quot;&gt;One endpoint, not the whole server&lt;/h2&gt;
&lt;p&gt;The number was the same, but the table under it looked completely different.&lt;/p&gt;
&lt;table tabindex=&quot;0&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Users&lt;/th&gt;
&lt;th&gt;req/s&lt;/th&gt;
&lt;th&gt;read p95&lt;/th&gt;
&lt;th&gt;write p95&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;2.1&lt;/td&gt;
&lt;td&gt;470 ms&lt;/td&gt;
&lt;td&gt;761 ms&lt;/td&gt;
&lt;td&gt;pass&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;5.1&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;488 ms&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;4,700 ms&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;fail (writes)&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;11.6&lt;/td&gt;
&lt;td&gt;520 ms&lt;/td&gt;
&lt;td&gt;4,900 ms&lt;/td&gt;
&lt;td&gt;fail (writes)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;23.1&lt;/td&gt;
&lt;td&gt;570 ms&lt;/td&gt;
&lt;td&gt;1,900 ms&lt;/td&gt;
&lt;td&gt;pass&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;150&lt;/td&gt;
&lt;td&gt;32.6&lt;/td&gt;
&lt;td&gt;855 ms&lt;/td&gt;
&lt;td&gt;2,000 ms&lt;/td&gt;
&lt;td&gt;pass&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;160&lt;/td&gt;
&lt;td&gt;34.7&lt;/td&gt;
&lt;td&gt;1,300 ms&lt;/td&gt;
&lt;td&gt;2,300 ms&lt;/td&gt;
&lt;td&gt;fail (reads and writes)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;200&lt;/td&gt;
&lt;td&gt;40.8&lt;/td&gt;
&lt;td&gt;1,350 ms&lt;/td&gt;
&lt;td&gt;2,750 ms&lt;/td&gt;
&lt;td&gt;fail&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;390&lt;/td&gt;
&lt;td&gt;40.9&lt;/td&gt;
&lt;td&gt;9,850 ms&lt;/td&gt;
&lt;td&gt;9,200 ms&lt;/td&gt;
&lt;td&gt;fail, p99 &amp;gt; 10 s, stopped&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;h2 id=&quot;why-the-attendance-save-was-slow&quot;&gt;Why the attendance save was slow&lt;/h2&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;The fix was boring. Collect the changes in memory and write each kind in one statement.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot; data-language=&quot;python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;AttendanceRecord&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;objects&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;bulk_create&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;new_records&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
AttendanceRecord&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;objects&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;bulk_update&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    changed&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;status&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;note&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;updated_by&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;updated_at&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
AuditLog&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;objects&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;bulk_create&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;audit_rows&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;One gotcha is that &lt;code&gt;bulk_update&lt;/code&gt; skips &lt;code&gt;auto_now&lt;/code&gt;, so &lt;code&gt;updated_at&lt;/code&gt; 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.&lt;/p&gt;
&lt;h2 id=&quot;the-ceiling-was-waiting-not-working&quot;&gt;The ceiling was waiting, not working&lt;/h2&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;To check that, I ran the same breaking-point test with Postgres 1 ms away and nothing else changed.&lt;/p&gt;
&lt;figure&gt;
  &lt;img src=&quot;https://blogs.instione.com/images/load-testing/throughput-db-distance.svg&quot; alt=&quot;Requests per second against users: with the database 15 ms away throughput flattens near 42; with it 1 ms away it climbs to about 70&quot; width=&quot;800&quot; height=&quot;400&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; /&gt;
  &lt;figcaption&gt;Same code, same CPUs. Only the distance to the database changed.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;h2 id=&quot;three-bugs-that-only-showed-up-under-load&quot;&gt;Three bugs that only showed up under load&lt;/h2&gt;
&lt;p&gt;The load test was meant to measure speed. It also found three bugs that were already in production. All
three are fixed now.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Notifications for published results and certificates never arrived.&lt;/strong&gt; 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 &lt;code&gt;SET LOCAL&lt;/code&gt;, which lasts only until the
transaction ends. The notifications were written in a &lt;code&gt;transaction.on_commit&lt;/code&gt; 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.&lt;/p&gt;
&lt;p&gt;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
&lt;code&gt;transaction.on_commit&lt;/code&gt; directly.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Two staff admitting students at once got a 500.&lt;/strong&gt; 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.&lt;/p&gt;
&lt;pre class=&quot;language-sql&quot; data-language=&quot;sql&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sql&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;SELECT&lt;/span&gt; pg_advisory_xact_lock&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;hashtext&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;student_code&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; :branch_id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Publishing a test emailed every student inside the request.&lt;/strong&gt; 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.&lt;/p&gt;
&lt;h2 id=&quot;after-the-fixes&quot;&gt;After the fixes&lt;/h2&gt;
&lt;p&gt;Same profile, same seed, same breaking-point test.&lt;/p&gt;
&lt;figure&gt;
  &lt;img src=&quot;https://blogs.instione.com/images/load-testing/write-p95-before-after.svg&quot; alt=&quot;Write p95 per load step before and after the fixes: before, most steps sit near 4.9 seconds; after, they sit near 1 second up to 140 users&quot; width=&quot;800&quot; height=&quot;400&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; /&gt;
  &lt;figcaption&gt;Write p95 between 20 and 100 users dropped from 4.7–4.9 s to 0.9–1.1 s.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;ul&gt;
&lt;li&gt;Capacity went from &lt;strong&gt;10 to 140 users&lt;/strong&gt;, with every step up to 140 passing.&lt;/li&gt;
&lt;li&gt;Admissions day passed with zero errors and a write p95 of 1.3 s. Before, it failed with 7 server errors.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;li&gt;There were still zero errors all the way to 400 users.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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 🙂&lt;/p&gt;
</content:encoded><category>Engineering</category><category>load-testing</category><category>django</category><category>postgres</category><category>performance</category></item><item><title>I built a replica of production to load test my Django app. It held 10 users.</title><link>https://blogs.instione.com/load-testing-without-touching-production/</link><guid isPermaLink="true">https://blogs.instione.com/load-testing-without-touching-production/</guid><description>I copied production&apos;s CPU, memory and connection limits into Docker, drove it with Locust and per-kind p95 limits, and found the ceiling in 8 seconds.</description><pubDate>Sat, 31 Oct 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The server could serve about 10 users at once with free available resources initially. It’s not 10 requests per second. I meant 10 users, each
clicking something every 5 to 30 seconds. With 15 users, saving an attendance sheet took 4.6 seconds at p95. With 20 users it
took 11 seconds, and the test stopped itself due to OOM/Memory issues.&lt;/p&gt;
&lt;p&gt;This number came from the first load test I ran on a test server, a Django + DRF + Postgres app that schools
and coaching centres use for attendance, fees and results. This post is about how I got a number I
trust, covering the test setup, the test data, the scenarios and the pass/fail rules I configured. Part 2 is what happened when I
moved to a server with 13 times the CPU and the number didn’t change.&lt;/p&gt;
&lt;h2 id=&quot;why-not-just-point-locust-at-production&quot;&gt;Why not just point Locust at production&lt;/h2&gt;
&lt;p&gt;Production has real institutes on it. A breaking-point test is meant to break something, so I didn’t
want that something to be a real user experiencing in production.&lt;/p&gt;
&lt;p&gt;The usual alternative is a staging box. Mine would have been a different size, with a different database
and none of production’s network latency. A test on a bigger machine tells you how the bigger machine
behaves. So I set clear replication. &lt;strong&gt;The  test lab runs the same image as production, with production’s hard limits,
and nothing in it talks to a real outside service.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;At the time, production was a small hosted container with 0.15 of a CPU, 512 MB of memory, gunicorn with 2
workers × 4 threads, and a managed Postgres allowing 20 connections with free resources due to I don’t want to pay for infrastructure without actual users. So to confirm how much concurrency or breaking point of this system to find I rebuilt that in Docker Compose
inside a 2-core GitHub Codespace.&lt;/p&gt;
&lt;pre class=&quot;language-yaml&quot; data-language=&quot;yaml&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-yaml&quot;&gt;&lt;span class=&quot;token key atrule&quot;&gt;web&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;token key atrule&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token key atrule&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; ../..&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token key atrule&quot;&gt;dockerfile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Dockerfile &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# the production image&lt;/span&gt;
  &lt;span class=&quot;token key atrule&quot;&gt;cpus&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.15&lt;/span&gt;
  &lt;span class=&quot;token key atrule&quot;&gt;mem_limit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 512m
  &lt;span class=&quot;token key atrule&quot;&gt;memswap_limit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 512m           &lt;span class=&quot;token comment&quot;&gt;# no swap: going over is a real OOM kill&lt;/span&gt;
  &lt;span class=&quot;token key atrule&quot;&gt;restart&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; unless&lt;span class=&quot;token punctuation&quot;&gt;-&lt;/span&gt;stopped       &lt;span class=&quot;token comment&quot;&gt;# and a real restart, like the host does&lt;/span&gt;

&lt;span class=&quot;token key atrule&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;token key atrule&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; postgres&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;17&lt;/span&gt;
  &lt;span class=&quot;token key atrule&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;postgres&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;-&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; max_connections=20&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;-&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; shared_preload_libraries=pg_stat_statements&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token key atrule&quot;&gt;cpus&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;token key atrule&quot;&gt;mem_limit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 1g&lt;/code&gt;&lt;/pre&gt;
&lt;figure&gt;
  &lt;img src=&quot;https://blogs.instione.com/images/load-testing/lab.svg&quot; alt=&quot;Locust and a sampler drive a Docker Compose lab: Caddy, a web container capped at 0.15 CPU and 512 MB, and Postgres, Redis and object storage behind Toxiproxy&quot; width=&quot;800&quot; height=&quot;400&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; /&gt;
  &lt;figcaption&gt;The lab runs production&apos;s image and limits, with every outside service replaced, on one 2-core Codespace.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;A few details mattered more than I expected.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;memswap_limit&lt;/code&gt; equal to &lt;code&gt;mem_limit&lt;/code&gt;.&lt;/strong&gt; Without it Docker lets the container swap, and a memory
problem shows up as slowness instead of the OOM kill production would give you.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Toxiproxy in front of Redis (+40 ms) and object storage (+5 ms).&lt;/strong&gt; In production those were managed
services in another data centre. Latency you don’t model is latency you don’t measure.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Caddy for TLS.&lt;/strong&gt; Production cookies are &lt;code&gt;Secure&lt;/code&gt;, so plain HTTP would have failed every login.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A &lt;code&gt;tick&lt;/code&gt; container&lt;/strong&gt; that triggers the app’s scheduled jobs every 60 seconds, like the real cron.
Background work competes for the same 0.15 CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Email to Django’s in-memory backend, push notifications off.&lt;/strong&gt; A load test that sends 3,000 real
emails is a different kind of incident.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;test-data-and-resetting-it&quot;&gt;Test data, and resetting it&lt;/h2&gt;
&lt;p&gt;An empty database is always fast. So I seeded three institutes with 1,000 students each, plus staff, and
every one of them can log in. Seeding takes about 25 minutes, far too slow to repeat before every run.&lt;/p&gt;
&lt;p&gt;So what I did is saved the seed once as a Postgres template database, and each run starts from a fresh
&lt;code&gt;CREATE DATABASE ... TEMPLATE&lt;/code&gt; copy, which takes only seconds. I never run 2 inherits the attendance that run 1
marked, so two runs can actually be compared.&lt;/p&gt;
&lt;h2 id=&quot;scenarios-as-locust-users&quot;&gt;Scenarios as Locust users&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://locust.io/&quot;&gt;Locust&lt;/a&gt; describes users as Python classes. I wrote two classes. One is a student on the mobile app
(bearer token, 5 to 30 seconds between actions) and the other is a staff member on the web portal (session cookie and
CSRF, 3 to 15 seconds) similar to how the production works. Each task calls the same API endpoints the real clients call. A scenario is a mix
of those users plus a load shape. Eg. specific usecase of time period the load will increase like placing big friday sales order. In my usecase the below scenario are possible to happen.&lt;/p&gt;
&lt;table tabindex=&quot;0&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scenario&lt;/th&gt;
&lt;th&gt;What it simulates&lt;/th&gt;
&lt;th&gt;Shape&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;normal day&lt;/td&gt;
&lt;td&gt;10 staff + 100 students per institute&lt;/td&gt;
&lt;td&gt;steady, 20 min&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;check-in rush&lt;/td&gt;
&lt;td&gt;400 students of one institute check in within 5 min&lt;/td&gt;
&lt;td&gt;spike&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;results day&lt;/td&gt;
&lt;td&gt;a teacher publishes a test, 300 students open results&lt;/td&gt;
&lt;td&gt;spike&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;admissions day&lt;/td&gt;
&lt;td&gt;20 front-desk staff admit walk-ins with photos and receipts&lt;/td&gt;
&lt;td&gt;steady, 15 min&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;certificate day&lt;/td&gt;
&lt;td&gt;certificates issued, then downloaded as PDFs&lt;/td&gt;
&lt;td&gt;spike&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;breaking point&lt;/td&gt;
&lt;td&gt;the normal-day mix, +5 users every 2 minutes&lt;/td&gt;
&lt;td&gt;step, stops itself&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The breaking-point shape is a list of stages that Locust’s &lt;code&gt;LoadTestShape&lt;/code&gt; walks through.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot; data-language=&quot;python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Shape&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;StagedShape&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    stages &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; step&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;start&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; increment&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; step_s&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;120&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    STOP_ON_BREAK &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# errors &amp;gt; 5%, p99 &amp;gt; 10 s, OOM, or no DB connections left&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;FYI: &lt;code&gt;Locust&lt;/code&gt; is similar like &lt;code&gt;k6&lt;/code&gt; if you’re familiar with to test real user behaviour like scripted way.&lt;/p&gt;
&lt;p&gt;Next to Locust, a sampler reads the web container’s &lt;code&gt;cgroup v2&lt;/code&gt; files (the kernel files Docker uses to enforce a container’s CPU and memory limits) every few seconds. It tracks CPU used
against the quota, the share of scheduler periods it was throttled, memory, and OOM kills from &lt;code&gt;memory.events&lt;/code&gt;.
It also records Postgres connections and Redis commands. Also it reads the files from the host rather than
through &lt;code&gt;docker exec&lt;/code&gt;, so measuring doesn’t eat the 0.15 CPU it’s measuring. If Locust itself goes over
80% CPU, the report marks the run as unreliable due to we can’t trust the numbers.&lt;/p&gt;
&lt;h2 id=&quot;finding-what-too-slow-means-before-the-first-run&quot;&gt;Finding what “too slow” means before the first run&lt;/h2&gt;
&lt;p&gt;I wrote the limits down first, so I couldn’t move them after seeing the results. Every request name in
Locust starts with its kind, and each kind has its own p95 limit.&lt;/p&gt;
&lt;table tabindex=&quot;0&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Kind&lt;/th&gt;
&lt;th&gt;Examples&lt;/th&gt;
&lt;th&gt;p95 limit&lt;/th&gt;
&lt;th&gt;Errors&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;R&lt;/code&gt; read&lt;/td&gt;
&lt;td&gt;lists, home screen, results&lt;/td&gt;
&lt;td&gt;&amp;lt; 1 s&lt;/td&gt;
&lt;td&gt;&amp;lt; 1%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;W&lt;/code&gt; write&lt;/td&gt;
&lt;td&gt;check-in, attendance, payment&lt;/td&gt;
&lt;td&gt;&amp;lt; 2 s&lt;/td&gt;
&lt;td&gt;&amp;lt; 1%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;H&lt;/code&gt; heavy&lt;/td&gt;
&lt;td&gt;PDFs, photo uploads&lt;/td&gt;
&lt;td&gt;&amp;lt; 5 s&lt;/td&gt;
&lt;td&gt;&amp;lt; 1%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;One global p95 would let fast list pages hide a slow payment. Splitting by kind means a step fails if any
kind breaks its limit. &lt;strong&gt;Capacity&lt;/strong&gt; is the user count of the last step that passed before the first one
that failed. That rule is strict on purpose, because it matters in next part I’ll tell.&lt;/p&gt;
&lt;h2 id=&quot;the-result&quot;&gt;The result&lt;/h2&gt;
&lt;table tabindex=&quot;0&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Users&lt;/th&gt;
&lt;th&gt;req/s&lt;/th&gt;
&lt;th&gt;read p95&lt;/th&gt;
&lt;th&gt;write p95&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;1.4&lt;/td&gt;
&lt;td&gt;566 ms&lt;/td&gt;
&lt;td&gt;1,392 ms&lt;/td&gt;
&lt;td&gt;pass&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;2.2&lt;/td&gt;
&lt;td&gt;535 ms&lt;/td&gt;
&lt;td&gt;940 ms&lt;/td&gt;
&lt;td&gt;pass&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;15&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;3.6&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;865 ms&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;4,600 ms&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;fail (writes)&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;4.9&lt;/td&gt;
&lt;td&gt;2,000 ms&lt;/td&gt;
&lt;td&gt;11,208 ms&lt;/td&gt;
&lt;td&gt;fail, p99 &amp;gt; 10 s, stopped&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;figure&gt;
  &lt;img src=&quot;https://blogs.instione.com/images/load-testing/p95-first-server.svg&quot; alt=&quot;Read and write p95 against users on a log scale: writes cross the 2 second limit between 10 and 15 users, reads cross 1 second by 20&quot; width=&quot;800&quot; height=&quot;400&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; /&gt;
  &lt;figcaption&gt;Writes break first, going from 940 ms at 10 users to 4.6 s at 15.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;The sampler shows exactly why and when it’ll break. Eight seconds into the run, with only 5 users, the web container was using 98% of
its CPU quota and was throttled in 97% of scheduler periods. &lt;code&gt;cpus: 0.15&lt;/code&gt; gives a container 15 ms of CPU
time in every 100 ms period. A request that needs more than that waits for the next period, and the one
after. The whole server topped out at about 4 to 5 requests per second, which works out to roughly 30 to
40 ms of CPU per request. After that, requests queue, and the slowest endpoint in the mix shows it first then
saving an attendance sheet took 5.5 s at the median and 11 s at p95.&lt;/p&gt;
&lt;p&gt;The other scenarios also justified. Four of the five I ran hit the CPU wall within about 8 seconds. The fifth,
certificate day, hit memory first. The idle app already used 360 of its 512 MB, so rendering certificate
PDFs on first download and got workers OOM-killed due to memory spike, and by the last step 87% of requests were failing.
Also Admissions day, with photo resizing and receipt PDFs, had 4 OOM kills same reason due to memory spike.&lt;/p&gt;
&lt;p&gt;The database never showed up because of no.of allowed connection already within limit of breaking point. Only Postgres peaked at 10 of its 17 usable connections, and the most expensive
query in a whole run added up to 424 ms in total.&lt;/p&gt;
&lt;p&gt;So the answer looked clear where I need to do the next optimisation to increase the concurrency and application scalability. CPU came first, then memory for PDFs. I moved to a virtual machine with 2 ARM
cores and 4 GB for the web container, about 13 times the CPU, and ran the breaking-point test again.&lt;/p&gt;
&lt;p&gt;Surprisingly, It still said &lt;strong&gt;10 users.&lt;/strong&gt; Part 2 is about why 🥲&lt;/p&gt;
</content:encoded><category>Engineering</category><category>load-testing</category><category>django</category><category>locust</category><category>docker</category></item></channel></rss>