Things to keep in mind while building a scalable and efficient Node.js application

NODE-JS-Scalability-PerformanceAre you looking for a light weight, efficient, scalable platform to provide services for applications? then Node.js is a great and right choice. It is an interesting platform which allows you to write highly performance asynchronous code. Libraries also blur the line between server side development and developing for the browser. This quality makes Node.js a outstanding technology choice for application development.

Node.js gleams in real-time web applications and push technologies are being useful over websockets. Node.js built in support for package management using NPM tool. It comes with every Node.js installation by default. NPM tool is quite similar to Ruby Gems. It is very good and useful when you need to do several things at the same time. Node.js takes a very little time duration to build a marvellous application and one can build a highly scalable cloud based application with ease. Also allows you to create a global real-time logistic system.

Things to keep in mind while building a scalable and efficient Node.js application

A. Use Asynchronous Code:

  1. Asynchronous code allows applications to display information faster to user without the need for refreshing.
  2. Maximizing through put and efficiency using non-blocking I/O and asynchronous events.
  3. Makes Node.js application ideal for building harp platform.
  4. To scale to the large volume of clients all I/O intensive operations in Node.js are performed asynchronously.

B. Event Loop:

  1. Node.js maintains an event loop which manages all asynchronous operations for large no of clients and to avoid inefficiency and difficulties of programming threaded applications.
  2. Node.js sends an asynchronous task to the event loop, along with the call back function and continuous to execute rest of its program to perform blocking operations.
  3. Leaving responsibilities to the event loop makes life easy for Node.js developers.
  4. Event loop is a fundamental part of a Node.js by having a hold of how the event loop works you’ll be able to use its full advantages and avoid the pitfalls of this approach.
  5. Because of strange style of programming we need to look for patterns to help as effectively programming on the server that starts with event loop.
  6. Using event loop application can perform many tasks at a time efficiently, quickly.

C. Go Parallel

1. Full Parallel:

– For running asynch task all at the same time.

-Take a small set of operations and launch them all in a parallel.

-We take every item in the array and start asynchronous operation for each of the item immediately.

2. Limited Parallel:

-For running a limited number of asynchronous task at the same time.

-We launch asynchronous task once other task finish and then store the result from those task back in the result array.

D. Custer Module:

  1. Using Custer module you can take more effectively advantages of modern hardware.
  2. The cluster module allows you to easily create child processes that all share server ports.
  3. The cluster module allows you to create a small network of separate processes which can share server ports; this gives your Node app access to the full power of your server.
  4. If your application receives significant traffic, so you need scale out to multiple machines. This is possible with reverse proxy server to load balance the incoming request among multiple servers.
  5. Node.js is a single threaded by default but using new cluster module you can scale horizontally across multiple threads.
  6. Cluster module allows you to represent work to child processor. This means that node creates a copy of current program in another process. Each child processor has a special ability such as sharing socket with other children this allows us to write node program that starts many other programs and then delegate work to them.

E. Code Quality:

  1. Write as short code as possible.
  2. Re-use code as much as possible.
  3. Do not over or under abstract your design.
  4. Break things in module areas that can run on multiple systems.
  5. Run heavy processing to a separate system.
  6. That will keep your application running long into the future so you will not deal with maintenance issue.

F. Use Multiple Processors:

  1. Node is a single threaded. This means Node is using only one processor to perform its worth.
  2. However most servers have ‘several multicore’ processor and ‘single multicore’ processor has many processor.
  3. To make the best use of Node you should use a server with physical CPU socket. This is 24 processor exposed to the operating system.

G. Multy-Core CPU :

  1. Application program would be scalable if it would be moved from smaller to a large operating system and take full advantage of the larger operating system in terms of performance.
  2. Scale up: Faster CPU, more RAM, and more disk space
  3. Scale out: More core in CPU, more CPU’s, more servers
  4. Adding more resources is very good, attractive and quick to decision makers.

H. Use NGINX:

  1. Nginx is an open source HTTP server and reverse proxy which is extremely good at serving static files such as CSS and HTML.
  2. It is used to handle task such as coaching and serving the static content.
  3. Nginx lightens the incoming load on Node.js processes.

I. Error Handling:

  1. To build a strong, robust Node.js application it need to deal properly with errors.
  2. Always remember Node can crash for a single unhandled error so handle all errors properly and throw them with proper message.
  3. Avoid use of Try-Catch block, it slowdowns your process.

J. npm-a sanctification:

  1. npm is like a grace/blessing/sanctification to the Node.js developers.
  2. npm helps you to do interesting stuff and keeps your code simple.

K. Avoid Callback and instead use Event handler:

  1. Event handler is a specific case of call back.
  2. It is always better to use event handler than callback.
  3. Callback is executable code that passed as an argument to other code and event handler is asynchronous callback subroutine that handles inputs received in a program.
  4. Use event handler when something can happen unexpectedly