Backend Discussion Javascript

Trade-off running atomic daemons in Node.js

When you develop daemons that runs periodically in Node.js, you’d have some tricky issues.
I have used to node-schedule npm package and I had several problems.

  • As it runs by certain time interval, it could overlap each other as well as it will run another process without completing all jobs. For example, you have a daemon that fetches user balances in every 1 min. The daemon runs in every 1 min whether or not it processed all accounts. it’s serious.
  • The node-schedule has a problem with memory allocation and CPU usage.

The trade-off here is to use setTimeout and “recursive” function call.

export const syncUserBalance = async () => {
   // process user balances
   setTimeout(syncUserBalance, 60000);
};

This function calls another process after all work is done so there isn’t any overlapping. Furthermore setTimeout flushes memory as it sleeps 60000ms.
I have used this method in many projects and it worked nicely.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *