当前位置:网站首页>面试官给我挖坑:单台服务器并发TCP连接数到底可以有多少 ?
面试官给我挖坑:单台服务器并发TCP连接数到底可以有多少 ?
2022-04-23 13:17:00 【朱小厮】
曾几何时我们还在寻求网络编程中 C10K 问题的解决方案,但是现在从硬件和操作系统支持来看单台服务器支持上万并发连接已经没有多少挑战性了。有关 C10K(即单机1万个并发连接问题)的信息可以参考《上一个10年,著名的C10K并发连接问题[1]》和《The C10K problem[2]》这两篇文章。
我们先假设单台服务器最多只能支持万级并发连接,其实对绝大多数应用来说已经远远足够了,但是对于一些拥有很大用户基数的互联网公司,往往面临的并发连接数是百万、千万、甚至上亿。虽然现在的集群,分布式技术可以为我们将并发负载分担在多台服务器上,那我们只需要扩展出数十台电脑就可以解决问题,但是我们更希望能更大的挖掘单台服务器的资源,先努力垂直扩展,再进行水平扩展,这样可以有效的节省服务器相关的开支(硬件资源、机房、运维人力、电力其实也是一笔不小的开支)。
那么到底一台服务器能够支持多少TCP并发连接呢?
首先需要考虑文件句柄的限制。在linux下编写网络服务器程序的朋友肯定都知道每一个tcp连接都要占一个文件描述符,一旦这个文件描述符使用完了,新的连接到来返回给我们的错误是“Socket/File:Can't open so many files”。这时你需要明白操作系统对可以打开的最大文件数的限制。我们可以通过 ulimit -n
命令、/etc/security/limits.conf 文件 以及 /etc/sysctl.conf 文件等来修改文件句柄数。更多细节可以参阅 「朱小厮的博客」里的这篇文章《 文件句柄?文件描述符?傻傻分不清楚》。
其次要考虑的是端口范围的限制。操作系统上端口号1024以下是系统保留的,从1024-65535是用户使用的。由于每个TCP连接都要占一个端口号,所以我们最多可以有60000多个并发连接。我想有这种错误思路朋友不在少数吧?面试官也比较喜欢在这里引导挖坑,类似的问题还有:一个UDP连接可以复用已经被TCP连接占用的端口嘛?(可以在下方留言区留下你的答案。)
如何标识一个TCP连接? 系统使用一个4四元组来唯一标识一个TCP连接:本地端口号 local port、本地IP地址 local ip、远端端口号 remote port、远端IP地址 remote ip。server通常固定在某个本地端口上监听,等待client的连接请求。不考虑地址重用(unix的SO_REUSEADDR选项)的情况下,即使server端有多个ip,本地监听端口也是独占的,因此server端tcp连接4元组中只有remote ip(也就是client ip)和remote port(客户端port)是可变的,因此最大tcp连接为客户端ip数×客户端port数,对IPV4,不考虑ip地址分类等因素,最大tcp连接数约为2的32次方(ip数)×2的16次方(port数),也就是server端单机最大tcp连接数约为2的48次方。
上面给出的结论都是理论上的单机TCP并发连接数,实际上单机并发连接数肯定要受硬件资源(内存)、网络资源(带宽)的限制,至少对我们的需求现在可以做到数十万级的并发了。
参考资料
[1]
上一个10年,著名的C10K并发连接问题: http://www.52im.net/thread-566-1-1.html
[2]
The C10K problem: http://www.kegel.com/c10k.html
想知道更多?扫描下面的二维码关注我
后台回复”加群“获取公众号专属群聊入口
版权声明
本文为[朱小厮]所创,转载请带上原文链接,感谢
https://blog.51cto.com/u_15352876/5248510
边栏推荐
- Three channel ultrasonic ranging system based on 51 single chip microcomputer (timer ranging)
- Melt reshape decast long data short data length conversion data cleaning row column conversion
- 这几种 VSCode 扩展是我最喜欢的
- 「玩转Lighthouse」轻量应用服务器自建DNS解析服务器
- Riscv MMU overview
- Imx6ull QEMU bare metal tutorial 2: usdhc SD card
- CMSIS cm3 source code annotation
- MySQL5.5安装教程
- nodeJs + websocket 循环小案例
- 4.22 study record (you only did water problems in one day, didn't you)
猜你喜欢
解决虚拟机中Oracle每次要设置ip的问题
榜样专访 | 孙光浩:高校俱乐部伴我成长并创业
nodeJs + websocket 循环小案例
[point cloud series] unsupervised multi task feature learning on point clouds
The first lesson is canvas, showing a small case
9419 page analysis of the latest first-line Internet Android interview questions
Solve the problem that Oracle needs to set IP every time in the virtual machine
The filter() traverses the array, which is extremely friendly
AUTOSAR from introduction to mastery 100 lectures (52) - diagnosis and communication management function unit
Super 40W bonus pool waiting for you to fight! The second "Changsha bank Cup" Tencent yunqi innovation competition is hot!
随机推荐
AUTOSAR from introduction to mastery 100 lectures (51) - AUTOSAR network management
Solve the problem that Oracle needs to set IP every time in the virtual machine
Brief introduction of asynchronous encapsulation interface request based on uniapp
Async void provoque l'écrasement du programme
You and the 42W bonus pool are one short of the "Changsha bank Cup" Tencent yunqi innovation competition!
Xi'an CSDN signed a contract with Xi'an Siyuan University, opening a new chapter in IT talent training
100000 college students have become ape powder. What are you waiting for?
ECDSA signature verification principle and C language implementation
缘结西安 | CSDN与西安思源学院签约,全面开启IT人才培养新篇章
这几种 VSCode 扩展是我最喜欢的
XML
vscode小技巧
Uniapp image import local image not displayed
MySQL -- 16. Data structure of index
【微信小程序】flex布局使用记录
Stack protector under armcc / GCC
[official announcement] Changsha software talent training base was established!
torch. Where can transfer gradient
office2021安装包下载与激活教程
nodeJs + websocket 循环小案例