Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS horizontal scaling

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?

like image 732
Casey Flynn Avatar asked Nov 12 '12 02:11

Casey Flynn


People also ask

How do I horizontally scale node JS application?

Clustering is a technique used to horizontally scale a Node. js server on a single machine by spawning child processes (workers) that run concurrently and share a single port.

Is node good for scaling?

Node.js offers Easy Scalability With Node. js, you'll have no problems scaling the application horizontally across multiple servers or vertically to increase its performance on a single server.

Why Nodejs is highly scalable?

Where Node. js really shines is in building fast, scalable network applications, as it's capable of handling a huge number of simultaneous connections with high throughput, which equates to high scalability.

How do I scale a node JS project?

The easiest thing to do to scale a big application is to clone it multiple times and have each cloned instance handle part of the workload (with a load balancer, for example). This does not cost a lot in term of development time and it's highly effective. This strategy is the minimum you should do and Node.


2 Answers

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.

like image 198
JohnnyHK Avatar answered Sep 28 '22 07:09

JohnnyHK


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.

like image 20
Pramma Avatar answered Sep 28 '22 07:09

Pramma