What is the role of worker_connections and worker_processes in Nginx?
Understand Nginx worker_processes and worker_connections, how they set concurrency, the file-descriptor limit and capacity formula, with examples.
Expected Interview Answer
worker_processes sets how many worker processes Nginx runs (ideally one per CPU core), while worker_connections sets the maximum simultaneous connections each worker can handle, so together they define Nginx's total concurrency ceiling.
Nginx uses a master process that spawns worker processes; each worker runs an event loop that can juggle thousands of connections asynchronously. worker_processes should typically be auto so every core is used, and worker_connections is bounded by the operating system's per-process file-descriptor limit. The rough theoretical maximum concurrent clients is worker_processes multiplied by worker_connections, though proxied requests consume two connections (client and upstream), so effective capacity is lower.
- Fully utilizes multi-core CPUs
- Defines predictable concurrency limits
- Enables tuning capacity to hardware
- Supports Nginx's event-driven scalability
- Prevents connection exhaustion when sized correctly
AI Mentor Explanation
Think of worker_processes as the number of bowlers in your attack and worker_connections as how many deliveries each can send before tiring. Four bowlers each capable of many overs cover a full innings, just as four workers each holding thousands of connections cover a huge crowd of requests without a single bowler being overloaded.
Step-by-Step Explanation
Step 1
Master spawns workers
The Nginx master process forks worker_processes worker processes at startup.
Step 2
Set workers to cores
worker_processes auto assigns one worker per CPU core for maximum parallelism.
Step 3
Each worker event loops
A worker asynchronously handles up to worker_connections open connections at once.
Step 4
Respect the FD limit
worker_connections cannot exceed the per-process file-descriptor limit (worker_rlimit_nofile).
Step 5
Estimate capacity
Rough max clients equals worker_processes times worker_connections, halved for proxied requests.
What Interviewer Expects
- worker_processes maps to CPU cores; auto is preferred
- worker_connections is the per-worker concurrency limit
- Total concurrency is roughly the product of the two
- File-descriptor limits bound worker_connections
- Proxied requests use two connections per client
- Understanding of the master/worker event-driven model
Common Mistakes
- Thinking worker_connections is a global rather than per-worker limit
- Setting worker_connections above the OS file-descriptor limit
- Forgetting proxied requests consume two connections
- Running one worker on a multi-core machine
- Confusing connections with requests per second
Best Answer (HR Friendly)
“worker_processes tells Nginx how many worker programs to run, usually one per CPU core, and worker_connections tells each of those workers how many connections it can handle at the same time. Multiply them and you get roughly how many simultaneous visitors Nginx can serve.”
Code Example
# One worker per CPU core
worker_processes auto;
# Raise the per-worker file descriptor limit
worker_rlimit_nofile 65535;
events {
# Max simultaneous connections per worker
worker_connections 8192;
}
# Theoretical max clients = workers * worker_connections
# (halved for proxied requests: client + upstream)Follow-up Questions
- Why is worker_connections bounded by the file-descriptor limit?
- How does the master process differ from worker processes?
- Why do proxied requests halve effective capacity?
- What does multi_accept change about connection handling?
- How would you calculate connections needed for 50k concurrent users?
MCQ Practice
1. worker_connections defines the connection limit for what scope?
worker_connections is a per-worker limit; total capacity is that value times worker_processes.
2. What is the recommended worker_processes setting for a 4-core server?
auto assigns one worker per CPU core, so a 4-core machine runs 4 workers.
3. worker_connections cannot exceed which system limit?
Each connection uses a file descriptor, so worker_connections is capped by worker_rlimit_nofile / ulimit.
Flash Cards
worker_processes? — Number of worker processes; set to auto for one per CPU core.
worker_connections? — Max simultaneous connections a single worker can handle.
Max concurrency formula? — worker_processes x worker_connections (halved for proxied requests).
What caps worker_connections? — The per-process file-descriptor limit (worker_rlimit_nofile).