Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run WebSockets with multiple server instances?

Tags:

websocket

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?

like image 943
B Seven Avatar asked Mar 19 '13 23:03

B Seven


People also ask

Can you have multiple WebSocket 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.

How many WebSocket a server can handle?

By default, a single server can handle 65,536 socket connections just because it's the max number of TCP ports available.

Would WebSockets be able to handle 1000000 concurrent connections?

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.

Can WebSocket go through load balancer?

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.


2 Answers

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

like image 136
CallMeLaNN Avatar answered Oct 09 '22 21:10

CallMeLaNN


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.

like image 27
Umair Anwar Avatar answered Oct 09 '22 21:10

Umair Anwar