Home » Php » node.js – PHP vs. Node REST-API

node.js – PHP vs. Node REST-API

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m building a simple REST-API which has one endpoint, which will be penetrated heavily. Let’s call it POST /message. I have to decide between using Node or PHP. The Database is MySQL.

What happens inside this route:
– Credentials through HTTP-Auth will be checked by reading them from the database.
– Request to another REST-API.
– Another write-database action will be performed.

So there are 2 database connections and a http request to another REST-API. The route should all be about speed. I would go for PHP, because the current system is based on PHP, but the request inside the route scares me, cause it’s not made asynchronously when using PHP. I don’t care about the result of this request and in node I could just check the credentials and return success, send the request asynchronously and do the database write performance after the request returns. I don’t think I can do that in PHP, cause when I return the REST call with success, everything has to be done before, right?

Go for PHP or node?

How to&Answers:

You wrote:

cause it’s not made asynchronously when using PHP

Are you sure that is not possible? Not even with Guzzle Async Requests?

Anyway, I implemented the same REST API server in a few languages and tested on the same machine (Ubuntu Linux 16.04, i7 Intel NUC, 16GB RAM) and found:

(source)

Note that Node.js was the only platform that was not able to use multiple cores efficiently.

In order to simulate your requirements I tried adding a 15ms usleep to the PHP one and a 15ms setTimeout to the Node.js one and found that when hitting it with 2000 concurrent requests Node.js was having a higher throughput (4300 vs 1800 req/sec), but also a higher latency (450 vs 130 ms/req). Probably because it was using just one core and had to respond to many events. This higher latency with higher throughput may be caused by using an event loop. Using Apache (pre)fork may be more expensive, but is able to achieve a higher concurrency.

I’m not sure all this is gonna help you much directly, but it may give you a starting point for your own research. Have fun!