Episode 18 showed you how to deploy with zero downtime, but a single server running 3 Gunicorn workers will hit its limit sooner or later: CPU saturates, requests queue up, and the statistics page gets slower and slower. This episode shows how to scale one step at a time — add workers, balance the load with Nginx, split off a read replica for PostgreSQL — and the signs that tell you when scaling is actually needed.
Step 1: Add Workers Before Adding Machines
Before buying another server, squeeze everything out of the one you have. The classic Gunicorn formula is two times the CPU count plus one, but that is only a starting point — measure the real CPU usage and response latency. If CPU still has headroom but requests are slow, the problem is usually a bad query or missing cache — go back to episode 12 and episode 9 before scaling.
# Check the real load before deciding to scaleuptimecurl -s http://localhost:8000/health/ -w "\n%{time_total}s\n"
Step 2: Load Balancing with Nginx
When one machine is no longer enough, run the app on two machines and let Nginx spread requests across them. Nginx has been the reverse proxy since episode 3 — now you only add servers to the upstream and pick a load distribution algorithm. least_conn sends each request to the machine with the fewest active connections, which suits heavy requests such as monthly statistics. One important note: the app must be stateless — sessions and file uploads must not live on local disk.
upstream expense_app { least_conn; server 10.0.0.11:8000; server 10.0.0.12:8000;}server { listen 80; server_name chi-tieu.example.com; location / { proxy_pass http://expense_app; proxy_set_header Host $host; }}
Step 3: A Read Replica for PostgreSQL
Most apps read far more than they write: transaction lists, monthly statistics, and reports are all reads. Split off a read replica to carry the read queries, while the primary database only handles writes. Django supports multiple databases out of the box: declare an extra replica in DATABASES, then use a small router to send reads to the replica while writes still go to the primary.
# settings.pyDATABASES = { "default": { "ENGINE": "django.db.backends.postgresql", "NAME": "expense", "HOST": "db-primary.internal", }, "replica": { "ENGINE": "django.db.backends.postgresql", "NAME": "expense", "HOST": "db-replica.internal", },}DATABASE_ROUTERS = ["expense.routers.ReadReplicaRouter"]
# expense/routers.pyclass ReadReplicaRouter: def db_for_read(self, model, **hints): return "replica" def db_for_write(self, model, **hints): return "default" def allow_migrate(self, db, app_label, model_name=None, **hints): return db == "default"
When Do You Actually Need to Scale?
Do not scale on a feeling. Scale when at least one clear signal persists for several days: CPU above 70% during peak hours, p95 latency double the normal level, or the database connection pool running dry even with PgBouncer from episode 4. Conversely, if errors only appear in one short window, treat it as unusual traffic or a single slow query. Every scaling step adds one more thing to monitor — only expand when the numbers from episode 8 prove you have hit the ceiling.
Apply It to Your Project
For the expense tracker app, start by measuring: record CPU, p95 latency, and database connections for one week. If CPU stays under 50%, do nothing yet — optimize slow queries first. When you need more, add workers, then move to two-machine load balancing; only split off a read replica when read queries are actually hammering the primary database.
Related Posts
- Django in Production #18: Zero-Downtime Deploys — Blue-Green/Rolling, Migration Order and Restarts
- Django Production Operations #4: PostgreSQL in Production — Connections, CONN_MAX_AGE, PgBouncer Pooling, Basic Backups
- Django Running in Production #9: Caching with Redis — Cache Framework, Invalidation Patterns, and Avoiding Stale Data
Frequently Asked Questions
How many Gunicorn workers are enough?
Start with two times the CPU count plus one, then measure the real numbers. Adding workers only helps when CPU still has headroom; if the database or a query is the bottleneck, more workers only make everything slower.
Does a read replica serve stale data?
Yes — replicas lag by tens of milliseconds to a few seconds. Never read freshly written data from a replica: after creating a new transaction, read it back from the primary database, or use using("default").
Does load balancing need sticky sessions?
No, as long as the app is stateless: sessions stored in Redis or the database, files on S3. Sticky sessions are only a temporary patch when the app still keeps state on local disk.
