Wednesday, March 22, 2017

NGNIX vs Apache vs Node

Architectural decisions are always hard to make but the most crucial ones when it comes to server architecture and technology selection. This article is meant to make few of them a bit simpler.

Let us compare Apache, NGNIX and Node and help everyone give an insight to make a right decision.

Apache and Nginx are both HTTP servers. They can serve static files like (.jpg and .html files) or dynamic pages (like a Wordpress blog or forum written in a language like Java, PHP or Python). Apache/nginx need to be configured to recognize the URLs that users will be requesting and route them to the right place.


So, for example, with a typical Java Application you tell Apache that any file that ends with .jsp should be interpreted as Java code, so when the user visits "http://javaexpert.com/java.jsp?q=training", for example, Apache will launch the Java interpreter (JVM) to read the file and process it. As part of this process, Java Controller may talk to a MySQL database and use that to generate the page. Lastly, Tomcat Container gives the final Java Embeded HTML Code to Apache to send to the user's browser.

Sounds simple correct?

Now, Node is a bit different. It's a programming environment like PHP that lets you talk to database, make dynamic pages, etc. However, it differs in that it includes an HTTP server. That means that it can actually act completely on its own without nginx or Apache. You can just run Node and it will be the HTTP server and also the "app server" (which actually creates your dynamic pages and talks to the DB).

It's bullseye deal !
Now let us get into more in detail of each one of them and compare them.
Apache
Apache's architecture simplicity :
Apache debuted in 1995 and in those days the web pages were very simple and the traffic was very low.
Apache's architecture was pretty simple for each new request a new process of correct type was spawned to handle the HTTP connection. The process would receive the request, process the request, make a response and return the response. The architecture was pretty simple and apache adopted it, there was very big downside to it we will discuss it in next paragraph. To make the process of spawning faster apache adopted a prefork model a number of processes were pre-spawned and they would process the HTTP connection as soon as the requests were made.
So each request was handled by a separate process but by 2005 the web traffic was increasing at a very high rate and the web pages were becoming more and more complex and to decrease the web page load time what was earlier a single request to the server was broken down into multiple requests and each request was made in parallel so that the resources could be downloaded in parallel and moreover browser would keep these connections open using keep-alive header. So apache's basic architecture of one process for one request soon started to consume the entire memory and crash.
Nginx :
NGINX was written specifically to address the performance limitations of Apache web servers. It was created in 2002 by Igor Sysoev, a system administrator for a popular Russian portal site (Rambler.ru), as a scaling solution to help the site manage greater and greater volumes of traffic. It was open sourced in October 2004.
The success of Nginx is attributed to its architecture. Nginx follows an asynchronous event driven model for handling requests. Each worker can handle number of requests.
Question is what is event based model and what is relation of event based model with a worker handling multiple number of request ?
So suppose hypothetically we have only one worker on the nginx server. So nginx server maintains a queue for all the requests. It picks up the request if no I/O is required for that request it does the processing and returns the request with a response.
If the request has an I/O the a process is spawned for it at this point asynchronous behaviour and event driven behaviour comes into picture. The web worker does not waits for I/O to complete it moves to next request. What happens to the I/O the I/O on completion fires an event of completion and its callback is placed in queue for execution.
So in this way one web worker is able to handle multiple number of requests and generally on Nginx server there are number of workers or processes.
Node.js :
What JVM is to java, node.js is to javascript. It allows us to use javascript as a language for scripting on servers.
Ryan Dahl creator of Node.js was impressed by Nginx and looked at javascript which was made with asynchronous and event based model. He used google V8 engine which is used in chrome to process Javascript and created Node.js.

No comments:

Post a Comment