Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Synchronize object between multiple instance of Node Js application

Is there any to lock any object in Node JS application.

Is there are multiple instance for application is available some function shouldnt run concurrent. If instance A function is completed, it should unlock that object/key or some identifier and B instance of application should check if its unlock it should run some function.

Any Object or Key can be used for identifying the locking and unlocking the function.

How to do that in NodeJS application which have multiple instances.

like image 263
Shan Khan Avatar asked Mar 11 '17 13:03

Shan Khan


1 Answers

As mentioned above Redis may be your answer, however, it really depends on the resources available to you. There are some other possibilities less complicated and certainly less powerful which may also do the trick.

  1. node-cache may also do the trick, if you set it up correctly. It is not any where near as powerful as Redis, but on the bright side it does not require as much setup and interaction with your environment.

So there is Redis and node-cache for memory locks. I should mention there are quite a few NPM packages which do the cache. Depends on what you need, and how intricate your cache needs to be.

However, there are less elegant ways to do what you want, though less elegant is not necessarily worse.

  1. You could use a JSON file based system and hold locks on the files for a TTL. lockfile or proper-lockfile will accomplish the task. You can read the information from the files when needed, delete when required, give them a TTL. Basically a cache system to disk.

The memory system is obviously faster. The file system requires just as much planning in your code as the memory system.

  1. There is yet another way. This is possibly the most dangerous one, and you would have to think long and hard on the consequences in terms of security and need.

Node.js has its own process.env. As most know this holds the system global variables available to all by simply writing process.env.foo where foo would have been declared as a global system variable. A package such as .dotenv allows you to add to your system variables by way of a .env text file. Thus if you put in that file sam=mongoDB, then in your code where you write process.env.sam it will be interpreted as mongoDB. Tons of system wide variables can be set up here.

So what good does that do, you may ask? Well these are system wide variables, and they can be changed in mid-flight. So if you need to lock the variables and then change them it is a simple manner to do it with. Beware though of the gotcha here. Once the system goes down, or all processes stop, and is started again, your environment variables will return to the default in the .env file.

Additionally, unless you are running a system which is somewhat safe on AWS or Azure etc. I would not feel secure in having my .env file open to the world. There is a way around this one too. You can use a hash to encrypt all variables and put the hash in the file. When you call it, decrypt before actually requesting use of the full variable.

  1. There are probably many wore ways to lock and unlock, not the least of which is to use the native Node.js structure. Combine File System events together with Crypto. But this demands a much deeper level of understanding of the actual Node.js library and structures.

Hope some of this helped.

like image 182
twg Avatar answered Nov 17 '22 09:11

twg