NodeJS + SocketIO: Scaling and Preventing a Single Point of Failure

So, the first application that people usually create using SocketIO and Node is usually a chat application. This chat application basically has 1 Node server, which will be broadcast to multiple clients. In Node code, you have something like.

//Psuedocode for(client in clients){ if(client != messageSender){ user.send(message); } } 

This is great for a small number of users, but I see a problem with this. First of all, there is one point of failure, which is the Node server. Secondly, the application will slow down as the number of customers grows. What to do when we reach this bottleneck? Is there an architecture (horizontal / vertical scaling) that can be used to solve this problem?

+4
source share
2 answers

To do this, β€œone day”, when your chat application needs several fault-tolerant node servers, and you want to use socket.io for cross-communication between the server and the client, there is a node.js module that corresponds to the account.

https://github.com/hookio/hook.io

This is basically an event emitting a framework for cross-sharing between several "things" - such as multiple node servers.

This is relatively difficult to use compared to most modules, which is understandable, since it is a difficult task to solve.

In doing so, you will probably have to have several thousand simultaneous users and many other problems before you have problems with this.

Another thing you can do is try to develop the application in such a way that if the connection is lost (which happens all the time), for example. the server is leaving, the client has problems with the network (for example, a mobile user), etc., your application should be able to deal with this and cure such problems.

+4
source

Since Node.js has one stream of events, this single point of failure is recorded in its DNA. Even rebooting the server after code changes requires this thread to be stopped.

However, there are many tools to flexibly manage such failures. You can use forever ; simple CLI tool to ensure continuous execution of a given script. Other options include distribute and up . Distribution is a load balancing middleware for Node. Up builds on top of Distribute to offer zero-downtime reboots using either the JavaScript API or the command line interface:

Further reading I believe that you just need to use the Redis Store with Socket.io to support links to connections between two or more processes / servers. These options have already been discussed here and here .

You can also use socket.io-clusterhub if you are not going to use Redis storage.

+2
source

Source: https://habr.com/ru/post/1401739/


All Articles