Back to articles
AI & Cloud

Deep Linux Kernel Optimization: Solution for 100,000 Concurrent WebSocket Connections for Node.js on VPS

Detailed guide on deep Linux kernel optimization to support 100,000 concurrent WebSocket connections for Node.js on VPS, including sysctl tuning, resource limits, and performance...

DPTCloud Engineering4 min read
Deep Linux Kernel Optimization: Solution for 100,000 Concurrent WebSocket Connections for Node.js on VPS

Deep Linux Kernel Optimization for 100,000 Concurrent WebSocket Connections

Node.js applications handling WebSocket connections often face high demands for concurrent connections, especially in messaging systems, gaming platforms, or IoT solutions. To reach the 100,000 concurrent WebSocket connection threshold on a VPS, deep Linux kernel optimization is essential. This guide provides a step-by-step configuration to maximize system performance.

1. Check and Upgrade System Resource Limits

First, ensure the system has sufficient resources to handle a large number of connections. Check file descriptor and process limits using the following commands:

ulimit -n
ulimit -u

The default values are often 1024. To increase them to 100,000, edit the /etc/security/limits.conf file:

nodejs soft nofile 100000
nodejs hard nofile 100000
* soft nofile 100000
* hard nofile 100000

Restart the system or log out and back in to apply the changes.

2. Configure sysctl for Network Connection Optimization

Edit the /etc/sysctl.conf file to increase network connection handling capacity:

net.core.somaxconn = 65535
net.core.netdev_max_backlog = 5000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_max_tw_buckets = 200000
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl = 15
net.ipv4.tcp_rmem = 4096 87380 6291456
net.ipv4.tcp_wmem = 4096 65536 6291456
net.core.rmem_max = 67108864
net.core.wmem_max = 67108864
net.core.netdev_max_backlog = 5000
net.core.somaxconn = 65535

Apply the changes with the command:

sysctl -p

3. Optimize Node.js and WebSocket Configuration

In your Node.js application, use the ws or socket.io library with optimized settings:

const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080, maxPayload: 10485760 });

Set an appropriate maxPayload value to prevent errors when transmitting large data.

4. Performance Testing and Verification

Use tools like ab (Apache Benchmark) or wscat to test connections:

wscat -c ws://localhost:8080

Run a script to test concurrent connections:

#!/bin/bash
for i in {1..100000}; do
  wscat -c ws://localhost:8080 &
done

Monitor CPU, RAM, and connection count using netstat or ss:

ss -s

5. Troubleshooting and Notes

  • Ensure the VPS has at least 16GB RAM and 4+ CPU cores.
  • Do not enable tcp_tw_recycle on IPv6 or NAT systems.
  • Monitor system resources using htop or vmstat.
  • Test in a staging environment before production deployment.

References

DPTCloud Tier III Infrastructure

High-RAM Cloud VPS Plans Optimized for AI, Databases & Cache

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02Recommended

VPS-4E

297.000 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe
ECO · vn-02

VPS-5E

614.900 ₫ / mo

10% VAT included

  • CPU: 8 vCPU
  • RAM: 16 GB
  • SSD: 100 GB NVMe
ADVANCED · vn-03Recommended

VPS-4T

548.900 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478