如何保留一百万个并发 TCP 连接? [英] How to retain one million simultaneous TCP connections?

查看:32
本文介绍了如何保留一百万个并发 TCP 连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要设计一个服务器,它需要为数百万个客户端提供服务,这些客户端同时通过 TCP 与服务器连接.

I am to design a server that needs to serve millions of clients that are simultaneously connected with the server via TCP.

服务器和客户端之间的数据流量将是稀疏的,因此可以忽略带宽问题.

The data traffic between the server and the clients will be sparse, so bandwidth issues can be ignored.

一个重要的要求是,每当服务器需要向任何客户端发送数据时,它都应该使用现有的 TCP 连接,而不是向客户端打开一个新连接(因为客户端可能在防火墙后面).

One important requirement is that whenever the server needs to send data to any client it should use the existing TCP connection instead of opening a new connection toward the client (because the client may be behind a firewall).

有人知道怎么做吗,需要什么硬件/软件(以最低成本)?

Does anybody know how to do this, and what hardware/software is needed (at the least cost)?

推荐答案

您为此考虑使用哪些操作系统?

What operating systems are you considering for this?

如果使用 Windows 操作系统并使用比 Vista 晚的操作系统,那么在一台机器上有数千个连接应该不会有问题.我已经运行了测试(这里:http://www.lenholgate.com/blog/2005/11/windows-tcpip-server-performance.html) 与低规格的 Windows Server 2003 机器一起轻松实现超过 70,000 个活动 TCP 连接.一些影响可能连接数的资源限制在 Vista 上已大大取消(请参阅此处:http://www.lenholgate.com/blog/2005/11/windows-tcpip-server-performance.html),所以你可以用一个小的机器集群.我不知道在这些之前你需要什么来路由连接.

If using a Windows OS and using something later than Vista then you shouldn't have a problem with many thousands of connections on a single machine. I've run tests (here: http://www.lenholgate.com/blog/2005/11/windows-tcpip-server-performance.html) with a low spec Windows Server 2003 machine and easily achieved more than 70,000 active TCP connections. Some of the resource limits that affect the number of connections possible have been lifted considerably on Vista (see here: http://www.lenholgate.com/blog/2005/11/windows-tcpip-server-performance.html) and so you could probably achieve your goal with a small cluster of machines. I don't know what you'd need in front of those to route the connections.

Windows 提供了一种称为 I/O 完成端口的工具(请参阅:http:///msdn.microsoft.com/en-us/magazine/cc302334.aspx),它允许您使用很少的线程为数千个并发连接提供服务(我昨天在运行测试时有 5000 个连接使到服务器的链接饱和有 2 个线程来处理 I/O...).因此,基本架构具有很强的可扩展性.

Windows provides a facility called I/O Completion Ports (see: http://msdn.microsoft.com/en-us/magazine/cc302334.aspx) which allow you to service many thousands of concurrent connections with very few threads (I was running tests yesterday with 5000 connections saturating a link to a server with 2 threads to process the I/O...). Thus the basic architecture is very scalable.

如果你想运行一些测试,那么我在我的博客上有一些免费可用的工具,允许你使用数千个连接(1) 和 (2) 和一些可用于帮助您入门的免费代码 (3)

If you want to run some tests then I have some freely available tools on my blog that allow you to thrash a simple echo server using many thousands of connections (1) and (2) and some free code which you could use to get you started (3)

根据您的评论,您问题的第二部分更加棘手.如果客户端的 IP 地址不断变化,并且您和他们之间没有任何东西提供 NAT 来为您提供一致的 IP 地址,那么他们的连接无疑将被终止并需要重新建立.如果客户端在 IP 地址更改时检测到此连接断开,则他们可以重新连接到服务器,如果不能,那么我建议客户端需要经常轮询服务器,以便他们可以检测到连接丢失和重新连接.服务器在这里无能为力,因为它无法预测新 IP 地址,并且在尝试发送数据时会发现旧连接失败.

The second part of your question, from your comments, is more tricky. If the client's IP address keeps changing and there's nothing between you and them that is providing NAT to give you a consistent IP address then their connections will, no doubt, be terminated and need to be re-established. If the clients detect this connection tear down when their IP address changes then they can reconnect to the server, if they can't then I would suggest that the clients need to poll the server every so often so that they can detect the connection loss and reconnect. There's nothing the server can do here as it can't predict the new IP address and it will discover that the old connection has failed when it tries to send data.

请记住,一旦您的系统扩展到这个级别,您的问题才刚刚开始......

And remember, your problems are only just beginning once you get your system to scale to this level...

这篇关于如何保留一百万个并发 TCP 连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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