简体   繁体   English

Node.js如何处理更多并发请求?

[英]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. 我一直在阅读apachenodejs之间的以下参数,我不明白。

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,则整个线程都将被阻塞。

  • What did I loose here? 我在这里放了什么? I will also wait when running nodejs, even if it means waiting asynchronously. 运行nodejs时,我也将等待,即使这意味着异步等待。 Isn't it? 是不是

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相比,它可以处理更多的并发请求

  • Could anyone explain me how? 谁能解释我怎么做?

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: 并执行以下步骤:

  1. Client sends HTTP requests to NodeJS server. 客户端将HTTP请求发送到NodeJS服务器。
  2. Requests come to 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. 它将每个请求/响应作为JavaScript闭包传递给具有回调的相关辅助函数。
  3. After that you do some operations (long-running jobs etc.) inside non-blocking workers. 之后,您可以在不阻塞的工作程序中执行一些操作(长时间运行的工作等)。
  4. As soon as it's done, response is sent back to main thread via callback. 完成后,响应将通过回调发送回主线程。
  5. In the end 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. 在请求处理程序中执行blockingnon-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: 要提高服务器性能:

  • Horizontally , you might want to play with NodeJS clusters , load-balancer configurations etc. 从水平上讲 ,您可能想使用NodeJS 集群 ,负载均衡器配置等。
  • Vertically , you might want to play with V8 engine. 纵向而言 ,您可能想使用V8引擎。 Look here to find more V8 flags. 在此处查找更多V8标志。 Or you can upgrade your hardware of course. 或者,您当然可以升级硬件。

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.

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