ThisCalculator

Everyday

WebSocket Capacity: How Many Connections Per Server?

Updated · Standard formula, shown below

Average pace 5.00 min/km
Average speed
12.00 km/h
Result
Finish time
50 min
You entered
Distance
10 km
You entered

One server can handle tens of thousands to millions of concurrent WebSocket connections, depending on hardware, operating system settings, and application design. The practical limit is rarely the WebSocket protocol itself—it’s the server’s memory, file descriptor limits, and how efficiently the application manages each connection.

Formula

The calculator above is a pace/speed converter, not a WebSocket capacity calculator—because WebSocket capacity has no single formula. It depends on variables like memory per connection, CPU overhead, and network bandwidth. However, a rough estimate for memory usage is:

Total memory ≈ (connections × memory per connection) + application overhead

Where connections is the number of active WebSocket clients, memory per connection includes the socket buffer, protocol state, and application data (typically 20–100 KB), and application overhead covers the event loop, timers, and shared resources.

Worked Example

Using the default values from the calculator: finish time = 50 minutes, distance = 10 km. Pace = 50 / 10 = 5 min/km. Speed = 10 / (50 / 60) = 12 km/h. That’s the pace/speed conversion. For WebSocket capacity, a similar “default” example: if a server has 8 GB RAM and each connection uses 50 KB, then 8,000 MB / 0.05 MB = 160,000 connections—before other processes consume memory. This illustrates the order-of-magnitude thinking needed.

What Actually Limits WebSocket Connections

WebSocket connections are long-lived TCP connections. Each one consumes a file descriptor (FD) on the server. The OS has a default FD limit (often 1024 per process, but can be raised to 65535 or more). The ulimit -n command shows the current limit. To support 100,000 connections, you need at least 100,000 FDs available. Also, each TCP connection has a kernel buffer (send and receive), typically 64–256 KB combined. Multiply that by connections to get kernel memory usage.

Application-level memory is often the bigger factor. In Node.js, each WebSocket connection might use 30–80 KB for the socket object, plus any user data. In Go, goroutines are lightweight (2 KB stack), so memory per connection can be lower. In Java, each connection may use more due to object overhead. The event loop model matters: single-threaded languages like Node.js can handle many idle connections because they don’t block, but CPU-heavy work per message will reduce capacity.

How to Estimate Your Server’s Capacity

To estimate, you need to know:

  • Memory per connection (measure or approximate: 20–100 KB)
  • File descriptor limit (check with ulimit -n)
  • Network bandwidth (each connection may send/receive data; bandwidth per connection = total bandwidth / connections)
  • CPU per message (if you process messages, measure CPU time per message)

Example: A 4-core server with 16 GB RAM, 1 Gbps network, and a lightweight chat app. If each connection uses 50 KB, memory allows ~320,000 connections. FD limit can be set to 500,000. Bandwidth: if each client sends 1 KB/s, 100,000 connections = 100 MB/s = 800 Mbps, close to the limit. So bandwidth becomes the bottleneck.

Common Misconceptions and Mistakes

One common mistake is assuming the OS default limits are fine. Most Linux systems have a default FD limit of 1024 per process. You must raise it via ulimit and system-wide limits in /etc/security/limits.conf and sysctl settings like fs.file-max. Another mistake is ignoring the TCP backlog and keepalive settings. Also, people forget that each connection has a unique IP:port pair; the server’s IP and port are fixed, but the client side uses ephemeral ports—this is usually not a limit for the server, but the server must handle many connections from the same IP (e.g., NAT).

Another misconception: WebSocket connections are “free” when idle. They still consume memory and FDs. Also, using a single-threaded event loop (like Node.js) can handle many idle connections, but if one connection sends a heavy message, it blocks others. Load balancing across multiple processes or servers is often necessary.

Scaling Strategies

