如何构建一个可以处理 20.000 个并发连接的服务器?
我很难详细了解服务器需要做什么(由于保密协议和其他原因),但应该足以说它需要处理具有许多功能的轻量级二进制协议并发连接用户数约为 20.000,这是我们的一个相当不错的估计。
请注意,客户端不会不断发送/接收,但我需要保持套接字打开,因为当客户端需要响应时,我们需要尽快响应,并且没有时间每次打开新连接的开销。
该协议非常轻量级,因此不会有大量数据通过网络传输 - 主要问题是同时保持约 20.000 个套接字打开。
(我知道规格有点模糊,但我真的无法详细说明)
我非常清楚我需要做什么以及我们需要什么类型的硬件需要服务器,但我想我会在这里询问现有的项目、技术、语言(例如 Erlang)等可以帮助我构建这个。
如何才能实现这一目标?
It's hard for me to go into exact detail on what the server needs to do (due to NDAs and what not), but it should be sufficient to say that it needs to handle a lightweight binary protocol with many concurrent connected users, ~20.000 is where we have a pretty decent estimate.
Note that clients won't be sending/receiving constantly, but I need to keep the socket open because when the client needs a response we need it as fast as possible and don't have time for the overhead of opening a new connection every time.
The protocol is very lightweight, so there won't be a lot of data going over the wire - the main problem is keeping ~20.000 sockets open at the same time.
(I'm aware that the specifications are a little fuzzy, but I really can't go into more detail)
I have a pretty decent idea what of what I need to do and what type of hardware we need for the server(s) but I figured I'd ask here for existing projects, technologies, languages (Erlang for example), etc. that could assist me in building this.
How can this be achieved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果不需要通过防火墙,请考虑使用基于 UDP 的协议。 NFS 是基于 UDP 协议的一个很好的例子。 UDP 没有 TCP 的设置开销,并且可以扩展到超过 65k 并发连接。但是,如果您需要有保证的交付,则必须将此功能构建到应用程序中。
为了获得大量用户群的性能,请考虑使用基于非阻塞 I/O 的服务器架构。
另一个可能值得关注的项目是 Douglas Schmidt 的自适应通信环境(高手)。它是一个成熟的C++框架,用于构建高性能服务器,主要针对电信应用。它支持各种线程模型并为您处理大多数棘手的事情。您可能会发现,预先花在学习如何驱动它上的时间可以节省下来,从而减少对混乱的同步问题的调试工作。
If you don't have to go through a firewall, consider using a protocol based on UDP. NFS is a good example of a UDP-based protocol. UDP doesn't have the setup overhead of TCP and can scale to more than 65k concurrent connections. However, if you need guaranteed delivery you will have to build this functionality into the application.
For performance with large user bases, consider using a server architecture based on non-blocking I/O.
Another item that might be worth looking at is Douglas Schmidt's Adaptive Communications Environment (ACE). It's a mature C++ framework for building high performance servers, mainly aimed at telecommunications applications. It supports a variety of threading models and handles most of the tricky stuff for you. You might find that the time spent up front learning how to drive it would be saved down the track in reduced debugging effort on messy synchronisation issues.
查看来自 Microsoft 机器人专家的 CCR。我使您能够进行 Erlang 类型编程(消息传递、队列等),但仅使用 C# 而不是全新的函数式语言。
此外,它能够利用异步编程模型,您不需要线程池中的数十个线程来完成您的工作。它速度更快,并且代码非常优雅。
我自己将它用于短信服务器,该服务器需要以惊人的速度发出短信,而且它这样做根本不会给 CPU 带来压力
Take a look at the CCR from the robotics guys at Microsoft. I enables you to do Erlang type programming (message passing, queues, etc), but just using c# and not a totally new functional language.
Furthermore it is able to make use of the asynchronous programming model where you don't need dozens of threads in threadpools to do your stuff. It's much faster and gives really elegant code.
I'm using it myself for an SMS server which needs to spit out SMS's at ridiculous speeds, and it does so without stressing the CPU at all
维护 20,000 个连接的套接字不是问题。只要您使用 I/O 完成端口和/或线程池 API,您就可以在 Windows(服务器)上使用 C 轻松地完成此操作。
我猜真正的问题是为这 20,000 个连接生成数据。 这可能需要一些奇特的解决方案 - Erlang 或其他。但套接字方面的事情并不简单,但完全在传统服务设计的范围内。
Maintaining 20,000 connected sockets is not a problem. You can do it using C on Windows (Server) rather easily as long as you use I/O completion ports and/or the threadpool APIs.
The real prblem I guess is generating the data for those 20,000 connections. That might require some exotic solutions - Erlang or whatever. But the socket side of things is not trivial, but well within the bounds of traditional service design.
Erlang 以其轻量级线程和出色的二进制处理能力使其非常适合。就硬件而言,如果协议非常轻量级,我看不出您将需要极其昂贵的服务器,但这将取决于收到数据包后需要完成的其他处理。
编辑
如果您需要通过索引或其他方式进行数据查找,Mnesia 也更强大,支持内存和基于磁盘的存储,并且如果您最终需要移动到更多服务器,它是完全分布式的
一些现实世界信息关于 Erlangs 连接处理能力
http://www.sics.se/~joe/apachevsyaws.html
Erlang with its lightweight threads and awesome binary handling will make it a great fit. As far as the hardware goes, I can't see that you will need an extremely expensive server if the protocol is very lightweight, but that would depend on other processing that needs to be done after the packet have been received.
Edit
If you need to do data lookups by index or something Mnesia is also greater and supports both in memory and disk based storage and is fully distributed if you end up needing to move to more servers
Some real world info on Erlangs connection handling capabilities
http://www.sics.se/~joe/apachevsyaws.html
您不需要在单个服务器上支持 20K 并发用户。在三到四个之间进行负载平衡,如果您正在进行任何数据库工作,请让它们连接到后端数据库;也许可以添加内存缓存以达到良好的效果,具体取决于您正在构建的应用程序。
You don't need to support 20K concurrent users on a single server. Load balance between three or four, and have them connect to the back end database if you're doing any database work; perhaps throw in memcache for good measure, depending on what app you're building.