Worker count and connections
Count connections, not people
A worker connection slot represents a connection managed by that worker. A browser may use several sockets, and HTTP/2 may multiplex requests on one. In a simple HTTP/1.1 reverse proxy with one active upstream connection per active client, two connection slots are needed per pair. Idle downstream keepalive sockets and idle upstream cached sockets also occupy resources. This illustration is a snapshot, not an estimate of requests per second.
client A ── worker ── upstream Aclient B ── worker ── upstream B4 connection slots for 2 active pairsChoose the number of workers
The main-context worker_processes directive chooses the worker count. Start with a modest count for a known CPU allocation. auto is an attempt to detect available processors, not a guarantee of matching container CPU quota. More workers may add scheduling and memory overhead. The events-context worker_connections value applies separately to each worker. The fragment below is illustrative and not a complete server.
worker_processes 2;events { worker_connections 256;}Calculate a conservative slot and descriptor budget
Assume each worker has 256 connection slots, a measured soft file-descriptor limit of 220, 16 reserved connection slots, 40 reserved descriptors (including non-client sockets and open files), 20 idle client sockets and 10 idle upstream sockets. For a paired request consuming two sockets and two descriptors, the smaller remaining budget is 150 descriptors, allowing 75 active pairs per worker. The slot budget allows 105 pairs. Two equally loaded workers give an illustrative ceiling of 150 pairs, not a guaranteed service capacity. Reserves must reflect the actual workload. CPU, memory, backend capacity and uneven distribution can lower this bound.
slots = 256 - 16 - 20 - 10fds = 220 - 40 - 20 - 10pairs = max(0, min(slots, fds) // 2)print(pairs, 2 * pairs) # 75 150Account for connection reuse
Client keepalive avoids repeated handshakes but idle connections still consume slots. Upstream keepalive pools are per worker and their size limits cached idle connections, not all active upstream sockets. Use explicit HTTP/1.1 and clear Connection when teaching the older local version. These snippets are partial contexts; backend is fictional and local. Compare short and long idle lifetimes using a bounded test, not a production change.
upstream lab_backend { server 127.0.0.1:19443; keepalive 8;}# inside a server location:proxy_http_version 1.1;proxy_set_header Connection "";proxy_pass http://lab_backend;Practice and recap
A worker allows 180 slots and 160 descriptors. Reserve 20 slots and 30 descriptors, plus 10 idle sockets from both budgets. Each active pair uses two sockets. Estimate the ceiling and explain why it is not measured throughput. Check your reasoning against the manual exercise. Next: OS limits and event settings.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Worker count and connections
Estimate a capacity bound with upstream overhead
What does worker_connections bound?
How many socket slots do 2 active client/upstream pairs need under the stated model?
What is the per-worker ceiling in the worked budget?
What does upstream keepalive pool size bound?
Why is auto not a capacity guarantee?