How to Achieve One Million TCP Connections on Linux: Step‑by‑Step Guide
This article provides two practical approaches, complete with theory, kernel tuning, source code links, and detailed step‑by‑step commands, enabling readers to set up and verify one million concurrent TCP connections on Linux systems.
Many readers asked for source code after the “million TCP connections” series; this article provides two practical approaches and step‑by‑step instructions to create one million TCP connections on Linux.
TCP Concurrency Theory
TCP connections are identified by a 4‑tuple (source IP, source port, destination IP, destination port). Changing any element creates a new connection.
Server side theoretical maximum
With a 32‑bit source IP space (2³²) and a 16‑bit port space (2¹⁶), the theoretical upper bound is about 2×10¹⁵ connections, though memory limits prevent reaching it.
Client side theoretical maximum
Although the port range is limited to roughly 64 k, a client can use multiple IP addresses or rely on different server ports to exceed that limit; the practical upper bound is astronomically large.
Linux file‑descriptor limits
fs.file-max – system‑wide maximum open files
fs.nr_open – per‑process maximum open files
nofile – per‑user limit
TCP connection memory cost
Each socket consumes roughly 3 KB of kernel memory (slab objects such as densty, flip, sock_inode_cache, TCP). The slabtop command can be used to inspect memory usage.
Solution 1 – Multiple client IPs
Requires two machines (client and server). The client needs 20 distinct IPs (or 20 machines). 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 the additional IPs on the client (modify tool.sh, run make ping to verify, then make ifup).
Start the server:
make run-srvVerify listening with netstat -nlt | grep 8090. Launch the client:
make run-cliMonitor established connections with watch "ss -ant | grep ESTABLISH". When the count exceeds 1 000 000 the test succeeds. Use cat /proc/meminfo and slabtop to examine memory consumption.
Stop the test with make stop-cli (client) and Ctrl‑C (server), then remove the temporary IPs with make ifdown.
Solution 2 – Single client IP, multiple server processes
Deploy several server instances on different ports and launch multiple client processes, each connecting to a distinct server port. The same source code (C, Java, PHP) is used; download from the provided GitHub repository.
Adjust the same kernel parameters as in Solution 1 (port range, fs.file-max, fs.nr_open, nofile).
Start each server:
make run-srvCheck that ports 8090‑8119 are listening. Configure the client’s tool.sh with the server IP, then run:
make run-cliMonitor connections, stop with make stop-cli and make stop-srv, and verify memory usage as before.
Final Thoughts
One million TCP connections consume only about 3 KB each; modern servers with hundreds of gigabytes of RAM can handle far more. Concurrency is just one performance metric; real‑world throughput also depends on request processing logic, I/O models (epoll, NIO, Go net package), and business requirements.
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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
