user2344173
user2344173

Reputation: 121

Nodejs failover

I am a beginner in nodejs. I am trying to use nodejs in production. I wanted to achieve nodejs failover. As I am executing chat app, if a node server fails the chat should not breakup and should be automatically connected to a different nodeserver and the same socket id should be used for further chatting so that the chat message shouldn't go off. Is this can be achieved? Any samples.

I should not use Ngnix/HAProxy. Also let me know how the node servers should be: Either Active-Active or Active-Passive

Upvotes: 9

Views: 5443

Answers (5)

Gautam Anand
Gautam Anand

Reputation: 19

You could try usingPm2 start app.js -I 0. This would run your application in cluster mode creating many child processes for same thread. You can share socket information between various processes.

Upvotes: 0

user5671078
user5671078

Reputation:

PM2 is preferred to be the manager of process, especially the features of auto-failover, auto-scailing, auto-restart .

The introduction is as followed,

PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

Starting an application in production mode is as easy as:

$ pm2 start app.js

PM2 is constantly assailed by more than 700 tests.

Official website: http://pm2.keymetrics.io

Works on Linux (stable) & MacOSx (stable) & Windows (bêta).

Upvotes: 8

An Nguyen
An Nguyen

Reputation: 126

For a simple approach, I think you should build a reconnect mechanism in your client side and use a process management as forever or PM2 to manage your Node.js processes. I was trying too many ways but still can't overcome the socket issue, it's always killed whenever the process stops.

Upvotes: 1

David
David

Reputation: 974

There's several problem's you're tackling at once there:

Daemonization - keeping your app up: As already mentioned, scripts such as forever can be used to supervise your nodeJS application to restart it on fail. This is good for starting the application in a worst-case failure.

Similarly recluster can be used to fork your application and make it more fault-resistant by creating a supervisor process and subprocesses.

Uncaught exceptions: A Known hurdle in nodejs is that asyncronous errors cannot be caught with a try/catch block. As a consequence exceptions can bubble up and cause your entire application to crash.

Rather than letting this occur, you should use domains to create a logical grouping of activities that are affected by the exception and handle it as appropriate. If you're running a webserver with state, an unhandled exception should probably be caught and the rest of the connections closed off gracefully before terminating the application.

(If you're running a stateless application, it may be possible to just ignore the exception and attempt to carry on; though this is not necessarily advisable. use it with care).

Security: This is a huge topic. You need to ensure at the very least:

  1. Your application is running as a non-root user with least privileges. Ports < 1024 require root permissions. Typically this is proxied from a higher port with nginx, docker or similar.
  2. You're using helmet and have hardened your application to the extent you can.

As an aside, I see you're using Apache in front of NodeJS, this isn't necessarily as apache will probably struggle under load with it's threading model more than nodeJS with it's event-loop model.

Upvotes: 3

Gntem
Gntem

Reputation: 7165

assuming you use a database for authenticating clients, there isn't much into it to accomplish, i mean, a script to manage state of the server script, like forever does,

it would try to start the script if it fails, more than that you should design the server script to handle every known and possible unknown error, any signal send to it , etc.

a small example would be with streams.

(Websocket Router)
 |       
 |_(Chat Channel #1) \
 |_(Chat Channel #2) - Channel Cache // hold last 15 messages of every channel  
 |_(Chat Channel #3) / 
 |_(Authentication handler) // login-logout

-- hope i helped in some way.

Upvotes: 1

Related Questions