How Nginx Handles Millions of Requests: Event‑Driven Model and Performance Tuning
This article explains how Nginx achieves high concurrency through its event‑driven architecture, asynchronous non‑blocking I/O, epoll‑based I/O multiplexing, and practical caching configurations that dramatically reduce memory usage and improve response speed in high‑traffic scenarios.
Nginx is a critical middleware for large‑scale architectures and a must‑know skill in big‑tech companies.
Event‑Driven Model
The event‑driven model is the key to Nginx’s high performance and ability to handle massive concurrency.
In a traditional thread model each request gets its own thread or process, which consumes a lot of memory. For example, handling 10,000 concurrent connections would require creating and destroying 10,000 threads, using roughly 10 GB of memory—unacceptable for most servers.
Nginx typically starts a small number of worker processes (usually equal to the number of CPU cores). Each worker can handle thousands of connections without creating a separate thread per connection.
Asynchronous Non‑Blocking I/O
Nginx uses an asynchronous non‑blocking I/O model, avoiding the blocking that occurs while waiting for I/O operations to complete.
When a connection initiates an I/O operation, the worker process does not block; it continues processing other events until the operating system notifies that the I/O is complete.
I/O Multiplexing
Asynchronous non‑blocking I/O is usually combined with I/O multiplexing techniques such as epoll, select, and poll. epoll has no limit on the number of file descriptors and does not need to traverse the entire descriptor set, so its performance does not degrade with increasing concurrency.
Therefore, most high‑concurrency deployments use epoll for I/O multiplexing.
Nginx Performance Optimization
Properly configuring Nginx’s cache mechanism can dramatically reduce backend load and improve response speed.
Static resources such as images, CSS, and JavaScript can be cached to boost site performance.
# Set cache time
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, max-age=2592000";
}Proxy caching can be used when Nginx acts as a reverse proxy, caching backend responses to relieve backend pressure.
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m;
server {
location / {
proxy_cache my_cache;
proxy_cache_key "$scheme$host$request_uri";
proxy_cache_valid 200 301 302 10m;
proxy_cache_valid 404 1m;
proxy_pass http://backend_server;
}
}Beyond these settings, additional Nginx configuration tweaks can further optimize performance for high‑concurrency scenarios.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Mike Chen's Internet Architecture
Over ten years of BAT architecture experience, shared generously!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
