Locust Distributed Load Testing: Scale to Millions of Users
Locust distributed mode uses a master-worker architecture: one master coordinates the test, multiple workers generate load. This guide covers starting master and worker processes, distributing users across workers, running workers on separate machines, and troubleshooting common distributed test failures.
A single machine running Locust can generate thousands of concurrent users before hitting CPU limits. But to simulate realistic production load—tens of thousands of simultaneous users—you need distributed mode. Locust's master-worker architecture makes this straightforward: one process coordinates the test, any number of worker processes generate the actual HTTP traffic.
This guide walks through setting up distributed Locust tests, from two processes on one machine to workers distributed across a network.
How Locust Distributed Mode Works
Locust uses a primary-replica model:
- Master: Runs the Locust web UI, accepts user count and spawn rate from the operator, and distributes users evenly across workers. The master does not generate load itself.
- Workers: Connect to the master via ZeroMQ sockets, receive their user allocation, and execute the locustfile. Each worker generates HTTP requests independently.
The master aggregates stats from all workers in real time. From the UI perspective, you see one unified view of RPS, response times, and failures even if you have 50 workers spread across 10 machines.
Starting a Basic Distributed Test
Step 1: Start the Master
locust --master --host https://api.example.comThe master starts on port 8089 (web UI) and port 5557 (worker communication). Open http://localhost:8089 — you'll see the standard Locust UI, but it shows "0 workers" until workers connect.
Step 2: Start Workers
On the same machine (or any machine that can reach the master):
locust --worker --master-host localhostStart as many workers as you have CPU cores available. Each Locust worker is single-threaded but uses gevent for cooperative concurrency, so one worker can handle hundreds of simulated users efficiently. A good starting point: one worker per CPU core.
# Start 4 workers in the background
for i in {1..4}; do
locust --worker --master-host localhost &
doneThe master UI immediately shows "4 workers connected."
Step 3: Run the Test
Set your total user count and spawn rate in the UI and click Start. Locust distributes users evenly: with 4 workers and 1000 users, each worker gets 250.
Configuring Master-Worker Communication
By default, the master listens on all interfaces (0.0.0.0) on ports 5557 (ZeroMQ) and 8089 (web). You can customize:
# Master with custom ports
locust --master \
--master-bind-host 0.0.0.0 \
--master-bind-port 5557 \
--web-port 8089
# Worker connecting to a specific master
locust --worker \
--master-host 192.168.1.10 \
--master-port 5557For remote workers to reach the master, ensure port 5557 is open in your firewall. The web UI port (8089) only needs to be accessible from your browser—workers don't use it.
Running Workers on Remote Machines
Option 1: SSH and tmux
The simplest approach for small clusters:
# On worker machine
ssh user@worker-1
cd /path/to/tests
locust --worker --master-host 192.168.1.10 --locustfile locustfile.py
# Repeat for worker-2, worker-3...Keep the locustfile identical on all machines. Locust doesn't sync code between master and workers—each worker runs its local copy of the locustfile.
Option 2: Docker
Use Docker to ensure identical environments across machines.
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY locustfile.py .# docker-compose.yml
version: '3'
services:
master:
build: .
command: locust --master --host https://api.example.com
ports:
- "8089:8089"
- "5557:5557"
worker:
build: .
command: locust --worker --master-host master
depends_on:
- master
deploy:
replicas: 4docker compose up --scale worker=4Option 3: Kubernetes
For large-scale tests in CI/CD:
# locust-master.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: locust-master
spec:
replicas: 1
selector:
matchLabels:
app: locust-master
template:
metadata:
labels:
app: locust-master
spec:
containers:
- name: locust
image: your-registry/locust:latest
command: ["locust", "--master", "--host", "https://api.example.com"]
ports:
- containerPort: 8089
- containerPort: 5557
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: locust-worker
spec:
replicas: 10
selector:
matchLabels:
app: locust-worker
template:
metadata:
labels:
app: locust-worker
spec:
containers:
- name: locust
image: your-registry/locust:latest
command: ["locust", "--worker", "--master-host", "locust-master-svc"]Scale workers with kubectl scale deployment locust-worker --replicas=20.
Controlling User Distribution
By default, Locust distributes users evenly across workers. You can observe the distribution in the master UI: with 1000 users and 5 workers, each worker gets 200.
If a worker disconnects mid-test, the master redistributes its users to remaining workers. This keeps your total user count stable but may cause a spike in per-worker load.
Fixed User Counts per Worker
Sometimes you want each worker to handle a fixed user count rather than splitting a total. Use --fixed-count:
locust --worker --fixed-count 500Each worker with --fixed-count 500 will run exactly 500 users regardless of master configuration. The master still aggregates stats.
Headless Distributed Tests (CI Mode)
For automated pipelines, skip the UI entirely:
# Master runs headlessly and stops after 5 minutes
locust --master \
--headless \
--users 5000 \
--spawn-rate 100 \
--run-time 5m \
--host https://api.example.com \
--expect-workers 4 \
--csv results/load-testThe --expect-workers flag tells the master to wait until that many workers are connected before starting. Without it, the master starts immediately and may run with fewer workers than expected.
# Workers (start before master, or master will timeout waiting)
locust --worker --master-host localhost &
locust --worker --master-host localhost &
locust --worker --master-host localhost &
locust --worker --master-host localhost &Monitoring Distributed Test Health
Worker Count
The master UI shows connected workers. If a worker crashes, the count drops and you'll see a spike in per-user errors. Add worker health checks in CI:
# check_workers.py
import requests
import sys
r = requests.get("http://localhost:8089/workers")
workers = r.json()
if len(workers) < 4:
print(f"Expected 4 workers, got {len(workers)}")
sys.exit(1)Stats API
Locust exposes stats via REST API at /stats/requests:
curl http://localhost:8089/stats/requests | jq '.stats[] | {name, num_requests, avg_response_time, failure_count}'Poll this endpoint from your CI pipeline to track progress and capture results without relying on CSV exports.
Common Problems and Fixes
Workers Can't Connect to Master
Check:
- Port 5557 is reachable from worker machines:
telnet master-ip 5557 - Master is bound to
0.0.0.0, not127.0.0.1(the default) - Firewall rules allow the connection
Fix master binding:
locust --master --master-bind-host 0.0.0.0Uneven Load Distribution
If some workers are overloaded while others are idle, the users-per-worker calculation may be off due to integer rounding. With 1000 users and 3 workers, Locust assigns 334, 333, 333. This is normal and expected.
ZeroMQ Version Mismatch
All machines must run the same pyzmq version. Pin it in requirements.txt:
locust==2.x.x
pyzmq==25.1.0Workers Lag Behind Master
Each worker generates load independently, so clock drift can cause workers to start ramp-up at slightly different times. This is normal for distributed systems. If you need synchronized starts, use --expect-workers with headless mode to ensure all workers are connected before ramp-up begins.
Calculating Required Worker Count
Estimate how many workers you need:
- Benchmark a single worker: run locally and note max RPS before response times degrade
- Divide target RPS by single-worker max RPS
- Add 20% headroom for safety
Example: target 10,000 RPS, single worker handles 800 RPS → need 13 workers (10,000 / 800 = 12.5, rounded up, plus buffer).
CPU-bound tasks like TLS handshakes and response parsing limit per-worker throughput more than simple HTTP requests. If your test includes heavy JSON parsing, benchmark with realistic payloads.
Summary
Locust distributed mode scales from a two-process setup on one laptop to hundreds of workers across a Kubernetes cluster. The master-worker model keeps configuration centralized—you set total users in one place and Locust handles distribution. For CI pipelines, headless mode with --expect-workers ensures all workers are ready before load starts. For large-scale infrastructure tests, Docker or Kubernetes deployments give you reproducible, scalable worker fleets with minimal manual coordination.