R
R
Runcorn2013-11-17 22:07:11
PHP
Runcorn, 2013-11-17 22:07:11

What is the advantage of asynchronous servers over PHP/nginx?

Hello.
Such a question: what is the main advantage of asynchronous servers (Node.js, Python Tornado), frameworks (Python Twisted), etc. over a bunch of asynchronous server + any programming language (or, as the title says, PHP). That is, we get that nginx collects requests from users, and PHP processes them one by one. In an asynchronous server, as many requests are processed at a time as there are workers, and in PHP / nginx the same way.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
_
_ _, 2013-11-17
@Runcorn

In short, the error crept in here:
Imagine that you have a request coming to your server related to fetching data from the database.
It works out, let's say, in 150 ms, of which 130 is working with the database.
In the case of PHP, your worker will be blocked for these 150ms to process other requests.
In the case of an asynchronous server, while request 1 is waiting for data from the database for 130 ms, it will be able to accept and start processing other requests. Let's say we have one PHP worker. In this case, such requests as from the example, he will be able to process seven pieces per second.
Asynchronous, for example, will receive 20 requests. It will process each one before interacting with the database, for example, in 10 ms, 20 requests to the database will fly, they will pass, say, in 500 ms, and the server will generate a response. And it's almost all the same. In total, in less than a second, we will process 20 requests in this way.
You can, of course, increase the FastCGI pool, but the overhead when processing a request by each worker will be disproportionately higher than when processing by an asynchronous server.

S
Sergey, 2013-11-18
Protko @Fesor

All servers designed for heavy loads are asynchronous. That is, the processing of new incoming connections occurs before the current connections finish working.
The advantage of ready-made asynchronous servers is that they are ready. You can write an asynchronous server in php in the same way (socket + select), but this is not as efficient as in python and even more so in node.js, where this functionality comes out of the box.
The standard use case for node.js/Python twisted and any other implementations is to handle either raw tcp connections (like push notifications) or keep-alive for the same purpose. When processing short http requests, the profit is not so great. Anyway between php and python. node.js will be faster, but it will also eat a little more memory.

R
rozhik, 2013-11-18
@rozhik

Such a question: what is the main advantage of asynchronous servers ... over a bunch of asynchronous server + any language
---
before a bunch of asynchronous server + any language - it's not entirely clear what you mean. An asynchronous server or not is one thing, but asynchronous or no access to data from a PL is quite another. I'm sure you're more interested in the second.
All the beauty, complexity and problems in the asynchronous approach arise for the same reason. The result of the request does not come immediately, but asynchronously. This gives a huge reduction in program response time if it uses the result of several independent IO operations. In all other cases, the synchronous approach is simpler, with the same response delivery time. (IO is working with files, external api, etc.)
Nodejs has one significant drawback - it has one execution process. Therefore, heavy calculations on it are significantly inferior in performance to PHP on multiprocessor / kernel systems. (this is easily circumvented by workers etc, but that's another drawback)
erlang, java, and a few other languages ​​don't have the above problem.
Nodejs has 2 more important advantages compared to PHP:
1. You can store data, timers, descriptors, etc. between requests in a node. This makes it much more efficient than various PHP caching mechanisms.
2. The node does not waste time loading the code.
>> In an asynchronous server, as many requests are processed at a time as there are workers, and in PHP / nginx the same way.
Not true, neither in the first nor in the second case. In an asynchronous server there is only one thread that processes any number of requests, in nginx it is exactly the same. PHP is a different story, but each request needs at least a thread or process.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question