Node.js project to production with a lot of traffic

479 Views Asked by At

I started a project and I started to have a lot of web traffic, and at some point I felt insecure of how to scale the project to production in two issues:

  1. How to perform updates without leaving my users without service?
  2. How to correctly configure Node.js so that it consumes less memory?

I have microservices working with Hydra-express, and I have not been able to implement Hydra-router and I do it with Express.js; I also have NGINX as a proxy gateway.

I am programming in ES6, transpiling with BABEL and maintaining active microservices with PM2, some with fork and the most important in cluster mode.

I was thinking about using docker, but I have not found any tutorial on how to use it with CDN, upload files and serve them to the user.

2

There are 2 best solutions below

0
Renato Gama On BEST ANSWER

Adding to @ralphtheninja answer, I suggest you reading more about blue green deployments, as proposed by Martin Fowler; https://martinfowler.com/bliki/BlueGreenDeployment.html

One of the challenges with automating deployment is the cut-over itself, taking software from the final stage of testing to live production. You usually need to do this quickly in order to minimize downtime. The blue-green deployment approach does this by ensuring you have two production environments, as identical as possible. At any time one of them, let's say blue for the example, is live. As you prepare a new release of your software you do your final stage of testing in the green environment. Once the software is working in the green environment, you switch the router so that all incoming requests go to the green environment - the blue one is now idle.

Blue-green deployment also gives you a rapid way to rollback - if anything goes wrong you switch the router back to your blue environment.

I have no idea where your backend is running but there are some services that will do it for you, AWS ElasticBeanstalk for example will put your instances behind a load balancer and will manage deployment according to a policy. have a look; https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.rolling-version-deploy.html.

0
rtn On

It's impossible to give a definite answer to 2 since that is completely up to what the application does, there's no silver bullet configuration that you can apply.

This leaves the first point, which is around something called zero downtime.

So, in the context of having multiple servers returning content to users, e.g. http servers I guess it's fair to say that most production environments have something at the front that's not related to the business logic. This could be a load balancer (comes in many shapes and forms) or a reverse proxy. This is usually the spot where you point your DNS A record. This server should basically never be down.

Now, lets assume you have changed some business logic and want to deploy a new backend. What you normally do is to swap out the already running processes behind the load balancer (or reverse proxy), one by one. So if you have five node processes, you stop one, start up a new with the updated code, and repeat until all running have been swapped out.

You can also utilize this to swap out just one, run tests on that one, then proceed to swap out the rest.

To really make sure you don't disrupt any users, you should stop accepting new http requests on the old processes, so new http requests are routed to the updated processes. This will allow for http requests that are taking place to finish up. Then you stop the old processes.

Hopes this helps.