Achieving One Million TCP Connections: Theory, Kernel Tuning, and Practical Implementation in C/Java/PHP
This article explains TCP concurrency theory, details two experimental approaches for reaching one million simultaneous connections using multi‑IP or multi‑port strategies, and provides step‑by‑step kernel tuning, source code, and validation commands for C, Java, and PHP implementations.
TCP Concurrency Theory Basics
Explain TCP 4‑tuple, theoretical server and client limits, and Linux file‑descriptor constraints.
Approach 1 – Multi‑IP Client
Use a single‑process server and a client configured with 20 IP addresses; adjust kernel parameters:
# vi /etc/sysctl.conf
net.ipv4.ip_local_port_range = 5000 65000
fs.file-max=1100000
fs.nr_open=60000Set user limits in /etc/security/limits.conf:
* soft nofile 55000
* hard nofile 55000Configure 20 IPs, bring up interfaces with make ifup , start server ( make run-srv ) and client ( make run-cli ), monitor with watch "ss -ant | grep ESTABLISH" .
Approach 2 – Single‑IP Client, Multi‑Port Server
Run 20 server processes on different ports; client reuses ports without bind.
Similar sysctl changes, but server fs.nr_open=60000 per process.
Start servers ( make run-srv ) and clients ( make run-cli ), verify connections exceed 1,000,000.
Validation and Memory Observation
Check memory via cat /proc/meminfo (look for Slab usage) and slabtop to see ~3 KB per socket.
Key Takeaways
TCP connection cost is modest (~3 KB); modern hardware can support far more than one million connections when I/O handling is efficient (epoll/NIO). The provided C/Java/PHP code is a minimal blocking acceptor for testing; production use requires event‑driven IO.
Refining Core Development Skills
Fei has over 10 years of development experience at Tencent and Sogou. Through this account, he shares his deep insights on performance.
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.