3

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?

tannerwj
  • 154
  • 3
  • 11

2 Answers2

6

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).

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • That's what I was thinking. Would there ever be the circumstance in which the primary server were to go down and some clients would connect to the backup, and then the primary to go back online, and some clients would connect back to the primary? If so, is there a way to make sure they are all on the same server? – tannerwj Jan 13 '15 at 00:48
  • 1
    @Tanner - It is up to your network infrastructure to decide which server a given incoming connection goes to if there are multiple servers available to serve the same IP address. There are many ways to control this (load balancer, proxy, DNS fail-over, etc...). This problem is usually dealt with at the network level, not at the server level. – jfriend00 Jan 13 '15 at 01:14
3

SocketIO in primary and backup server can be connected to a redis server. This will maintain the sessions in the primary server and can be used by backup server, The clients should once again connect to the new server(when primary fails).

SoketIO- Redis

HA - proxy is used for load balancing between multiple node.js instances. The usage of HA proxy will depend on how you are going to deal with failure of primary server. If you have any method to automatically switch primary server, then HA-proxy will not be much useful, else you can configure HA-Proxy to forward request to backup server if the primary server is unreachable.

Other options similar to HA-Proxy are:

node-http-proxy

Nginx

Community
  • 1
  • 1
Arjun
  • 577
  • 6
  • 13