How Many TCP Connections Can a Single Server Actually Handle?
This article explains the theoretical and practical limits of TCP connections on a Linux server, covering kernel parameters such as fs.file-max, soft/hard nofile and fs.nr_open, memory constraints, client port limits, and step‑by‑step configuration examples to maximize concurrent connections.
When preparing for a technical interview, a common question is how many TCP connections a single server can support. The answer depends on Linux kernel limits, memory availability, and client port constraints.
Linux file descriptor limits
Three kernel parameters control the maximum number of open files (including sockets) on a Linux system:
fs.file-max : system‑wide limit; root can bypass it.
soft nofile : per‑process soft limit; must be increased together with the hard limit.
fs.nr_open : per‑process hard limit; can be set per user.
These parameters are inter‑related: raising soft nofile without raising the hard limit has no effect, and fs.nr_open must be larger than the hard limit.
Adjusting the limits (example)
vim /etc/sysctl.conf
fs.file-max=1100000 # system‑wide buffer
fs.nr_open=1100000 # ensure > hard limitApply with sysctl -p.
vim /etc/security/limits.conf
soft nofile 1000000
hard nofile 1000000Theoretical maximum connections
Each TCP connection is identified by a 4‑tuple (source IP, source port, destination IP, destination port). Theoretically the number of possible connections is: 2^32 (IP) × 2^16 (port) ≈ 2.8×10^14 In practice, CPU and memory limit the achievable count.
Practical limits on a server
For a server with 4 GB RAM, an established idle connection consumes roughly 3.3 KB of memory. Thus a 4 GB machine can hold about 1 million concurrent TCP connections, assuming no data processing overhead.
Client‑side limits
A client consumes one local port per connection. With a single IP, the maximum is ~65 535 connections; with multiple IPs or multiple server ports, the limit scales accordingly (e.g., n × 65535 for n client IPs).
Port range can be changed via net.ipv4.ip_local_port_range.
Using bind() on the client is discouraged because it overrides the kernel’s port selection strategy.
Other important parameters
net.core.somaxconncontrols the listen backlog (default 128).
After a process exits, its ports may remain in TIME_WAIT; restarting too quickly can cause “address already in use”.
"Too many open files" error
This error occurs when a process exceeds the allowed number of file descriptors. Resolving it involves increasing fs.file-max, soft nofile, and fs.nr_open, while respecting their coupling relationships.
Author: 文攀 Source: juejin.cn/post/7162824884597293086
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.
