Prev Next

Web / NGINX Interview questions

How does NGINX's event-driven architecture work?

NGINX's event-driven architecture is what lets a handful of worker processes serve tens of thousands of connections without the overhead of one thread per client.

Each worker runs a single-threaded event loop. It registers all open sockets with the OS's efficient event notification interface, then blocks on a single call that returns only when one or more sockets have an event ready - such as data arriving, a connection closing, or a write buffer becoming free. The worker processes each ready event quickly and non-blockingly, then goes back to waiting.

Because no request blocks the worker while waiting on I/O, one worker can interleave work across many connections at once. CPU-bound work is the exception NGINX tries hard to avoid inside the main loop - operations like heavy regex processing or blocking disk access are minimized or offloaded, since they'd stall every connection that worker is handling.

flowchart TD
  A[Worker starts event loop] --> B[Register sockets with epoll/kqueue]
  B --> C[Block on event notification call]
  C --> D{Event ready?}
  D -- Yes --> E[Handle read/write for that connection]
  E --> F[Return control, no blocking]
  F --> C
  D -- No events yet --> C

This is why NGINX's memory and CPU usage stay flat as connection counts climb, in contrast to architectures where each connection carries the fixed overhead of its own thread or process.

Why does NGINX try to avoid CPU-bound work inside a worker's main event loop?
What triggers a worker's event loop to wake up and act?

More Related questions...

What is NGINX? What is the purpose of NGINX as a reverse proxy? What are the main features of NGINX? What are the types of context blocks in NGINX configuration? Define upstream in NGINX? What is the master-worker process model in NGINX? List common NGINX configuration directives? How do you install NGINX on Ubuntu? How do you start, stop, and reload NGINX? What is the nginx.conf file? What are server blocks in NGINX? What is a location block in NGINX? How do you serve static files with NGINX? What is load balancing in NGINX? What are the types of load balancing methods in NGINX? Define a virtual host in NGINX? What is SSL termination in NGINX? How do you enable Gzip compression in NGINX? What is caching in NGINX? What is the purpose of the events block in NGINX? What is the difference between NGINX and Apache HTTP Server? What is the difference between proxy_pass and rewrite in NGINX? What is the difference between a forward proxy and a reverse proxy? How does NGINX handle concurrent client connections? How does NGINX's event-driven architecture work? Why is NGINX considered more performant than process-per-connection servers? Why do we use upstream blocks with multiple servers? When should you use NGINX purely as a reverse proxy versus also as a load balancer? When would you choose round robin over least_conn load balancing? What happens when an upstream server fails a health check? How is SSL/TLS termination configured in NGINX? How can you optimize NGINX for high-traffic websites? How do you troubleshoot a 502 Bad Gateway error in NGINX? How do you troubleshoot high memory usage in NGINX worker processes? Explain the lifecycle of an HTTP request in NGINX? Explain the execution flow of NGINX's request processing phases? Explain the internal working of NGINX worker processes and the event loop? Why doesn't NGINX use a thread-per-request model like Apache's prefork MPM? Why should location block matching order matter between regex and prefix matches? What is the difference between try_files and rewrite directives? How does NGINX handle SSL session caching for performance? How can you optimize NGINX buffer settings for large file uploads? What is the difference between active and passive health checks in NGINX? How do you troubleshoot NGINX configuration errors before reloading? Why is the worker_connections directive important for concurrency limits? When should you use NGINX caching instead of a dedicated CDN? What is the difference between limit_req and limit_conn for rate limiting? How does NGINX handle WebSocket proxying? Explain the internal working of NGINX's keepalive connection pooling to upstream servers? Why do we use try_files with a fallback to PHP-FPM in WordPress-style setups?
Show more question and Answers...


Comments & Discussions