简体   繁体   English

Node.js WebSocket

[英]Nodejs websocket

I am working on a nodejs application and the requirement is to send around 10k requests per second per connection. 我正在开发一个nodejs应用程序,要求是每个连接每秒发送约1万个请求。 The client application has to open one websocket connection to send these requets and at the server side it has to just receive and send the data to a queue. 客户端应用程序必须打开一个websocket连接以发送这些请求,并且在服务器端它必须仅接收数据并将其发送到队列。 The number of socket connections at the server side isn't that much, may be around 1k. 服务器端的套接字连接数量不是很多,可能约为1k。 I have few questions regarding this and any help is greatly appreciated. 我对此没有任何疑问,非常感谢您的帮助。

  • First, is it possible to achieve this setup with a single master process? 首先,是否可以通过一个主过程来完成此设置? Since I cannot share the web socket connections with the child processes I need to get the bandwith from master process. 由于我无法与子进程共享Web套接字连接,因此需要从主进程那里获得带宽。
  • When I tried benchmarking nodejs ws library, I was only able to send approximately 1k requests per second of 9kb each. 当我尝试对nodejs ws库进行基准测试时,我每秒只能发送大约1k请求,每个请求9kb。 How can I increase the throughput? 如何增加吞吐量?
  • Are there any examples on how to achieve max throughput since I can only find posts with how to achieve max connections? 因为我只能找到有关如何实现最大连接数的帖子,所以有没有关于如何实现最大吞吐量的示例?

Thanks. 谢谢。

You will eventually need to scale 您最终将需要扩展

First, is it possible to achieve this setup with a single master process? 首先,是否可以通过一个主过程来完成此设置?

I don't really think its possible to achieve this with a single thread. 我真的不认为有可能通过单个线程来实现这一目标。 (you should consider scaling and never design restricting yourself from that option) (您应该考虑扩展,并且永远不要限制自己使用该选项)

Since I cannot share the web socket connections with the child processes I need to get the bandwith from master process. 由于我无法与子进程共享Web套接字连接,因此需要从主进程那里获得带宽。

Im sure you will be happy to know about the existance of socket.io-redis . 我肯定您会很高兴知道socket.io-redis的存在。

With it you will be able to send/receive events (share clients) between multiple instances of your code (processes or servers). 使用它,您将能够在代码的多个实例(进程或服务器)之间发送/接收事件(共享客户端)。 Read more : socket.io-redis (Github) 阅读更多:socket.io-redis(Github)

I know you are speaking about ws but maybe its worth the change to socket.io . 我知道您在谈论ws,但也许值得将其更改为socket.io Image Source Article 图片来源文章 套接字比较

Specially knowing you can scale both vertically (increase the number of threads per machine) and horizontally (deploy more "masters" accross more machines) with relative ease. 特别知道您可以相对轻松地同时扩展纵向 (增加每台计算机的线程数)和横向 (在更多台计算机上部署更多的“主机”)。 (and I repeat myself : sharing your socket clients & communications accross all instances) (我重复一遍:在所有实例中共享您的套接字客户端和通信)

When I tried benchmarking nodejs ws library, I was only able to send approximately 1k requests per second of 9kb each. 当我尝试对nodejs ws库进行基准测试时,我每秒只能发送大约1k请求,每个请求9kb。 How can I increase the throughput? 如何增加吞吐量?

I would suggest trying socket.io + socket.io-redis 我建议尝试使用socket.io + socket.io-redis

  1. spawn a master with a number of workers equal to the number of CPU cores. 生成一个主服务器,其工作人员的数量等于CPU内核的数量。 (vertical scaling) (垂直缩放)
  2. deploy your master accross 2 or more machines (horizontal scaling) 跨2台或更多台机器部署主服务器(水平缩放)
  3. learn about load-balancing & perform benchmarks. 了解有关负载平衡和执行基准测试的信息。

Are there any examples on how to achieve max throughput since I can only find posts with how to achieve max connections? 因为我只能找到有关如何实现最大连接数的帖子,所以有没有关于如何实现最大吞吐量的示例?

You will increase the total throughput if you increase the number of instances communicating with clients. 如果增加与客户端通信的实例数量,则将增加总吞吐量。 (Horizontal + Vertical Scaling) (水平+垂直缩放)

This might also be an interesting read : 这可能也是一个有趣的阅读:

Hope it helped. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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