Comparing Nginx And Apache In Burst Workloads
I recently converted a project over from Apache to NGINX. This is because the project in question was a RESTful API that another service was hitting with bursts of asynchronous requests for performance reasons. Unfortunately, because of the burstful nature of these requests, rather than being a steady stream over a longer period of time, the webserver was getting overwhelmed and a lot of the requests were not coming back successfully. Switching the webserver over from Apache to NGINX resolved this for me, but I wanted to be able to demonstrate and prove this to others. Thus, they may also consider switching, and they have something to reach for when arguing the case for making the change.
I published an open source tool on Github to allow you to test the differences. It allows you to deploy an Apache and NGINX version of the same codebase (which you could tweak to your needs). You can then use the Apache bench tool to hit it with requests and see how well they hold up. The instructions are in the README.md file and there is a video showing that even with just a burst of 100 requests using Apache would result in some failures whilst NGINX was fine.
Why The Difference
Apache has a very simple architecture and will fork a process for every request that comes in. The process will handle the request and then die off. If you suddenly get 100 requests, you will get 100 processes all trying to do their work at the same time. This can lead to a massive spike in memory usage to the point where you could possibly run out. On the other hand, NGINX with PHP-FPM uses pools of threads (NGINX has a pool, and so does FPM). Nginx's pool of threads handle the incoming requests in an event-loop type nature, and pass off the work to a pool of php-fpm processes who carry out the PHP side of the work. Because there will only be up to a specified number of processes running at one time, memory usage will not explode and the CPU scheduler isn't swamped with a mass of threads to switch between. There is a great post on DigitalOcean on the differences between Apache and Nginx.
Benchmarks And Results
Coming soon, I really need to go eat!