Django gives you a lot for free: the ORM, sessions, CSRF protection, the admin. It also makes it easy to write a view that runs 300 queries without noticing. This guide covers how to set up a Django app for a meaningful load test, how to handle CSRF, and the bottlenecks that show up most often.

Test the production setup, not runserver

manage.py runserver is a development server. It is not built for concurrency, and a load test against it measures the wrong thing. Test the stack you deploy:

  • gunicorn with sync or threaded workers, or uvicorn (or gunicorn with uvicorn workers) for ASGI;
  • DEBUG = False. With debug on, Django keeps every SQL query in memory for each request, and it gets slower and bigger the longer the test runs;
  • static files served by Nginx, WhiteNoise or a CDN, not by Django;
  • Django Debug Toolbar and Silk off, or at least not recording.

Size the workers

With gunicorn sync workers, each worker handles one request at a time. If you have 4 workers and each request takes 250 ms, the server tops out at about 16 requests per second, however many CPUs sit idle. A common starting point is 2 × CPU cores + 1 workers. Threaded workers (--threads) help when views wait on I/O.

ASGI with uvicorn helps only if your views are actually async. A sync view on an async server still blocks a thread. Measure both setups with the same test before you switch.

Handle CSRF and sessions

Django protects POST forms with a CSRF token. A realistic test does what a browser does:

  1. GET /accounts/login/, and capture the csrfmiddlewaretoken value from the form (the csrftoken cookie is set too);
  2. POST the username, password and token, with the Referer header set (Django checks it on HTTPS), and keep the sessionid cookie;
  3. continue with the same cookies. For later AJAX POSTs, send the token in the X-CSRFToken header.

For APIs built with Django REST Framework and token or JWT auth, sign in once per virtual user, capture the token and send it on every request.

Use a dataset of test accounts so each virtual user is someone different, and seed realistic data volumes.

The usual bottlenecks

N+1 queries. A template that loops over 50 orders and prints order.customer.name runs 51 queries. Fix with select_related('customer') for foreign keys and prefetch_related('items') for many-to-many and reverse relations. DRF serializers with nested fields are a common hiding place. django-debug-toolbar or nplusone catch these during development.

Database connections. By default Django opens a new connection for every request (CONN_MAX_AGE = 0). Under load that is slow, and PostgreSQL connections are expensive. Set CONN_MAX_AGE to keep connections open, or use Django's built-in pool (Django 5.1+ with psycopg 3), or put PgBouncer in front. Then count: 4 servers × 9 workers × threads per worker must fit inside PostgreSQL's max_connections.

Missing indexes. Fields used in filter(), order_by() and joins on big tables need an index. Check slow queries with EXPLAIN, or queryset.explain().

No cache, or the wrong one. The default local-memory cache is per process, so each worker has its own copy. Use Redis or Memcached for shared caching, and cache expensive fragments or querysets. Keep sessions out of the database if it is already busy: the cache or cached_db backend helps.

Slow work in the request. Sending email, resizing images or calling a slow API ties up a worker. Move it to Celery (or another task queue). Then load test with the workers running, and watch queue length: if tasks pile up, users will notice later, even if the request was fast.

Read the results per step

Look at each step separately. If the product list slows down while login stays flat, look at that view's queries, not the server. Compare p95 per step with a baseline run, and re-run the same test after each fix.

Django with Hoofbeat

Describe the journey in HoofbeatChat ("log in to my Django app, open orders, filter by status"), or import a HAR recording of yourself clicking through it (how). Add a capture for csrfmiddlewaretoken on the login page and reuse it in the POST. Hoofbeat keeps cookies per virtual user and can read accounts from a dataset. Connect Prometheus to see CPU, database connections and Celery queue length next to latency, and let the AI diagnosis point at the likely cause.