Nginx Vs Apache: Nginx Basic Architecture and Scalability

by Luke P. Issac on November 20, 2013

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.

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 16 comments… read them below or add one }

1 Jalal Hajigholamali November 21, 2013 at 3:08 am

Hi,

Nice and useful material …

2 l0pher November 21, 2013 at 3:28 am

Thanks for sharing this info. Never thought a server could do concurrent processing in a single threaded model.

3 Marko November 21, 2013 at 2:22 pm

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.”

4 Ivan Lobo November 21, 2013 at 3:35 pm

Very good article, I always wanted to know more about Nginx.

5 praveen patil November 21, 2013 at 10:30 pm

Interesting and informative… nice article.

6 Adnan Batara November 23, 2013 at 10:30 am

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.

7 sudi November 26, 2013 at 1:14 am

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).??

8 Chris December 1, 2013 at 1:18 pm

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.

9 panos December 2, 2013 at 12:48 am

yea indeed, nginx can work together with apache.

10 panos December 2, 2013 at 12:52 am

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

11 Abdo December 26, 2013 at 6:55 pm

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?

12 Ashok February 17, 2014 at 10:51 pm

Thank you

13 Aiden February 26, 2014 at 1:22 pm

too > to
“improve the web server architecture too optimize the hardware resources”

14 Subhankar Sengupta February 28, 2014 at 11:37 pm

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.

15 J Walton April 24, 2014 at 8:37 am

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

16 Vinod Jadhav May 4, 2014 at 10:42 am

nice article

Leave a Comment

Previous post:

Next post: