It is useful to run multiple instances of HTTP servers for scaling.
However, it seems that this would not work with WebSockets because each server instance would have its own set of connections to clients.
How can you run WebSockets on multiple server instances if they all need to share a common set of connections?
A server can open WebSocket connections with multiple clients—even multiple connections with the same client. It can then message one, some, or all of these clients. Practically, this means multiple people can connect to our chat app, and we can message some of them at a time.
By default, a single server can handle 65,536 socket connections just because it's the max number of TCP ports available.
The answer is complicated by several factors, but 1,000,000 simultaneous active socket connections is possible for a properly sized system (lots of CPU, RAM and fast networking) and with a tuned server system and optimized server software.
The load balancer knows how to upgrade an HTTP connection to a WebSocket connection and once that happens, messages will travel back and forth through a WebSocket tunnel. However, you must design your system for scale if you plan to load balance multiple WebSocket servers.
I was looking for the same thing to develop websocket app ready to scale, and apparently not much article discuss about this.
Case 1 The recommended approach is using Redis cache like Heroku suggest, use Service Bus or store into db like the 3 approaches mentioned in Scaleout in SignalR. However there is a catch, there must be a slight latency because in order the second instance want to know, scheduler or background worker/service should be run continuously to detect new changes in the cache/queue/db.
Case 2 In my case I want high frequency nearly real time. So it should have a persistence connection(s) between instances. However the challenge, we can't do that between stateless web server.
So we still need intermediate long running background/worker service and implement our own net/socket/TCP from code and maintain the connection pool but that is not an easy task.
I am thinking of using Websocket client on server (for this service) so the pattern to communicate with client and between instances is almost the same. See this: 1 2. One thing left is to update the instance IP/URL into distributed cache or db on every instance up/down.
Last option is to explore on a new WebRTC
I havn't tried it yet but i am thinking of making connections sticky may solve the problem. You can set this setting in your load balancer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With