设计/架构:网络套接字一个连接与多个连接 [英] Design/Architecture: web-socket one connection vs multiple connections

查看:147
本文介绍了设计/架构:网络套接字一个连接与多个连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设计客户端/服务器体系结构期间,与从客户端的每个线程/会话打开一个WEBSOCKET连接(按原样)相比,从同一进程到服务器的多个WEBSOCKET连接(即共享一个连接)有何优势?通常是在连接到Memcached或数据库服务器时完成的.)

During a designing of a client/server architecture, is there any advantage to multiplexing multiple WEBSOCKET connections from the same process to the server (i.e. sharing one connection) vs opening one WEBSOCKET connection per thread/session in the client (as is typically done when connecting to memcached or database servers.)

我知道与每个连接相关的开销(例如RAM ...).但是,每个客户端最多只能拥有不到1K到10K的空间.

I'm aware about the overhead associated with each connection (e.g. RAM ...). But expect to have less than 1K-10K at the most in each client side.

特定用例: 假设,我有一个远程服务器,在一侧有多个会话,而在另一侧,我有多个客户端,每个客户端将通过websocket服务器连接到一个不同的会话. 在远程服务器中,有两种实现方式:(1)每个会话都创建自己的websocket连接(2)所有会话都将使用相同的websocket连接.

Specific use case: Lets assume, I have a remote server with multiple sessions on one side, and on the other side I have multiple clients, each client will connect to a different session through the websocket server. In the remote server, there are 2 ways to implement it: (1) each session create its own websocket connection (2) all sessions will use same websocket connection.

从连接的角度来看,我喜欢共享解决方案(一个到所有会话的websocket连接),因为websocket服务器受可用连接数限制(保存服务器/扩展).

From connection point of view, I like the sharing solution (one websocket connection to all sessions), because websocket server is limited by #of connections available (saving servers/scaling).

但是从流量/数据速度/性能的角度来看,如果一个会话将通过该连接发送许多小数据包,那么,如果我们使用一个共享连接,则将无法利用带宽(有效负载). ../将几个小包收集到一个包中或将大包拆分为小包),因为我们可能必须从不同的会话将不同的包发送给不同的客户端,在这种情况下,我们将无法收集几个包(小包)因为它们的目的地和来源不同!!!除非我们创建虚拟连接"来管理每个会话数据以最大程度地提高速度,否则会带来很大的实现复杂性!!!

But from traffic/data speed/performance point of view, if a sessions will send lots of small packages through the connection, then, if we use one sharing connection, we will not be able to utilize the bandwidth (payload..../collect few small packages into one or split big package into small packages), because we may have to send different packages to different clients from different sessions, in this case, we will not be able to collect few packages (small packages) since they have different destination and from different sources!!, unless we will create "virtual connection" that manage each session data to maximize the speed, but this would create much implementation complexity!!!

还有其他意见吗?

谢谢, JB.

推荐答案

我认为您应该考虑使用有限的连接池,就像它们对数据库连接体系结构一样.

I think you should consider using a limited connection pool, like they do with Database connection architecture.

我将考虑的另一个解决方案是Pub/Sub数据库中间人,例如Redis.这使您可以使用现有的解决方案以及更轻松的可伸缩性.

Another solution I would consider is a Pub/Sub database middleman such as Redis. This allows you to use existing solutions as well as easier scalability.

据我所知,单一连接和使用大量连接都存在问题.

To the best of my understanding, both having a single connection and using a multitude of connections have their issues.

例如,一个连接一次只能发送一条消息.足够大的消息可能会阻止连接...您正在移动大数据吗?

For example, one connection can send only one message at a time. A big enough message could block the connection... are you moving big data?

许多连接会导致开销,这可能会非常昂贵,并且会带来更多的错误机会.请考虑以下内容:

Many connections can cause an overhead that could be very expensive as well as introduce more chances for errors. Consider the following:

  1. 创建新连接非常昂贵,占用带宽,遭受更长的网络延迟并需要本地资源,而这正是websocket可以避免的.

  1. Creating new connections is very expensive, uses bandwidth, suffers from longer network delays and requires local resources and this is exactly what websockets allows us to avoid.

您将遇到可伸缩性问题.例如,Heroku将websocket连接限制为每台服务器600个,或者至少不久前这样做(我认为这很合理)...您如何将所有服务器连接在一起到一个数据存储?

You will run into scalability issues. For instance, Heroku limits websocket connections to 600 per server, or at least they did so a short while back (and I think it's reasonable)... How will you connect all the servers together to one data-store?

请记住,每个操作系统都有一个打开文件限制,并且websockets使用IO架构(每个操作系统都是一个开放文件",因此websockets是有限的资源).

Remember every OS has an open file limit and that websockets use the IO architecture (each one is an 'open-file', so that websockets are a limited resource).

关于流量/数据速度/性能,这是服务器体系结构的问题...但是我相信您实际上会发现通过使用一个连接(或一小部分连接)可以稍微提高速度.重要的是要记住,当您需要发送TCP/IP数据包时,没有任何有效的多任务处理.

Regarding traffic/data speed/performance, it is a question of server architecture... but I believe you will actually see a slight speed increase by using one connection (or a small pool of connections). It's important to remember that there isn't any effective multi-tasking when you need to send TCP/IP packets.

此外,通过有限数量的连接(即使只有一个连接),您将能够从操作系统的数据包加入功能中受益,该功能将允许您通过一个TCP/IP数据包发送许多websocket帧(除非您不断刷新TCP/IP套接字).实际上,您将通过更多的连接浪费更多的带宽-甚至不理会用于打开每个新连接的带宽.

Also, with a limited number of connections (even with one connection), you will be able to benefit from the OS's packet joining feature that will allow you to send a number of websocket frames over one TCP/IP packet (unless you constantly flush the TCP/IP socket). You will actually waste more bandwidth with more connections - even disregarding the bandwidth used to open each new connection.

我敢肯定,只要我的5美分,我们都会有所不同.

Just my 5 cents, we will all think differently, I'm sure.

祝你好运!

这篇关于设计/架构:网络套接字一个连接与多个连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