Description
Overview
Introduce @DistributedLock
annotation that can be used together with @Scheduled
annotation to execute scheduled task only once in a given time window across all instances of the same Microservice.
Problem
Currently when using @Scheduled
annotation for Microservices that have more than one instance, Scheduled Task will be executed multiple times across different Microservices instances at the same time.
In some cases you want to execute Scheduled Task only once across all instances of the same Microservice.
Current Solutions
Solutions exists, however they are not as easy as using single @DistributedLock
annotation. Some examples include:
- Introduce custom annotation that will use RDBMS as a synchronization point to hold locks across multiple Microservices
- Use a Cloud Pattern - for example Create an HTTP function that is triggered by Cloud Scheduler
- Use Lambda
- Use Event Queue with Single Consumer
- Spring Integration Distributed Lock
- ...
Proposed Solution
Introduce easy to use annotation @DistributedLock
that would use some form of abstraction for distributed locks. By default it could use RDBMS.
Usage would look like below:
@DistributedLock(
lockId = "${payment.check.lock.id}",
timeout = "${payment.check.lock.timeout}",
minLockTime = "${payment.check.lock.minLockTime}"
)
@Scheduled(
fixedDelayString = "${payment.check.task.frequency}",
initialDelayString = "${payment.check.task.initalDelay}"
)
public void executePaymentCheckTask() {
...
}
Details
Min Lock Time
It is important to implement min lock time functionality to prevent execution of the scheduled task multiple times under cases when one instance starts with a slight delay.
Example:
- task should be executed every 10 seconds
- task execution time is 2 seconds
- Instance 2 starts with a 7 seconds delay compared to Instance 1
- Because of the delay in startup, task schedule looks like below:
- Instance 1: 09:00:00, 09:00:10, 09:00:20, 09:00:30, ...
- Instance 2: 09:00:07, 09:00:17, 09:00:27, 09:00:37, ...
Problem: Without min lock time set to 10 seconds, when Instance 1 starts the task at 09:00:00, and when task is finished after 2 seconds at 09:00:02, the Instance 2 would execute the task at 09:00:07.
Solution: Introduce min lock time to prevent the other instance with delayed startup time to execute scheduled task more often then specified by a schedule.