Video in progress
You want to hold 100,000 open WebSocket connections on one Linux server. Which resources run out first?
Usually the file descriptor limit first (ulimit -n, often 1024 or 65535), then the memory of each connection (socket buffers, session object, application buffers), then the thread model. The connections themselves use little CPU; the load mostly comes from heartbeats and broadcasts.
📖 Answer
What I check, in order:
File descriptors. Every socket is one fd. Viewing and raising the limit:
cat /proc/$(pgrep -f app.jar)/limits | grep "open files"
# inside the systemd unit
LimitNOFILE=200000
When the limit runs out, Too many open files appears in the logs and new connections are not accepted, while the old ones keep working, so the problem isn't noticed right away.
Memory. 100k connections × (kernel socket buffers + the session, attributes and send buffer on the Java side). Say 20 to 50 KB per connection, and that's several GB. Spring's sendBufferSizeLimit is 512 KB; that's a maximum and the buffer is usually empty, but think about what happens if they all fill up at once.
Threads. On Tomcat NIO, idle WebSocket connections don't hold a thread; a thread is needed only while a message is being processed. But if your code starts a thread per session or makes blocking calls, everything ends here.
Kernel settings. net.core.somaxconn, net.ipv4.tcp_max_syn_backlog: during a reconnect storm many handshakes arrive at once.
Network/LB. The load balancer has its own connection limits too; with Nginx, remember worker_connections and that every connection needs two fds (client and backend).
It's better to measure than to guess the numbers: open 10k, 50k and 100k connections with k6 or Gatling and watch the heap, RSS and GC. Often the unexpected bottleneck is a lock around your own Map<userId, session> or a scheduler created for every session.
Sign up to mark studied questions and save your progress.