Tips for Deploying Node.js Applications into Production

Are you willing to create server applications in JavaScript? Nothing can beat the leading tool, Node.js that offers the functionality of both a web server and an application server. Being a single-threaded and uses nonblocking I/O, the tools allow us to scale and support tens of thousands of concurrent operations. It shares these architectural characteristics with NGINX and solves the C10K problem.

But that doesn’t mean that the tool is perfect.

Node.js app deployment

What could possibly go wrong is, the tool comprises of a few weak points and vulnerabilities that can make Node.js based systems prone to underperformance or even crashes. And such problems arise more frequently when a Node.js‑based web application experiences rapid traffic growth.

However, the tool is great when it comes to creating and running application logic that produces the core, variable content for your web page. But when it comes to serving static content-it’s not so great! I mean whether it is about images and JavaScript files, for example – or load balancing across multiple servers.

Node.js Applications Deployment Tips

Now once you are done with your Node.js set up in a local development environment, there are some further steps to take before running this setup. In the following post, I would like to shed some light on such a few tips and tricks on deploying scalable Node.js applications into production.

1. Get your deployment architecture right

A typical deployment might look like this.

Node.js App Architecture

2. You can even make use of a reverse proxy

Never tend to expose your Node.js app server on the direct basis of incoming HTTP traffic. Instead, always put a reverse proxy in front of it to handle a bunch of concerns such as:

  • SSL termination
  • Serving static content
  • Caching slow changing parts of the site
  • Gzip compression and more

3. Deploy a cluster

Node.js is an event loop, a single-threaded one that comprises of additional threads for handling file and network events. This architecture restricts each instance to one logical CPU core. So what can be done is deploy a cluster of Node.js app servers to maximize usage of multicore systems and also high availability.

You will come across a bunch of multiple options such as PM2 and StrongLoop Cluster Management. However, most of node.js developers end up using StrongLoop’s cluster solutions, because many of our applications are built on Loopback API and also it has nice features like options to run as background process (detached), clustered (with options of specifying number of CPU cores), non-profiled, profiled, with Log aggregation and routing options, with ability to send PIDs to file for history tracking.

4. Set up a load balancer

No matter how much you tune a single instance Node.js app server, it can only handle so much traffic. For horizontal scaling, set up a load balancer like HAProxy or Nginx. And since StrongLoop Process Manager integrates with Nginx Controller, we always use Nginx as the load balancer.

One thing to consider while load balancing is whether your application is sticky sessions. In fact, it may quite interest you to know that Nginx plus supports sticky sessions. Besides, you can use tokens and move your session to a shared store like Redis. In general, for high-performance applications, it is not good to have sticky sessions.

5. Automatic restarts

All you require to do is set up the automatic restart of Node.js processes using something like StrongLoop PM or Forever. Also, add it to init.d just to make sure things restart on server restart.

6. Monitor and collect metrics

If you have the budget, make use of commercial APM solutions like AppDynamics to monitor and collect metrics on your production setup. Or you can also hook up StrongLoop PM to Graphite to achieve a degree of this.

7. Security

A rich plugin Node.js ecosystem makes it superior. So chances are there that your professionals might inadvertently include a plugin that is not secure. And that is why we suggest you ask your professional to maintains a private NPM repository of whitelisted which includes only plugins that go through the review process.

8. Logging

Here it’s time to make sure your application uses an asynchronous performance logger like Winston or Bunyan.

9. Automate everything

With the help of LambdaCD, you can define your infrastructure as code using and automate everything including environment-specific configuration.

Leave a Comment

Your email address will not be published. Required fields are marked *