Middle+
How to ensure that a Scheduled Job runs only on one instance out of 20 in a cluster?
sobes.tech AI
Answer from AI
To ensure that a Scheduled Job runs only on one instance out of 20 in a cluster, a distributed locking mechanism can be used. Main approaches:
-
Using a database with a lock flag:
- When starting the task, the instance attempts to set a flag in the database (for example, in a table with a single record).
- If the flag is free, the instance sets it and executes the task.
- After completion, it releases the flag.
- Other instances see that the task is already running and skip the start.
-
Using a distributed cache with lock support (Redis, ZooKeeper, etcd):
- For example, with Redis, you can use Redlock — a distributed lock algorithm.
- The instance attempts to acquire a lock with TTL.
- If successful, it executes the task.
- Upon completion, it releases the lock.
-
Using specialized libraries:
- Spring Boot with
@Scheduledand libraries supporting distributed tasks, such as Quartz with clustering.
- Spring Boot with
Example with Redis (pseudocode):
String lockKey = "scheduledJobLock";
boolean acquired = redisClient.setIfAbsent(lockKey, instanceId, ttlSeconds);
if (acquired) {
try {
// execute task
} finally {
redisClient.delete(lockKey);
}
}
Thus, only one instance can acquire the lock and execute the task, while others will skip the start.