I have a containerized Node app, which runs on a DigitalOcean server. When I update the app on the server, the app has to go down for a small amount of time. In order to be able to update the app and avoid downtime, I am currently reading on zero-downtime deployment / blue green deployment with the intention of integrating Docker Swarm and Kubernetes as soon as I am more confident in my ability to use them.
But there is something that really confuses me when I imagine my app being replicated across several nodes. In my application, a User can define some Rules. So, for example, every day at 11AM, I want an email to be sent to Bob.
When my application starts, it fetches all CronTriggers from the database and builds CronTrigger objects that live in the app.
const { CronJob } = require('cron');
module.exports = class CronTrigger {
constructor(cronString) {
this.job = new CronJob(cronString, () => {
// Send email here
}, null, false, 'America/Toronto');
this.job.start();
}
}
But what happens if my app is replicated across several nodes? Does that mean that the CronTrigger will be executed as many times as there are nodes? If I have a 3-nodes cluster, how can I make sure that at 11AM, only one email will be sent to Bob and not 3?
How is this kind of problem dealt with with technologies such as Docker Swarm, Kubernetes or even AWS EC2?
Yes, you fix this by either moving the actual cron execution to a different daemon that you only run one copy of or use some kind of leader election system so that only one of the copies runs them at any given time.