Event loop and connection handling
Readiness instead of a thread per connection
An event-driven worker keeps state for open connections and resumes work when an operation can make progress. Readiness is a notification, not a promise that a complete HTTP message is available. If a nonblocking read cannot progress, the handler saves state and yields. In this invented sequence A sends part of a header; B can finish while A waits. A later readable event allows A to resume. No timing or throughput claim follows from the trace.
A readable → partial header → yieldB readable → complete request → write responseA readable → remaining header → continueChoose the event mechanism by platform
On Linux, epoll is a common readiness interface; BSD systems commonly use kqueue. select and poll are other mechanisms with different costs and availability. Nginx ordinarily chooses an available suitable mechanism itself. The explicit Linux-only snippet is a diagnostic example, not a portable tuning recipe. The lab leaves the method unspecified and checks the startup log instead. A timer can wake the loop even when no socket becomes ready.
events { use epoll; # Linux build with epoll support only worker_connections 128;}Trace timers and nonblocking progress
A conceptual iteration waits until readiness or a deadline, dispatches bounded work, handles due timers and processes deferred callbacks. The developer guide describes the actual implementation in more detail, including posted events. This outline is intentionally not C source. A handler that blocks for a slow database call prevents its worker from servicing unrelated ready events until it returns. A separate worker may still make progress. Use event-compatible libraries and appropriate backpressure rather than busy polling.
deadline = next_timer()ready = wait_for_events(deadline)handle_ready(ready)expire_due_timers()handle_posted_work()Sockets are not the whole I/O story
Local file reads can stall, including on a cache miss. Supported builds can offload selected file operations to thread pools; this does not make arbitrary third-party code asynchronous. Below, threads mode is optional and requires a compatible build. Do not treat 128 connection slots as 128 simultaneous proxy users: downstream and upstream sockets both consume slots, and file limits and memory further constrain capacity. See the next chapter for budgets. A toy snapshot of 40 clients plus 40 active upstream sockets consumes about 80 slots before other connections.
location /downloads/ { root html; sendfile on; aio threads; # optional build support}Practice and recap
A has a partial header; B has a complete request; a timer C expires. Trace bounded handling without blocking on A. Then count 40 downstream and 40 upstream connections. Check your reasoning against the manual exercise. Next: Memory pools and request lifecycle.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Event loop and connection handling
Distinguish readiness notifications from one thread per connection
What can a readability notification mean?
Which event interface is commonly used on Linux?
What happens when a worker calls a blocking library?
Why maintain timers in an event loop?
Why is worker_connections not a user count?