10

I've been a ruby/php web application developer for quite some time and I'm used to the idea of horizontal scaling of server instances to handle more requests. Horizontal scaling - meaning separate instances of an application sitting behind a load-balancer that share nothing and are unaware of each other.

The main question I have is, since Node.js and it's emphasis on evented-io allows for a single box running a node.js server to handle 'thousands' of simultaneous requests - is load-balancing/horizontal scaling used to scale nodejs applications? Is scaling a node app limited to vertical scaling (throwing more RAM/Processing power at the problem)?

My second question has to do with node.js horizontal scaling and websockets. I've seen quite a few Node.js 'chat' tutorials out there that make use of websockets. (favorite: http://martinsikora.com/nodejs-and-websocket-simple-chat-tutorial)

Since websockets effectively keep an open line of communication open between a browser and a server, would a horizontally scaled architecture typical of the PHP/Ruby world cause a chat application like the one explained in the link to break - as new websocket connection requests would be assigned to different processes/servers and there would be no one central resource tracking all connected clients?

3 Answers 3

13

Node.js supports horizontal scaling in much the way you describe via the built-in cluster module.

Regarding your second question about the use of websockets/socket.io in this environment, you have to use something like Redis to store shared state across multiple instances of your application as described here.

1
  • This is an old question but I am using mongodb to store and retrieve all my chat application data. Since I want to horizontally scale my application as my chat application doesn't work when I have more than one server. What should I do should I remove mongodb completely and use redis? Or is there a better solution.
    – DragonBorn
    Nov 22, 2017 at 7:47
2

Node.js's cluster functionality is limited to single server with multiple processor. Mainly it leverages number of processors in server. I think the question if more about the scenario when we want to scale horizontally with multiple servers with a Load balancer facade.

1
  • And to answer the question about socket, just like any server call, socket creation call will be load balanced and say reaches on node Nx, for the life time of the web socket this TCP connection is kept open. i.e. all communication from browser to server, server to browser thru web socket will always be served by Nx.
    – Pramma
    Feb 27, 2015 at 15:42
0

If you have node.js instances spread across multi servers(horizontal scaling), it will serve the same purpose, you need to program it properly to support this type of setup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.