We achieved 10x performance using Node.js instead of PHP

Ahmet Aslan
Insider Engineering
3 min readMay 16, 2022
https://viprabusiness-40.webself.net/

In this article, I will explain how we have achieved 10x in response time and 4x in server cost after rewriting our PHP application’s with Node.js.

Let’s dive deeper into our case.

1. Purpose

The app itself is like a counter, which gets numerous requests from the client side and stores the count in the Redis cluster. With every incoming request, it increments the count and returns the modified value as a response to the user.

The sample response is as shown below:

sample response from the app

What we need to observe here is that:

  • The app is a very simple application,
  • The code base is not complicated,
  • There is no function that can affect the runtime performance.

But, since this application receives 25–30 million requests per day, PHP was computing more power on processing the request and returning a lightning fast response was not always possible. So, we have decided to migrate our application into Node.js.

Below you can see the request sum of the app, in a 1-day period.

1 day and 1 hour periods
Requests sum: 1 day in 1 hour period graph.

2. Why Node.js?

As it was described above, switching to Node.js gained as 10x faster system. Let’s investigate the reasons of so huge impact.

Node.js is asynchronous by nature, and can process other requests without waiting for the incoming request to complete. This leads to more efficient CPU usage that PHP can.

PHP is, on contrary, a synchronous language and is not the best match in this scenario — when PHP receives a request, it cannot process the other requests without completing the one in a row. This led to several servers needed with pretty much lower response time.

3. Environment Capacity — before & after.

Again, PHP, being synchronous, required new requests to be carried out by other servers while processing the current one.

For our app to return an instant response, we needed 8 instances on AWS Elastic Beanstalk, having a Load Balancer in front.

Switching to Node.js app version, we were able to handle all these requests with 2 instances in total. 8 versus 2 is equal to 4 times lesser amount of machines needed.

The lesser the amount of machines, the lesser the cost becomes.

https://medium.com/@jcnd/tricks-for-your-money-saving-from-an-expert-972c4722322

PHP vs Node.js Statistics

  1. PHP Average Response Time

Below you can see the average response time in the environment we use PHP.

As you can see, the response times are quite high, this is due to PHP’s synchronous operation. The more requests received, the more proportionally they would increase during this time.

1 day and 1 hour periods

2. Node.js Average Response Time

Below you can see the average response time in the environment we use in Node.js.

As seen here, response times are much lower than PHP. It works more efficiently due to the asynchronous operation of it and uses the CPU in its environment more efficiently than PHP.

1 day and 1 hour periods

Conclusion

This article shows how much performance you can gain if you use the right programming language in the right scenario. Of course, I’m not saying Node.js is better than PHP in every case.

Feel free to reach me on Linkedin, if you have any further questions.

Have a good development :)

--

--