Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js & Socket.io with High Availibility

We have a node.js server that is primarily used with socket.io for browser inter connectivity in a web application.

We want to have a high availability solution which would theoretically consist of two node.js servers, one as a primary server and the other as a backup should the primary fail. The solution would allow that if or when the primary node.js server goes down the backup would take over to provide seamless functionality without interruption.

Is there a solution that allows socket.io to maintain the array of client connections over multiple servers without duplication of clients or of messages sent?

Is there another paradigm we should be considering for HA and node.js?

like image 888
tannerwj Avatar asked Jul 17 '26 13:07

tannerwj


1 Answers

There is no way to have a webSocket auto fail-over without any interruption to a new server when the one it is currently connected to goes down. The webSockets that were connected to the server that went down will die. That's just how TCP sockets work.

Fortunately with socket.io, the client will quickly realize that the connection has been lost (within seconds) and the clients will try to reconnect fairly quickly. If your backup server is immediately in place (e.g. hot standby) to handle the incoming socket.io connections, then the reconnect will be fairly seamless from the client point of view. It will appear to just be a momentary network interruption from the client's point of view.

On the server, however, you need to not only have a backup, but you have to be able to restore any state that was present for each connection. If the connections are just pipes for delivering notifications and are stateless, then this is fairly easy since your backup server that receives the reconnects will immediately be in business.

If your socket.io connections are stateful on the server-side, then you will need a way to restore/access that state when the backup server takes over. One way of doing this is by keeping the state in a redis server that is separate from your web server (though you will then need a backup/high availability plan for the redis server too).

like image 108
jfriend00 Avatar answered Jul 20 '26 04:07

jfriend00



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!