RSS
Engineering

I built a replica of production to load test my Django app. It held 10 users.

I copied production's CPU, memory and connection limits into Docker, drove it with Locust and per-kind p95 limits, and found the ceiling in 8 seconds.

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.

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.

Why not just point Locust at production

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.

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. The test lab runs the same image as production, with production’s hard limits, and nothing in it talks to a real outside service.

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.

web:
  build: { context: ../.., dockerfile: Dockerfile }   # the production image
  cpus: 0.15
  mem_limit: 512m
  memswap_limit: 512m           # no swap: going over is a real OOM kill
  restart: unless-stopped       # and a real restart, like the host does

db:
  image: postgres:17
  command: [postgres, -c, max_connections=20,
            -c, shared_preload_libraries=pg_stat_statements]
  cpus: 1
  mem_limit: 1g
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
The lab runs production's image and limits, with every outside service replaced, on one 2-core Codespace.

A few details mattered more than I expected.

  • memswap_limit equal to mem_limit. Without it Docker lets the container swap, and a memory problem shows up as slowness instead of the OOM kill production would give you.
  • Toxiproxy in front of Redis (+40 ms) and object storage (+5 ms). In production those were managed services in another data centre. Latency you don’t model is latency you don’t measure.
  • Caddy for TLS. Production cookies are Secure, so plain HTTP would have failed every login.
  • A tick container that triggers the app’s scheduled jobs every 60 seconds, like the real cron. Background work competes for the same 0.15 CPU.
  • Email to Django’s in-memory backend, push notifications off. A load test that sends 3,000 real emails is a different kind of incident.

Test data, and resetting it

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.

So what I did is saved the seed once as a Postgres template database, and each run starts from a fresh CREATE DATABASE ... TEMPLATE copy, which takes only seconds. I never run 2 inherits the attendance that run 1 marked, so two runs can actually be compared.

Scenarios as Locust users

Locust 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.

Scenario What it simulates Shape
normal day 10 staff + 100 students per institute steady, 20 min
check-in rush 400 students of one institute check in within 5 min spike
results day a teacher publishes a test, 300 students open results spike
admissions day 20 front-desk staff admit walk-ins with photos and receipts steady, 15 min
certificate day certificates issued, then downloaded as PDFs spike
breaking point the normal-day mix, +5 users every 2 minutes step, stops itself

The breaking-point shape is a list of stages that Locust’s LoadTestShape walks through.

class Shape(StagedShape):
    stages = step(start=5, increment=5, step_s=120, steps=60)
    STOP_ON_BREAK = True  # errors > 5%, p99 > 10 s, OOM, or no DB connections left

FYI: Locust is similar like k6 if you’re familiar with to test real user behaviour like scripted way.

Next to Locust, a sampler reads the web container’s cgroup v2 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 memory.events. It also records Postgres connections and Redis commands. Also it reads the files from the host rather than through docker exec, 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.

Finding what “too slow” means before the first run

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.

Kind Examples p95 limit Errors
R read lists, home screen, results < 1 s < 1%
W write check-in, attendance, payment < 2 s < 1%
H heavy PDFs, photo uploads < 5 s < 1%

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. Capacity 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.

The result

Users req/s read p95 write p95 Result
5 1.4 566 ms 1,392 ms pass
10 2.2 535 ms 940 ms pass
15 3.6 865 ms 4,600 ms fail (writes)
20 4.9 2,000 ms 11,208 ms fail, p99 > 10 s, stopped
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
Writes break first, going from 940 ms at 10 users to 4.6 s at 15.

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. cpus: 0.15 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.

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.

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.

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.

Surprisingly, It still said 10 users. Part 2 is about why 🥲

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.

Keep reading