What is the advantage of asynchronous server in front of PHP/nginx?

Hello.
The question is: what is the main advantage of asynchronous servers (Node.js Python Tornado), frameworks (Python Twisted) etc. in front of a bunch of asynchronous server + any programming language (or, as it is written in the header PHP). That is, we get that nginx collects requests from users and PHP oproblem them in turn. In the asynchronous server in a single point in time is processed as many requests as there are sarkerov and PHP/nginx likewise.

October 3rd 19 at 01:52
5 answers
October 3rd 19 at 01:54
Solution

Briefly, an error has crept in here:
In the asynchronous server in a single point in time is processed as many requests as there are workerb

Imagine that you have comes to the server a request associated with fetching data from database.
He fulfills, say, for 150 MS, of which 130 is to work with the database.

In the case of PHP you have vorker will be blocked these 150 MS for processing other requests.
In the case of the asynchronous server, while request 1 is waiting for data from DB for 130 MS, will be able to accept and begin processing other requests. Suppose that we have a single PHP worker. In this case, such requests as in the example, it can handle seven pieces per second.

Asynchronous same, for example, arrive 20 queries. It will handle every interaction with the database, for example for 10 MS, fly 20 database queries, will be, for example, for 500 MS, and the server generates response. And it's all practically simultaneously. A total of less than a second we will process the 20 queries.

You can, of course, increase the pool of FastCGI, but the overhead in processing the request, each worker will be disproportionately higher than the asynchronous server.

Clear. On the one hand you are right, but on the other, is not so optimistic. If BD takes a heavy query that you need, say, 100 milliseconds, it will not be able to run other queries during this time. That is, if the DB server 4 cores, for 100 milliseconds the database will be able to perform 4 such a heavy request. In this case, gains from the asynchronous application is not, as DB is a very narrow place. But I don't know whether there are often in practice these difficult queries. - corene_Ledner commented on October 3rd 19 at 01:57
But in addition to the database queries are still queries that come out from the cache, for example. I.e. the DB query, and in parallel gives even more of the cache. Or requests associated with calls to a third-party API, which will block the worker in the case of PHP. - joe64 commented on October 3rd 19 at 02:00
> If the DB takes a heavy query that you need, say, 100 milliseconds, it will not be able to run other queries during this time. Can. If you do not have locks. And the payout will be. - Lilla99 commented on October 3rd 19 at 02:03
When the query is executed 100 milliseconds, the workflow (or worker, the worker thread), the database is not running any other queries at all, until I finish the current one. - corene_Ledner commented on October 3rd 19 at 02:06
October 3rd 19 at 01:56
Solution

Read it here - http://habrahabr.ru/post/128772/
The short version is that the asynchronous model is not better or worse. She has her own scope. For example, to run heavy math inside NodeJS server - the death of everything and everyone. But heavy queries to the database from PHP is quite a chipper will run on the server (well, as for them it is possible).
Or a good example from nginx-ovogo pearl queries in mysql-database to do (Yes, he can). Then he, too, begins the extremely disgusting work)

That is, the asynchronous model (at least in the area of web servers/applications) is good only when all the queries you have quickly and spend. Something like this.

(well, as usual - comments are not less valuable than the article).

I would like to clarify:
Do I understand correctly that for a large project with varying degrees of load, the best part of doing asynchronous and some not because in fact, asynchrony is not always an advantage, and merely looking for it(all the emphasis on it) not worth? - corene_Ledner commented on October 3rd 19 at 01:59
October 3rd 19 at 01:58
Solution

Quite a narrow place to put a stake in the entire system.
Partially asynchronous system better than the system without async.
But worse than the fully asynchronous.

The creation and support of a variety of processes and threads for synchronous processing - the operation is not cheap, even if they are in a wait state. In practice, this looks like a mad increase LoadAverage.

Async allows you to keep a fixed number of worker processes (ideally, one process per processor core). The process to create it is not necessary to switch almost not necessary in bitcoin I have a scheme to handle thousands of concurrent connections with cpu load less than 1%.

October 3rd 19 at 02:00

All servers are designed for large loads are asynchronous. That is, the processing of new incoming connections happening before I finish work current connections.
Advantage ready asynchronous servers that they are ready. You can just write an asynchronous server in php (socket + select), but is not so efficient as in python and especially in node.js where this functionality comes out of the box.

The standard case of the application node.js/Python and twisted any other implementations of processing or raw tcp connections (e.g. push notification), or keep-alive for the same purpose. In the processing of your http requests profit is not so big. In any case, between php and python. node.js will be faster, but it will eat a little more.

A little explanation: if the socket descriptor exhibited the O_NONBLOCK flag, then the operating system will not wait for new connections or data. The server will be an infinite loop, which will be constantly checked the status of descriptors (ready to read or write). To resolve it through the mechanisms of select/epoll. (multiplexing). Way easier - with a new connection to render its processing in a separate thread. That's actually all magic. All of these funds, not surprisingly, is in php. But to write demons in php is. - corene_Ledner commented on October 3rd 19 at 02:03
October 3rd 19 at 02:02

The question is: what is the main advantage of asynchronous servers ... in front of a bunch of asynchronous server + any language
---
in front of a bunch of asynchronous server + any other language is not entirely clear what You mean. Asynchronous server or not, is one thing, but asynchronous or no access to the data from JAPANESE is quite another. I'm sure You are more interested in the second.

The beauty, the complexity and challenges in the asynchronous approach arise for the same reason. The result of the request comes immediately, but asynchronously. This gives a huge reduction in response time of the program if it uses the result of several independent IO operations. In all other cases, the synchronous approach is easier, at the same time the response is delivered. (IO is a file, external api, etc )

Nodejs has one significant drawback - it has a single implementation. This heavy calculations on it much inferior to the performance of PHP on multiprocessor/nuclear systems. (this case is easily bypassed workername and so forth, but that's another fault)
erlang, java and several other languages do not have the above problems.

Nodejs over PHP has 2 important advantages:
1. the node can store data, timers, handles, etc between requests. This is much more effective than the various caching mechanisms in PHP.
2. the gcd is not wasted time loading of code.

>>In an async server in a single point in time is processed as many requests as there are sarkerov and PHP/nginx likewise.
Not true, neither in the first nor in the second case. In the asynchronous server has only one thread which handles any number of requests in nginx - exactly the same. About PHP is a different story, but each request needs at least a thread or process.

>> in front of a bunch of asynchronous server + any other language is not entirely clear what You mean. Here is a view of a server that is running in asynchronous mode (nginx, lighttpd, etc.) before the application written in any language without using async. >> Nodejs has one significant drawback - it has a single implementation. This heavy calculations on it much inferior to the performance of PHP on multiprocessor/nuclear systems. (this case is easily bypassed workername and so forth, but that's another fault) erlang, java and several other languages do not have the above problems. Process run maybe one, but workarou may be a few, so all CPU cores can be loaded evenly and the performance of these languages on the example of the heavy computation can be approximately equal. >> 1. the node can store data, timers, handles, etc between requests. This is much more effective than the various caching mechanisms in PHP. Well, that's understandable, but this approach can degrade the scaling (for HTTP), but in some cases it may be useful. And about your last paragraph. Either you misunderstood what I wrote or did not quite understand correctly the async. There can be several worker threads, not one, as you write, and in a single moment in time really is processed as many requests as there are worker threads. - corene_Ledner commented on October 3rd 19 at 02:05

Find more questions by tags Node.jsNginxPHPPython