CodeSamplez.com

Programming, Web development, Cloud Technologies

  • Facebook
  • Google+
  • RSS
  • Twitter
  • Home
  • Featured
    • C# Tutorials
      • LinQ Tutorials
      • Facebook C# API Tutorials
    • PHP Tutorials
      • CodeIgniter Tutorials
    • Amazon AWS Tutorials
  • Categories
    • Programming
    • Development
    • Database
    • Web Server
    • Source Control
    • Management
    • Project
  • About
  • Write
  • Contact
Home Project Create Multi-Process NodeJS Server With ‘Clustered-Node’

Create Multi-Process NodeJS Server With ‘Clustered-Node’

Rana Ahsan November 9, 2014 Leave a Comment


 Create Multi-Process NodeJS Server With ‘Clustered-Node’    

If you are a NodeJS application developer, by default you are developing it in single threaded/single process based. If you are deploying it on a multi-cpu/core based server, then you are only under-utilizing it. Sure, we can do better, right? Nodejs also provide some facility to improve its default single-thread nature by its cluster module. Sure you will have to know first how to code in that way. But, wouldn’t it be nice if you just tell it to run your application in multi-cluster way and it just work in that way, without coding bunch of lines manually?

The ‘clustered-node’ Library:

I am happy to introduce you to a new nodejs open source library that I have created recently, which will facilitate the exactly that, creating your nodejs server/application in multi-process/clustered mode in a super easy way, without knowing much inner details of how clustering works. The library is available as npm package and the source is available on github:

NPM Package: https://www.npmjs.org/package/clustered-node
Source On Github: https://github.com/ranacseruet/clustered-node

Background:

I was working on a small proxy server at my job which was needed to be multi-process based. I also found an already existing npm module named “multi-node“, but turned out it wasn’t maintained anymore and not compatible with latest nodejs versions. Didn’t able to find out anything else closely similar as its alternatives.

Thus, with help of the source code of hipache, we implemented our own multi-process mechanism. Later on, the thought came to my mind the have it as a separate module as multi-node and thus created its npm package and open sourced it on github.

Why using this library:

From the reputation from the earlier mentioned multi-node library, its obvious that, developers liked the idea of having the multi-process handling logic separated and use it as a ready solution, instead of implementing it individually for all applications. So, as an alternative of that library, this can help you, I hope.

This library provides very easy to use interface with two simple function that requires almost no additional effort to migration an existing application to multi-process mode.

Additionally, to have your application in multi-clustered mode, you will have to design and develop your application as stateless, which is very beneficial when it comes to the question of scaling. Even if you deploy your application on different geographical location, it will work seamlessly.

Installing:

It is very easy to install the package as a simple command below:

$npm install clustered-node --save

The above command

How To Use:

Currently this library offers two different use cases:

  1. Multi-Process Server: If your application is a nodejs server you can easily pass the server object with proper host/port where it should listen to and number of processes to this module’s ‘listen’ method. So, you don’t have to use server object’s internal ‘listen’ method. Example:
    var server = http.createServer(someCallbackMethod);
    require("clustered-node").listen({port:1337, workers:3}, server);
    
  2. Multi-Process App: if your application is a non-server app, like a crawler, test-tool etc, then you can just pass your entry point method as the parameter, along with the number of processes to this module’s ‘run’ method. Which will be in turn called for every process created. Example:
    function runMyApp() {
     //start your application coding starts here
    }
    require("clustered-node").run(config, runServer);
    

Things To Consider Before Migrate:

Though as shown above, migrating your existing app to multi-process based is very easy and efficient, there is something you should remember and careful about your application before you actually do the migration to make sure that the migration goes seamlessly without any issue:

  • Your Application Is Stateless: You will have to make sure that your application is stateless, which means it doesn’t store session data itself. If it is, migration the mechanism to use db/cache server to do that. Also, don’t use global variable(constants are OK) inside the entry-point method if you are using the ‘run’ method.
  • Number Of Process Is Reasonable: You shouldn’t just put any number as the number of processes to be run. You should keep it synced with the number of CPUs/Cores your server is running on and use that number. For example, if you are running two process on a single cpu core, they are at the end sharing same CPU schedule and won’t actually scale up the overall performance, instead could be worse(using OS scheduling for context switching)

Possible Improvements/Enhancements In Future:

This project is on very early stage and I already several things in mind for improvement on this project:

  • Unit Tests: I am planning to add unit tests to test each unit of functionality automated way with use of mocha and chai.
  • Support other use cases: I am also planning to support other use cases like, you will be able to migrate your existing app without even touching it, having the multi-process implementation on a separate file and pass the app main js file as parameter. This will give developers the peace of mind and flexibility to switch back and forth from/to single/multi-thread mode instantly anytime.

Your contribution/Feedback:

You are more than welcome to contribute provide feedback on this project. Just use the github issue tracker for clustered-node for suggestions/bug reports.

Pull requests are also very much welcome as well. This is just an early stage implementation and I believe can be enhanced/improved in lot of ways other than what I mentioned above. If you are interested to work on any such parts, feel free to start discussing on github or contact me.

Let me know if I have missed anything or you like to know about. Happy coding!

Related

Filed Under: Project Tagged With: javascript, nodejs, npm, scalability, server

About Rana Ahsan

Rana is a passionate software engineer/Technology Enthusiast.
Github: ranacseruet

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Email Subscription

Never miss any programming tutorial again.

Popular Tutorials

  • How To Work With JSON In Node.js / JavaScript
  • PHP HTML5 Video Streaming Tutorial
  • Generate HTTP Requests using c#
  • How To Work With C# Serial Port Communication
  • Facebook C# API Tutorials
  • How To Work With CodeIgniter Pagination
  • LinQ To SQL Database Update Operations In C#
  • Get Facebook C# Api Access Token
  • Utilizing Config File In C#.NET Application
  • How To Work With Multithreaded Programming In C#.NET Application

Recent Tutorials

  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read
  • Getting Started With Golang Unit Testing
  • Getting Started With Big Data Analytics Pipeline
  • NodeJS Tips And Tricks For Beginners
  • Apple Push Notification Backend In NodeJS
  • Web Based Universal Language Translator, Voice/Text Messaging App
  • How To Dockerize A Multi-Container App From Scratch

Recent Comments

  • intolap on PHP HTML5 Video Streaming Tutorial
  • manishpanchal on PHP HTML5 Video Streaming Tutorial
  • Rana Ghosh on PHP HTML5 Video Streaming Tutorial
  • ld13 on Pipe Email To PHP And Parse Content
  • Daniel on PHP HTML5 Video Streaming Tutorial

Archives

Resources

  • CodeSamplez.com Demo

Tags

.net apache api audio aws c# cache cloud server codeigniter deployment doctrine facebook git github golang htaccess html5 http image java javascript linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty ssh tfs thread tips ubuntu unit-test utility web application wordpress wpf

Copyright © 2010 - 2021 · CodeSamplez.com ·

Copyright © 2021 · Streamline Pro Theme on Genesis Framework · WordPress · Log in