Why Nginx Outperforms Apache in High‑Concurrency Scenarios
This article compares Nginx and Apache, showing how Nginx’s event‑driven, single‑threaded architecture and efficient memory usage enable it to handle tens of thousands of concurrent connections with far less resources, and explains the underlying Apache work modes and I/O models.
Nginx has quickly become the dominant web server, widely recognized for its superior efficiency in handling massive static request concurrency, easily solving the C10K problem.
In high‑concurrency tests, Nginx + PHP (FastCGI) sustained over 30,000 concurrent connections—roughly ten times the capacity of an equivalent Apache setup.
A 4 GB server running Apache in prefork mode could only manage about 3,000 concurrent connections, consuming more than 3 GB of memory and crashing when connections approached 3,800 due to memory and swap exhaustion.
By contrast, an Nginx + PHP (FastCGI) server handling 30,000 connections used just ten Nginx worker processes (≈150 MB) and 64 php‑cgi processes (≈1.28 GB). Including the OS, total memory usage stayed under 2 GB; reducing php‑cgi processes to 25 would lower consumption to about 500 MB.
Even under 30,000 concurrent connections, PHP scripts served by Nginx remain fast.
Why Nginx excels in high concurrency can be understood by comparing the architectures and operation modes of the two servers.
1. Apache’s Three Work Modes
Apache provides three Multi‑Processing Modules (MPMs): prefork, worker, and event.
prefork: a multi‑process model where each request is handled by a separate process, using the select mechanism for notifications.
worker: a multi‑threaded model; a process spawns multiple threads, each handling a request, still based on select but capable of handling more connections.
event: an asynchronous I/O model where a single process or thread can handle many requests, driven by an event loop (epoll).
1.1 Prefork
When no explicit MPM is specified, prefork is the default on Unix. It forks child processes in advance, each handling a single request without using threads, offering high stability.
1.2 Worker
Worker combines multiple processes with multiple threads per process, allowing higher request throughput with lower resource overhead than pure process‑based models.
1.3 Event
The event model lets a single process handle many connections using callbacks and epoll, keeping the process idle when no work is pending, thus supporting massive concurrency with minimal resources.
2. Boosting Web Server Concurrency
Key conditions:
Thread‑based processing: a process creates multiple threads, each serving a request.
Event‑driven model: a process handles many requests, using epoll for readiness notifications.
Asynchronous I/O (AIO) on disk.
Memory‑mapped I/O (mmap) to avoid copying data between kernel cache and user space.
Nginx supports all of these features, which is why its documentation claims support for up to 50,000 concurrent connections.
3. What Makes Nginx Superior
Traditional process‑ or thread‑based servers suffer from blocking I/O, low CPU/memory utilization, and costly context switches. Nginx adopts a modular, event‑driven, asynchronous, single‑threaded architecture that heavily uses multiplexing and event notifications.
In Nginx, a small number of worker processes (each with a single thread) handle thousands of concurrent connections via an efficient run‑loop.
4. Nginx Architecture
Nginx runs a master process and several worker processes; when caching is enabled, cache‑loader and cache‑manager processes are also spawned. All processes are single‑threaded and communicate via shared memory. The master runs as root, while workers and auxiliary processes run as non‑privileged users.
Under high‑concurrency loads, Nginx is a solid alternative to Apache.
Nginx is easy to install, has a concise configuration syntax (even supporting Perl), and is remarkably stable—capable of 24/7 operation for months without a restart, while still allowing seamless version upgrades.
5. Nginx’s Origin: Solving the C10K Problem
The choice of I/O multiplexing model explains the performance gap:
5.1 select (used by Apache)
Limited by a maximum number of file descriptors and incurs linear‑time scanning overhead as descriptor count grows.
5.2 poll
Removes the descriptor limit but still suffers from linear scanning.
5.3 epoll (used by Nginx)
Provides event‑driven readiness notifications, eliminating unnecessary scans, and uses memory‑mapped techniques to avoid copying large descriptor tables. However, epoll is Linux‑only, which limits cross‑platform portability.
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
