Comparing Apache and Nginx Web Servers
A brief comparison of the architectural decisions that underlie Apache and nginx, revealing why nginx has been taking the lead on webserver marketshare. We explore the thread spawning model and the event-driven model of Apache and nginx, respectively.
A web server is the piece of software running remotely on the host responsible for serving content to a visitor's browser.
According to Netcraft, the most popular web servers are Apache and nginx. Apache used to host most of the web, then nginx came along and is slowly taking over its position.
Apache's Thread Spawning Model
Apache's development dates back to 1995. Apaches is more resource-intensive than nginx, specially in regards to memory usage.
This happens because Apache's request handling model requires spawning a new thread to handle each request. Each thread blocks when waiting for I/O (for example, the response from a database), then is terminated once the request is done.
Thus, Apache requires the Operating System to keep track of more resources and to manage more blocking threads. This usually isn't a issue for under a thousand requests a second, but it does take more memory and computing resources per request, which ultimately means having to pay for a beefier host.
Nginx's Event-Driven Model
Nginx was first released in 2004, boasting a novel request handling model, which in contrast to Apache's, uses newer kernel features to leverage asynchronous event-driven I/O.
This means that nginx spawns a fixed number of worker threads (usually one per logical core), and these threads process requests as they come in. This allows nginx to handle upwards of 10 thousand requests per second on a single machine, while being light on resources.