Search code examples
node.jsrediscron

Cron cluster with one Redis instance and multiple server roles


I am trying to create a clustered Cronjob using cron-cluster and redis on Node.js. We are running multiple servers with different roles (production, staging, test) and the same codebase. They are all connected to the same Redis instance.

How do I make cron-cluster run only once on each role? Currently, it picks only one server across the whole fleet (production, staging, test) and runs everything there.


Solution

  • I had the same issue with cron-cluster library. To solve it, you should pass a unique key, while initializing an instance. For example:

    const ClusterCronJob = require('cron-cluster')(redisClient, { key: leaderKey }).CronJob;
    

    Where { key: leaderKey } can be taken as follow:

    const leaderKey = process.env.NODE_ENV;