[英]How is nodejs able to handle more concurrent requests?
I have been reading the following argument between apache
and nodejs
, which I do not understand. 我一直在阅读
apache
和nodejs
之间的以下参数,我不明白。
Apache
is thread and process based ie
each request is handled by a separate thread or process (depending upon configuration), which means if the process is waiting for the I/O, whole thread is blocked. Apache
是基于线程和进程的, ie
每个请求都由单独的线程或进程处理(取决于配置),这意味着如果进程正在等待I / O,则整个线程都将被阻塞。
Every nodejs
instance runs in a single thread and due to its asynchronous nature, it can handle far more number of concurrent requests as compared to apache
每个
nodejs
实例都在单个线程中运行,并且由于其异步特性,与apache
相比,它可以处理更多的并发请求
I have been trying to understand how nodejs scores and its main strong point, but I just do not get the essence of it. 我一直在尝试了解nodejs的得分及其主要优点,但我只是不了解它的本质。
Apache is totally thread based as you described.WHile nodejs is event based.It uses event loop. 如前所述,Apache完全基于线程,而nodejs基于事件,它使用事件循环。 Now lets take a example.
现在举一个例子。 Suppose you have thousands of requests.In apache it will spawn thread for each and every request(Which is obviously have a bottleneck,You cant scale too much vertically to support this.) While in node js 1 instance will have single thread.It doesnt blocks for an i/o.In mean time while i/o is there it can accept and start processing(cpu related) on the new request.
假设您有成千上万个请求。在Apache中,它将为每个请求生成线程(这显然是一个瓶颈,您不能在垂直方向上进行过多扩展以支持它。)而在节点js中,1个实例将只有一个线程。平均来说,当有I / O时,它可以接受并开始对新请求进行处理(与CPU相关)。 Concurrency is way higher in nodejs.As it allows many requests to be handled at the same time.However i donot have any benchmarking on whether request processing is bit higher in nodejs or not.But yes in terms of concurrency nodejs will defeat any of its competitors.
nodejs的并发性更高,因为它允许同时处理许多请求。但是我没有关于nodejs中请求处理是否更高的基准,但是在并发性方面,nodejs会击败它的任何一个竞争对手。
For better understanding let's start from this picture: 为了更好地理解,让我们从这张图片开始:
And there are following steps: 并执行以下步骤:
NodeJS
server. NodeJS
服务器。 Event Loop
(single thread), which is woken up by operation system. Event Loop
(单线程),该Event Loop
由操作系统唤醒。 It passes every request/response as Javascript closure to relevant worker functions with callbacks. Event Loop
returns result to client. Event Loop
将结果返回给客户端。 This way NodeJS main thread doesn't perform any operations by itself and only delegates management process to non-blocking workers where you can do your magic stuff concurrently. 这样,NodeJS主线程本身不会执行任何操作,只会将管理过程委派给非阻塞性工作人员,您可以在其中并行执行魔术工作。
Note: NodeJS can only receive or respond to a single request at a time. 注意: NodeJS一次只能接收或响应一个请求。 However, during the same time it can process multiple requests.
但是,它可以同时处理多个请求。
For example you have 100000 requests at a time. 例如,您一次有100000个请求。 It really makes sense
blocking
or non-blocking
operations you do in your requests handlers. 在请求处理程序中执行
blocking
或non-blocking
操作确实很有意义。
Blocking operations will block javascript processing since it's synchronous action takes place. 阻止操作将阻止javascript处理,因为它是同步操作。 This behavior can lead to server's halt.
此行为可能导致服务器停止。
Non-blocking operations will happen immediately, so everything will depend on vertical scaling . 非阻塞操作将立即发生,因此一切都将取决于垂直缩放 。 It means it will receive/respond as fast as it's possible for current hardware configuration.
这意味着它将以当前硬件配置的最快速度接收/响应。
To increase your server performance: 要提高服务器性能:
For example, max_old_space_size : 例如, max_old_space_size :
–max-old-space-size=8192
Increases the limit for each V8 node process to use max 8Gb of heap memory instead of the 1,4Gb default on 64-bit machines(512Mb on a 32-bit machine).
增加了每个V8节点进程的限制,以使用最大8Gb的堆内存,而不是64位计算机上的默认1,4Gb(32位计算机上的512Mb)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.