The need for serving large number of concurrent requests is raising every day. The prediction of C10K problem (i.e 10,000 concurrent clients) started the research on web server architecture which could solve this problem. As a result Nginx architecture was developed.
This article explains on a very high-level how Nginx works to solve the scalability problem, along with high level differences between Nginx and Apache webserver.
Increasing Web Server Scalability
The web server scalability problem can be solved either by increasing the hardware capabilities (i.e memory, CPU, etc ) or by improving the web server architecture. The goal here is to improve the web server architecture to optimize the hardware resources, which will eventually lead to a cost effective architecture.
For solving this problem, In 2002 Igor Sysoev started developing a web server written in C, which had an efficient thread management.
Nginx was quite different than the traditional way in which web servers implement thread-based models for serving the request. In the traditional thread-based models, for each client there is one thread which is completely separate and is dedicated to serve that thread. This might cause I/O blocking problems when process is waiting to get completed to release the resources (memory, CPU) in hold. Also, creating separate processes consumes more resources.
In Nginx, the solution to solve the above problem is to use Event-driven, asynchronous, non-blocking and single threaded architecture as shown in the diagram below.
How Nginx Works
By Event-driven it means that notifications or signals are used to mark the initiation or completion of a process. Thus, the resources can be used by other process until a process initiation event is triggered and resource can be allocated and released dynamically. This leads to the optimized use of memory and CPU.
By Asynchronous it means that the threads can be executed concurrently with out blocking each other. It enhances the sharing of resources without being dedicated and blocked.
By Single threaded it means that, multiple clients can be handled by a single worker process as the resources are not blocked.
Nginx do not create a new process or thread for a new request. Here the worker process accepts the requests and process thousands of it with the implementation of highly efficient event loops. As shown in the above diagram, Nginx can be configured to have n number of worker process with a single master process over them.
Thus nginx is able to do the same work with less memory as it is utilized in a very optimized way.
If you are new to Nginx, get started by installing Nginx from source.
Nginx Vs Apache
- As discussed above Nginx is based on event-driven architecture. Apache is based on process-driven architecture. It is interesting to note that Apache in its earliest release was not having multitasking architecture. Later Apache MPM (multi-processing module) was added to achieve this.
- Nginx doesn’t create a new process for a new request. Apache creates a new process for each request.
- In Nginx, memory consumption is very low for serving static pages. But, Apache’s nature of creating new process for each request increases the memory consumption.
- Several benchmarking results indicates that when compared to Apache, Nginx is extremely fast for serving static pages.
- Nginx development started only in 2002. But Apache initial release was in 1995.
- In complex configurations situation, when compared to Nginx, Apache can be configured easily as it comes with lot of configuration features to cover wide range of requirements.
- When compared to Nginx, Apache has excellent documentation.
- In general, Nginx have less components to add more features. But Apache has tons of features and provides lot more functionality than Nginx.
- Nginx do not support Operating Systems like OpenVMS and IBMi. But Apache supports much wider range of Operating Systems.
- Since Nginx comes only with core features that are required for a web server, it is lightweight when compared to Apache.
- The performance and scalability of Nginx is not completely dependent on hardware resources, whereas the performance and scalability of the Apache is dependent on underlying hardware resources like memory and CPU.
Comments on this entry are closed.
Hi,
Nice and useful material …
Thanks for sharing this info. Never thought a server could do concurrent processing in a single threaded model.
It would be nice to add Lighttpd to comparison:
Nginx Vs Apache Vs Lighttpd
On Lighttpd site they state:
“lighttpd powers several popular Web 2.0 sites like YouTube, wikipedia and meebo. Its high speed io-infrastructure allows them to scale several times better with the same hardware than with alternative web-servers.”
Very good article, I always wanted to know more about Nginx.
Interesting and informative… nice article.
I am yet to review and test nginx. You review above is a little bit too early for conclusion and it is true for Apache HTTPD MPM prefork only. Apache HTTPD is more mature and it does have MPM worker which is equivalent to nginx. Starting v2.4, Apache HTTPD offers MPM event which can perform more efficient than MPM worker. From its manual, Apache MPM worker can handle up to 200K clients concurrently. In my use case, I have tested 10K concurrent clients which generate 100K TPS and traffic reaching 8 Gbps. Apache HTTPD v2.2 MPK worker uses very little processor time and RAM compared to application servers running behind it. It acts as load balancer to 16 app servers.
awesome article ….!!!
good explanation for working of nginx…
so just a small question can we use the combination of apache and nginx together its like nginx for serving static pages and apache for rest other things..
if nginx and apache supports the same OS lets says it is centos
will the system resources get optmized properly by using this combination(nginx and apache).??
Sudi,yes you can use apache and nginx together. Django actually reccomends doing this for larger sites. Clients acess the site through nginx; which serves the static files. A proxy is set up to apache, which serves application logic, database, etc.
yea indeed, nginx can work together with apache.
Already using both on a centos – plesk server. Nginx does the job you described above. It is fast and it is not resource hungry.
hope it helps a bit
I am really confused, wether to install nginx or not, I use very large databases, any one tried both would recommend one for large databases?
Thank you
too > to
“improve the web server architecture too optimize the hardware resources”
I am really a big fan of TGS, but I beg to differ with your following statement that “Nginx doesn’t create a new process for a new request. Apache creates a new process for each request”
This is not completely true,
Apache MPM has two models,
1. Prefork (Your above statement is applicable for this model)
2. Worker
The worker MPM uses processes with many threads each process. Each thread handles one connection at a time. This model suitable for high performance web servers.
Please note, apache ALSO has had an mpm for event based just like nginx for well over a year now. Its memory consumption now is only higher than nginx, if you load other modules to do specific jobs.
http://httpd.apache.org/docs/2.4/mod/event.html
nice article
Very good article,i have install Nginx today for my new website.
Nginx is litle hard for newbie.
Thaks for the useful information…
Nginx = Apache lite
No seriously Nginx is just a fork of Apache, whether one uses events over threads does not really explain the whole story. Apache is yes allot beefier than Nginx, but that beefiness can also be scaled down to Niginx level. With proper configuration Apache can out do allot of web servers, fact is however rarely anyone configures their Apache setup its come down to deploy and forget. That and bad publicity just because their are more open source projects available now.
Thanks for the nice artical
Thank you Sir