How to create a distributed node.js web server

Suppose I need to implement a web application that will have a large volume of concurrent users. I decided to use node.js because it scales very well, has good performance, an open source community, etc. Etc. Then, to avoid bottlenecks, because I could have gazillions of users in a single event loop, I decided to use a process cluster to use a multi-core processor. In addition, I have 3 machines (main + 2), because I need to manipulate big data with Cassandra. Surprisingly, this means that I have 3 * n node.js processes, where n is the number of processor cores (the machines are identical).

Ok, then I start the study, and I end with the following scheme:

  • Nginx listens on port 80 and is used only for static content (img, css, js, etc.).
    Passes dynamic traffic to haproxy. I know how to configure nginx, but I still need to take a look at haproxy, so I will say that haproxy is listening on port 4000. Nginx and haproxy are installed on the main machine (entry point).
  • Haproxy load balances between three machines. It redirects traffic to port 4001, i.e. node.js processes are listening on 4001.
  • Each node.js has a cluster of n processes listening on 4001.

If I'm right, one HTTP request will be redirected to one node.js. process.

Creating a session is fine, right? A session is just a map, and this map is an object, and this object is in the process of node.js. Haproxy will be configured using a round-robin scheduler, so the same user can be redirected to different node.js processes. How can I share the same session object in all node.js processes? How can I exchange a global object (this includes the same computer (cluster node.js) and over the network)? How do I create a distributed web application with node.js? Are there any modules that make synchronization easier?

+6
source share
3 answers

You can use memcache or redis to store session objects. This is very useful if restarting node processes (if session data is stored in process memory, it will be lost).

You can also check the pm2 list of functions, and maybe some of them will be useful to you.

Building a microservice architecture will provide good scalability.

+1
source

As Ivan pointed out, you store your session objects in memcache or redis or even Couchbase (a memcache bucket). I would also like to add, if you want to build a scalable system, your goal should be to build the system in such a way that you can scale linearly to increase bandwidth on demand. By this, I mean that you should be able to add more hosts at any time (preferably during the peak) to different levels within your infrastructure to handle requirements.

So, you have to be very careful about what technology you choose and about the design decisions that you make during development.

Suppose I need to implement a web application that will have a large volume of concurrent users.

Another thing that I would like to add is, if you cannot measure it, you cannot overcome it. A good start would be to determine what β€œlarge volume of concurrent users” means to you? Is that what facebook or whatsApp volume type / concurrency? Define them first, working with your stakeholders (if any), then you can start making design decisions and choosing technologies.

A good litmus test when building a scalable system is to ask yourself: "Is there one point of failure?" if so, then your system will not scale.

+1
source

As suggested by another user; using Redis is a perfectly acceptable solution to this problem.

The disadvantage of this is the use of the service to store session objects and middleware. As mentioned earlier, this is useful in cases of node process restart, failure, etc. Saving session data in a node process carries risks. One of the benefits of using microposts (like Redis) is that these risks are reduced.

Assuming you're using Express for your middleware, you can use something called the Session store . There are many modules that use this feature.

One of these connect-redis modules

Installation is as usual:

 npm install connect-redis express-session 


Then you will use it like this:

 var session = require('express-session') var RedisStore = require('connect-redis')(session) app.use(session({ store: new RedisStore(options), secret: 'keyboard cat' })) 

Now you are using your session object as usual. ( req.session )


<strong> Examples:

To set session information (for example, from a POST form):

 req.session.email = req.body.email 


To get session information:

 console.log( req.session.email ) 
0
source

All Articles