Are 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. We can also modify Node.Js app to run from a docker container.
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:
- Asynchronous code allows applications to display information faster to user without the need for refreshing.
- Maximizing through put and efficiency using non-blocking I/O and asynchronous events.
- Makes Node.js application ideal for building harp platform.
- To scale to the large volume of clients all I/O intensive operations in Node.js are performed asynchronously.
B. Event Loop:
- 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.
- 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.
- Leaving responsibilities to the event loop makes life easy for Node.js developers.
- 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.
- 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.
- 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:
- Using Custer module you can take more effectively advantages of modern hardware.
- The cluster module allows you to easily create child processes that all share server ports.
- 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.
- 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.
- Node.js is a single threaded by default but using new cluster module you can scale horizontally across multiple threads.
- 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:
- Write as short code as possible.
- Re-use code as much as possible.
- Do not over or under abstract your design.
- Break things in module areas that can run on multiple systems.
- Run heavy processing to a separate system.
- That will keep your application running long into the future so you will not deal with maintenance issue.
F. Use Multiple Processors:
- Node is a single threaded. This means Node is using only one processor to perform its worth.
- However most servers have 'several multicore' processor and 'single multicore' processor has many processor.
- 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 :
- 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.
- Scale up: Faster CPU, more RAM, and more disk space
- Scale out: More core in CPU, more CPU's, more servers
- Adding more resources is very good, attractive and quick to decision makers.
H. Use NGINX:
- Nginx is an open source HTTP server and reverse proxy which is extremely good at serving static files such as CSS and HTML.
- It is used to handle task such as coaching and serving the static content.
- Nginx lightens the incoming load on Node.js processes.
I. Error Handling:
- To build a strong, robust Node.js application it need to deal properly with errors.
- Always remember Node can crash for a single unhandled error so handle all errors properly and throw them with proper message.
- Avoid use of Try-Catch block, it slowdowns your process.
J. npm-a sanctification:
- npm to the Node.js developers is like a grace/blessing/sanctification.
- npm helps you to do interesting stuff and keeps your code simple.
K. Avoid Callback and instead use Event handler:
- Event handler is a specific case of call back.
- It is always better to use event handler than callback.
- 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.
- Use event handler when something can happen unexpectedly