It is said that everyone should have a hobby, and my personal favorite is NodeJS development. Apart from being a leading tool to create server applications in JavaScript, it’s one of the most popular programming languages. Known for offering the functionality in both an application server and web server, Node.js is an essential tool for all the kinds of microservers-based development and delivery. However, when it’s time to take your application and server to its users in a production environment, few things are mandatory to understand to dodge terrible performance and keep your application from trashing.
I am writing this blog to discuss heavy production load and reliability for the applications deployed to production. I have categorized this topic into the “DevOps” world, both operational and traditional operations. So, the information is divided into two different parts.
Make sure to follow these things in your code;
- Don’t use synchronous functions
- Handle exceptions properly
- Use gzip compression
- Do logging correctly
Things to do in your Setup
- Use a load balancer
- Set NODE_ENV to production/development
- Make sure your app restarts automatically
- Run your app in a cluster
- Cache request results
- Make use of a reverse proxy
1. Things to do in your code to improve your application’s performance
- Handle exceptions properly
- Do logging correctly
- Don’t use synchronous functions
- Use gzip compression
We recently got an opportunity to build a project to resolve mathematical equation solution, and we had an obvious idea that load balancer in front of our application with an auto-scaling won’t be a good idea. So, we found it listed below tips very useful.
2. Increase Memory Limit for Your Process:
By default, Node.js is configured to the 1GB limit. Considering your server has assigned 4GB for your application will manually set the max memory limit by utilizing Node CLI and the following flag –max-old-space-size
3. Ensure To Use All Of Your CPU Cores:
If you have not defined any special configuration that runs multiple threads of Node on the same server, then save your money and choose a server with just one core.
4. Be Careful Before Using Synchronous Functions
A single call to a synchronous function can return in a few milliseconds. In the case of high-traffic websites, such requests lessen the performance of the application. Make sure to avoid synchronous functions used in production. At the time of the initial startup, the synchronous function is worth justifying.
If you are explicitly logging for the specific purpose of debugging, then instead of using console.log(), ensure to use debug. It will enable you to use DEBUG environment control what debug messages are sent to console.err().
5. Error Handling:
Make sure to use a logging tool Logz,io/AWS CloudWatch to find out the errors that can effortlessly fail your app. Here I would like to advise you to not errors to services like Slack as it usually comes in masses and there are high chances that it can block your application for throttling. My favorite library for logging is Winston.
6. Use Promises
Promises will take care of exceptions in asynchronous code blocks.
app.get('/', function (req, res, next) { // do some sync stuff queryDb() .then(function (data) { // handle data return makeCsv(data) }) .then(function (csv) { // handle csv }) .catch(next) }) app.use(function (err, req, res, next) { // handle error })
Source: expressjs.com
7. Use a Reverse Proxy
Usually, a reverse proxy performs supporting operations in front of a web application on the requests, instead of directing requests to the application, it has the capability to handle compression, error pages, serving files, caching and load balancing among other things. Handling tasks does not require proficient knowledge of the application to reverse proxy frees to perform specific application tasks. This is the reason; I would like to suggest you express a reverse proxy like HAProxy or Nginx in the production.
At Bacancy Technology, we used above-mentioned tips and it resulted in tenfold enhancement in the application’s performance and also helped in terms of reliability and heavy production load to serve thousands of users. If you find above-mentioned tips useful want to leverage most robust and scalable technology to develop high-performing and data-intensive applications, then hire Node.js developer from us to build ultrafast lightweight applications to deal with heavy production load.