Tuesday, March 19, 2024
HomeLinuxApache vs Nginx Architecture - Explained

Apache vs Nginx Architecture – Explained

The Apache HTTP Server was created by Robert McCool in 1995 and has been developed under the direction of the Apache Software Foundation since 1999. Since the HTTP web server is the foundation’s original project and is by far their most popular piece of software, it is often referred to simply as “Apache”.

In 2002, Igor Sysoev began work on Nginx as an answer to the C10K problem, which was a challenge for web servers to begin handling ten thousand concurrent connections as a requirement for the modern web. The initial public release was made in 2004, meeting this goal by relying on an asynchronous, events-driven architecture.

Nginx Architecture:

Nginx is an event-based web server.

nginx architecture

Nginx and Lighttpd are probably the two best-known asynchronous servers.The main advantage of the asynchronous approach is scalability.An asynchronous server, on the other hand, is event-driven and handles requests in a single (or at least, very few) threads.

Pulling numbers from thin air for illustrative purposes, serving 10,000 simultaneous connections would probably only cause Nginx to use a few megabytes of RAM.

How it 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.

nginx worker

By Asynchronous it means that the threads can be executed concurrently without 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.

Apache Architecture

apache Architecture

Apache is a process-based server

In a process-based server, each simultaneous connection requires a thread which incurs significant overhead and often perform on par with an asynchronous server under light loads, under heavier loads they usually consume far more RAM which significantly degrades performance. Also, they degrade much faster on less powerful hardware or in a resource-restricted environment such as a VPS.

Pulling numbers from thin air for illustrative purposes, serving 10,000 simultaneous connections would consume hundreds of megabytes.

How it works:

Apache provides a variety of multi-processing modules (Apache calls these MPMs) that dictate how client requests are handled. Basically, this allows administrators to swap out its connection handling architecture easily. These are:

  • mpm_prefork: This processing module spawns processes with a single thread each to handle request. Each child can handle a single connection at a time. As long as the number of requests is fewer than the number of processes, this MPM is very fast. However, performance degrades quickly after the requests surpass the number of processes, so this is not a good choice in many scenarios. Each process has a significant impact on RAM consumption, so this MPM is difficult to scale effectively. This may still be a good choice though if used in conjunction with other components that are not built with threads in mind. For instance, PHP is not thread-safe, so this MPM is recommended as the only safe way of working with mod_php, the Apache module for processing these files.
  • mpm_worker: This module spawns processes that can each manage multiple threads. Each of these threads can handle a single connection. Threads are much more efficient than processes, which means that this MPM scales better than the prefork MPM. Since there are more threads than processes, this also means that new connections can immediately take a free thread instead of having to wait for a free process.
  • mpm_event: This module is similar to the worker module in most situations, but is optimized to handle keep-alive connections. When using the worker MPM, a connection will hold a thread regardless of whether a request is actively being made for as long as the connection is kept alive. The event MPM handles keep alive connections by setting aside dedicated threads for handling keep alive connections and passing active requests off to other threads. This keeps the module from getting bogged down by keep-alive requests, allowing for faster execution. This was marked stable with the release of Apache 2.4.

As you can see, Apache provides a flexible architecture for choosing different connection and request handling algorithms. The choices provided are mainly a function of the server’s evolution and the increasing need for concurrency as the internet landscape has changed.

Adavantage of Nginx;

  • Nginx is one of a handful of servers written to address the C10K** problem.
  • Performance and efficiency.
  • Nginx is a fast, light-weight web server that can also be used as a load balancer and caching server.
  • Memory usage is very low.
  • Asynchronously with one thread, rather than using multi-threaded programming.

Advantage of Apache;

  • Apache comes with built in support for PHP, Python, Perl, and other languages.
  • digest access authentication
  • CGI
  • administrative console
  • .htaccess

**C10k problem is the problem of optimizing network sockets to handle a large number of clients at the same time. The name C10k is a numeronym for concurrently handling ten thousand connections.

When to Use Nginx vs Apache

There are a few factors which come into play when deciding whether you should use an Nginx or Apache web server. The pros and cons section above can also help determine which web server is best suited for your use-case. If you want improved performance in regards to static content then the better web server is in most cases, Nginx. Additionally, Nginx also comes ahead in terms of scalability, media streaming, VPS hosting, and reverse proxying.

On the other hand, Apache may be the preferred web server in other scenarios. Many users may want to modify an .htaccess file for server configuration purposes which is not available in Nginx. Additionally, Apache is better suited for shared hosting environments and can provide a control panel making it easier to make server-side changes. Apache also has a larger community / user base providing beginners with more readily available information and support.

RELATED ARTICLES
- Advertisment -

Most Popular

Recent Comments