DEV Community

emilioSp
emilioSp

Posted on • Updated on

NodeJS vs Apache performance battle for the conquest of my ❤️ ⚔️

I declare it immediately, JavaScript is my favorite programming language. Along with JavaScript in the last three years, PHP has gained the second position in my heart, mainly thanks to my job. However, I currently use both of them at work: JavaScript within the NodeJS runtime and PHP within Apache webserver.

IMHO Apache + PHP is simpler for a newbie programmer, NodeJS is a little harder due to its asynchronous architecture. In few words, Apache manages a request forking a process and assigning it to the request, NodeJS, on the other hand, has an event-driven architecture which allows it to be able to manage concurrent requests with a single main thread called the event loop. The event loop manages all the requests but delegates IO tasks to other threads.
The asynchronous approach could be harder to understand and if not properly managed could generate a mess in the code (the famous callback hell). However, when you start to rule the event loop properly, NodeJS returns back great satisfaction. I advise you to have a look at the async/await syntax if you are not familiar with it.

I have heard and read many times this sentence NodeJS is super fast, so I have decided to test its performance, and I'll show you the results, comparing it with Apache + PHP stack.

Methodology

The tests consist of a series of calls bombing, raising progressively the concurrency level in order to analyze the behavior of the server.

The requests belong to two categories:

  • A simulated IO operation that takes 100ms to complete. For instance, it could be an execution of a query in the database or a call to a rest API.
  • A CPU intensive task (compute the prime numbers from 0 to 5000 with a naive algorithm).

The KPI under observation are:

  • The number of requests per second successfully managed.
  • The time required to manage a request (mean).
  • The longest time required to fulfill a request (worst case).

The environment for the test is made up of:

  • A Docker container with Apache Benchmark software that represents the client.
  • A Docker container with the NodeJS server (v12.14.0).
  • A Docker container with the Apache (v2.4.41) + PHP (v7.3.13) server.
  • All the Docker images are based on the alpine v3.11 Linux distribution.
  • The Docker engine is v19.03.5.

The Docker containers run on my laptop, a MacBook Pro (13-inch, 2019, Two Thunderbolt 3 ports) with:

  • 1,4 GHz Quad-Core Intel Core i5
  • 8 GB 2133 MHz LPDDR3
  • SSD 256 GB
  • macOS Catalina (v10.15.2)

The Docker images use the official versions of NodeJS and Apache, no tuning is allowed.

Results

Requests per second

The NodeJS runtime is designed very well to manage requests which involved IO operation, thanks to its asynchronous model.
In fact, NodeJS was able to increase the managed requests per second in accordance with the increasing of concurrency level.

In the CPU task, both technologies reached the maximum result with the minimum concurrency level.

Time per requests (mean)

Here again, the asynchronous approach shows its strengths.

The response time is almost the same with the lowest concurrency level, but with 250 concurrent requests, NodeJS was five times faster than Apache.

Worst case

In this scenario is visible the maximum time waited by a user.
Analyzing the results with the maximum concurrency level:

  • IO task: NodeJS -> 1.2 sec, Apache -> 7.8 sec
  • CPU task: NodeJS -> 2.7 sec Apache -> 13.4 sec

Conclusions

...and the winner is...NodeJS.
Yes, it's true, NodeJS is superfast! Now I have the proof.

For code details have a look here:

GitHub logo emilioSp / node-vs-apache

Performance test of a NodeJS server vs a PHP Apache server




Top comments (6)

Collapse
 
roelofjanelsinga profile image
Roelof Jan Elsinga

Nice comparison! You should try Node.js vs Nginx next, I think that might be a better comparison for performance reasons

Collapse
 
aedanobrien profile image
Aedan • Edited

Your comparison is fundamentally flawed because you're essentially just comparing asynchronous vs synchronous models, blocking vs non-blocking I/O.
Try PHP with Swoole extension. Swoole adds the same asynchronous support to PHP that NodeJS has, allowing for multiple concurrent http connections. PHP with Swoole is up to 10 times faster than NodeJS depending on your use case. It also leaves GO behind. Seriously, look into PHP Swoole, it will blow your mind.

Collapse
 
marklester profile image
Mark Lester

You have to work dammed hard to beat a competent Node engineer in performance. You would need extremely high performance requirements to make a significant factor, and you will have to fight hard to justify an alternative. Choosing TypeScript for as much of the drugery as possible gets you more than rapid application development and never having to interrogate a set of Apache config files, which are horrendous and lead to errors. They generally have DONT ****ING TOUCH nailed at the top of any production environment file.
It's all one stack , disk to glass. Any state of the art application requires sophisticated use of JavaScript. Whatever your group size is, from 1 to 100, you will be more productive and develop your skill base better, if you view this as one project and do as much as possible in one language.
If you have an existing Java or other application, build a JavaScript interface and push all the HTTP handling and anything that's cheaper and better to handle this way, into Node.

Collapse
 
alaa2amz profile image
alaa zak

I think that the test is unfair you used Apache internal php module which recreate php process for every request and this super slow. we should repeat it with configuring php to work with FAST CGI interface since it works with array of persistent reusable processes to handle requests and it is by far faster than traditional internal php module
I am very interesting to know the results
sorry for inconvenience

Collapse
 
memorodmx profile image
MemoRodMx

Is nodejs recommended for production environments?

I read and heard that it is only for microservices

Collapse
 
emiliosp profile image
emilioSp

Imho it depends from the software application, but in general, node is safe to be used in production.