Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a distributed node.js application?

Creating a node.js application is simple enough.

var app = require('express')(); app.get('/',function(req,res){     res.send("Hello world!"); }); 

But suppose people became obsessed with your Hello World! application and exhausted your resources. How could this example be scaled up on practice? I don't understand it, because yes, you could open several node.js instance in different computers - but when someone access http://your_site.com/ it aims directly that specific machine, that specific port, that specific node process. So how?

like image 780
MaiaVictor Avatar asked Mar 15 '13 05:03

MaiaVictor


People also ask

Is Node js good for data intensive applications?

The asynchronous feature is what makes the single-thread of the Node. js program quite efficient. It makes the program fast and scalable without using as many resources as a multi-threaded application. Naturally, this makes Node a good fit for data-intensive and I/O intensive programs.

Can you build apps with node js?

Node. js can be used to build both simple and advanced fintech applications that require a lot of data processing real-time communication. Node's scalable features make it perfect for this kind of application.

What is a distributed node?

A distributed system contains multiple nodes that are physically separate but linked together using the network. All the nodes in this system communicate with each other and handle processes in tandem. Each of these nodes contains a small part of the distributed operating system software.


1 Answers

There are many many ways to deal with this, but it boils down to 2 things:

  1. being able to use more cores per server
  2. being able to scale beyond more than one server.

node-cluster

For the first option, you can user node-cluster or the same solution as for the seconde option. node-cluster (http://nodejs.org/api/cluster.html) essentially is a built in way to fork the node process into one master and multiple workers. Typically, you'd want 1 master and n-1 to n workers (n being your number of available cores).

load balancers

The second option is to use a load balancer that distributes the requests amongst multiple workers (on the same server, or across servers).

Here you have multiple options as well. Here are a few:

  • a node based option: Load balancing with node.js using http-proxy
  • nginx: Node.js + Nginx - What now? (using more than one upstream server)
  • apache: (no clearly helpful link I could use, but a valid option)

One more thing, once you start having multiple processes serving requests, you can no longer use memory to store state, you need an additional service to store shared states, Redis (http://redis.io) is a popular choice, but by no means the only one.

If you use services such as cloudfoundry, heroku, and others, they set it up for you so you only have to worry about your app's logic (and using a service to deal with shared state)

like image 50
Pascal Belloncle Avatar answered Oct 08 '22 08:10

Pascal Belloncle