How Many TCP Connections Can a Linux Server Really Support? Port, FD, Thread & Memory Limits
This article uses a playful story of a Linux process to explain the real limits on TCP connections, covering the configurable port range, file descriptor caps, thread model constraints, memory usage, and CPU saturation, and shows how to adjust these limits on a Linux server.
Port Range Limits
The story begins with a Linux process (named "Xiao Jin") trying to create a TCP connection to 110.242.68.3:80 using its own IP 123.126.45.68. The OS assigns a source port automatically, but the process soon discovers that the default local port range (shown by cat /proc/sys/net/ipv4/ip_local_port_range) is 1024 65000, allowing only 63977 usable ports. Changing this range in /etc/sysctl.conf (e.g., net.ipv4.ip_local_port_range = 60000 60009) can shrink or expand the available ports.
File Descriptor Limits
Each TCP connection consumes a file descriptor. Linux imposes three levels of limits:
System level : maximum open files, viewable via cat /proc/sys/fs/file-max.
User level : per‑user limits, set in /etc/security/limits.conf.
Process level : maximum open files per process, viewable via cat /proc/sys/fs/nr_open.
Example values from the article:
# cat /proc/sys/fs/file-max
100000
# cat /proc/sys/fs/nr_open
100000
# cat /etc/security/limits.conf
* soft nproc 100000
* hard nproc 100000To raise the per‑process limit temporarily, one can write directly to /proc/sys/fs/nr_open (e.g., echo 100 > /proc/sys/fs/nr_open).
Thread Model & C10K Problem
When each TCP connection is handled by a separate thread, the system quickly runs out of threads, leading to the classic C10K problem. The article explains that using I/O multiplexing (e.g., select, poll, epoll) allows a single thread to manage many connections, dramatically reducing thread count and context‑switch overhead.
Memory Limits
Every connection consumes kernel buffers and user‑space memory. When total memory is exhausted, the kernel reports an out‑of‑memory (OOM) condition, and new connections fail with errors such as "cannot assign requested address" or "memory overflow".
CPU Saturation
Even after expanding port and file‑descriptor limits, the CPU can become the bottleneck. The story ends with the CPU reaching 100% utilization, prompting a system reboot. This illustrates that after addressing port, FD, thread, and memory constraints, CPU capacity becomes the final limiting factor.
Conclusion
The narrative demonstrates a systematic approach to diagnosing and lifting TCP‑connection limits on a Linux server: check and adjust the local port range, raise file‑descriptor caps, switch to an I/O‑multiplexed model to avoid the C10K problem, monitor memory usage, and finally ensure sufficient CPU resources. Understanding these layers helps answer interview questions about maximum TCP connections and shows the importance of a step‑by‑step troubleshooting mindset.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
