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?
Due to the V8 engine, asynchronous execution, and real-time server interaction, Node. js offers a better execution speed and certainly outperforms PHP.
Since PHP can obviously handle HTTP requests, it has everything you need to build RESTful API's. The whole point of frameworks is to handle common tasks and things that are otherwise tedious. REST API's are commonly built with PHP, so a plethora of frameworks exist.
With a combination of modern-day programming functions, Node. js can execute programs faster than PHP and most other programming languages like Python, Java, Go, and so on.
Quick Summary :- PHP and Node. js are highly recommended backend technologies for the web. While PHP has been long considered ideal for server-side scripting, Node. js perfectly meets the development needs of modern web applications.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With