Sub topic 16
Choosing Worker Counts and Basic Tuning
This chapter assumes you understand Linux processes, systemd services, HTTP request flow, Nginx, PHP-FPM, and Gunicorn. You should also know how to inspect memory and CPU usage with commands such as free, top, and systemctl.
What a worker does
A worker is a process or thread that handles application requests. Nginx can accept many client connections, but dynamic requests eventually wait for an application worker.
The useful capacity of a server depends on several limits:
- Available CPU time.
- Available memory.
- The number of application workers.
- Database and other downstream services.
- Request duration.
- The number of requests waiting in queues.
Adding workers can improve concurrency when requests spend time waiting for I/O. It can reduce performance when too many workers compete for CPU or consume all memory.
Choosing Gunicorn workers
For a synchronous Gunicorn worker, one worker usually handles one request at a time. A common starting point is:
workers = (2 × CPU cores) + 1Treat this as a starting experiment, not a rule. A small server with two CPU cores might start with three workers. A memory-heavy application might need fewer workers. A mostly I/O-bound application might benefit from more concurrency, but the database can become the real bottleneck.
Measure the memory used by one worker before increasing the count. If each worker uses 250 MiB and the server has 1 GiB available for the application, four workers already exceed the safe budget after allowing for Nginx, the operating system, and other services.
A basic Gunicorn configuration might look like this:
gunicorn \
--workers 3 \
--bind unix:/run/gunicorn/app.sock \
--timeout 30 \
--access-logfile - \
app:applicationThe timeout option limits how long a worker may remain silent. A timeout can recover a stuck worker, but setting it too low can terminate valid slow requests. Fix slow database queries and external calls rather than hiding the problem with a large timeout.
Choosing PHP-FPM processes
PHP-FPM uses pools of worker processes. The pm.max_children setting is the pool's maximum simultaneous request capacity.
With dynamic process management, a pool can be configured like this:
; /etc/php/8.3/fpm/pool.d/www.conf
pm = dynamic
pm.max_children = 12
pm.start_servers = 3
pm.min_spare_servers = 3
pm.max_spare_servers = 6
pm.max_requests = 500The correct max_children value depends mainly on memory:
max_children ≈ memory available to PHP / average PHP worker sizeLeave room for the kernel, Nginx, databases, caches, and deployment tools. pm.max_requests recycles workers after a number of requests. This can limit the impact of gradual memory leaks, although recycling causes some process startup overhead.
Observe before changing values
Check whether workers are busy, idle, or being killed:
free -h
ps -o pid,rss,cmd -C gunicorn
sudo systemctl status php8.3-fpm
sudo journalctl -u gunicorn -u php8.3-fpm --since "15 minutes ago"Look for queueing, worker timeout messages, out-of-memory events, and connection errors. Change one setting at a time, generate representative traffic, and compare latency, error rate, CPU, and memory.
| Symptom | Likely cause | First check |
|---|---|---|
| High latency with idle CPU | Too few workers or slow downstream I/O | Application and database timings |
| High CPU and rising latency | Too many runnable workers or CPU-bound code | `top` and worker count |
| Workers killed unexpectedly | Memory pressure or an OOM event | `journalctl -k` |
| Requests wait in a queue | Worker pool is too small or requests are slow | Application server metrics |
Exercise
A two-core server has three Gunicorn workers, but memory usage reaches 95% and the kernel logs out-of-memory events. What should you do first?
Show answer
Measure per-worker memory, reduce the worker count or application memory use, and leave capacity for Nginx and the operating system before testing again.