Search

Top tips for Node JS Production Ready Setup [2021 Edition]



While Developing Node Js App it is very important that you deploy them to production with some procedures so they work good. As a developer I have always faced this problems "IT WORKED IN DEV BUT WHY NOT IN PROD"

HERE ARE FIVE IMPORTANT STEPS TO CONSIDER WHILE DEPLOYING A NODEJS APP TO PRODUCTION.

This are my personal things I do when setting up Node JS for production.

#1] USE A REVERSE PROXY

Never expose a Node JS server to Incoming traffic always use a reverse proxy to handle issues like:


  1. SSL Termination.

  2. Serving static content.

  3. GZip Compression.

  4. Enterprise routing.

  5. Clustering.


#2] DEPLOY A CLUSTER

Node.js event loop is single threaded, though it has additional threads for handling file and network events. This architecture restricts each instance to one logical CPU core.


So, deploy a cluster of Node.js app servers to maximize usage of multicore systems and also high availability.


There are multiple options for this, such as PM2 and StrongLoop Cluster Management. We usually 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.

#3] SETUP 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, setup a load balancer like HAProxy or Nginx. One thing to consider while load balancing is whether your application is sticky sessions. Nginx pl3us supports sticky sessions. Or 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.

#4] AUTOMATIC RESTARTS

Setup automatic restart of Node.js processes using something like StrongLoop PM or Forever. Also, to make sure things restart on server restart, add it to init.d also.

#5] MONITOR AND LOG EVERYTHING

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

Hope you all like this post. If you like it please share and subscribe to the email newsletter. I will be providing more and more informative tutorials. If any question please comments I will try to solve your queries.

8 views0 comments

Recent Posts

See All