When one server isn’t enough, you have options:

  • Vertical scaling: Add more RAM, CPU, and raise OS limits. This is simple but has a ceiling.
  • Horizontal scaling: Run multiple server instances behind a load balancer that supports WebSocket (e.g., HAProxy, Nginx, or cloud load balancers). The load balancer must support sticky sessions or a shared pub/sub layer (like Redis) to broadcast messages.
  • Use a messaging layer: For chat or real-time updates, use Redis Pub/Sub or a message queue to synchronize state across servers.
  • Connection pooling: For outgoing WebSocket connections (e.g., to external APIs), reuse connections rather than opening new ones.

Remember that WebSocket is stateful, so horizontal scaling requires a strategy for routing messages to the right server (e.g., consistent hashing or a lookup table).

Comparing Languages and Frameworks

Different stacks have different capacities. Node.js with the ws library is popular and can handle ~100k connections on a decent server, but CPU-bound tasks will hurt. Go with goroutines is very efficient; a simple WebSocket server can handle 1M+ connections on a large server. Java with Netty is also high-performance. Python (asyncio) is less efficient but fine for a few thousand. The choice depends on your team’s expertise and the workload: if you’re broadcasting to many clients, the message distribution logic matters more than the language.

Cost and Hardware Considerations

Running a server that handles 100k connections typically requires:

  • 8–16 GB RAM (for application and kernel buffers)
  • 4–8 CPU cores (for processing messages)
  • 1 Gbps network (if each client sends a few KB/s)

Cloud instances with these specs cost a few hundred dollars per month, but prices vary. A more efficient approach is to use a managed WebSocket service (like AWS API Gateway WebSocket or Azure Web PubSub), which handles scaling for you, but at a per-connection cost. For a hobby project, a single $5 VPS can handle a few thousand connections if optimized.

Safety and Reliability Tips

To keep your server stable:

  • Set a maximum number of connections per process and reject new ones with a 503 or close code 1013 (Try Again Later).
  • Implement heartbeat ping/pong to detect dead connections and free resources.
  • Use a timeout for idle connections if your app doesn’t need them.
  • Monitor memory and FD usage; set alerts at 80% of limits.
  • Test with a load tool like websocat or a custom script that opens many connections.

Also, be aware of security: WebSocket connections bypass the same-origin policy, so validate the Origin header to prevent cross-site WebSocket hijacking. Use WSS (TLS) for encryption, but note that TLS adds CPU overhead.

Frequently Asked Questions

What is the maximum number of WebSocket connections on one server?

There’s no hard protocol limit. With proper tuning, a single server can handle 100k–1M connections. The limit is set by memory, file descriptors, and CPU. For example, a 16 GB RAM server with 50 KB per connection can hold ~320k connections, but you also need FDs and network bandwidth.

Does the WebSocket protocol itself limit connections?

No. WebSocket is just a message framing protocol over TCP. The limit is on the TCP layer and the application. TCP has a 16-bit port number, but the server listens on one port; the client’s source port is unique, so the server can handle millions of connections as long as the OS and app can.

How do I increase the file descriptor limit for WebSocket?

Use ulimit -n 100000 for the shell, and set fs.file-max in /etc/sysctl.conf for the system. Also, in systemd service files, set LimitNOFILE=100000. Restart the service after changes.

Why does my server crash at 1024 connections?

That’s the default FD limit. Each connection uses one FD. Raise the limit with ulimit -n and also check the system-wide limit. Also, ensure the application doesn’t leak FDs (e.g., not closing sockets properly).

Is it better to use multiple servers or one big server?

It depends. A single big server is simpler and cheaper up to a point. Multiple servers provide redundancy and horizontal scaling, but require a load balancer and a way to share state (like Redis). For most applications, start with one server and scale out when you hit ~50–70% of capacity.

Can I use WebSocket with a free hosting service?

Some free tiers (like Heroku’s free dyno) support WebSocket but have limited connection counts and may sleep after inactivity. For production, you’ll need a paid plan or a VPS. Managed services charge per connection, so for many connections, a VPS is more cost-effective.

Related

Formula used
in t | Finish time | min | 50 | 0
in d | Distance | km | 10 | 0
out pace | Average pace | min/km | t / d | 2
out speed | Average speed | km/h | d / (t / 60) | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.