简体   繁体   中英

PHP vs. Node REST-API

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?

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM