Introduction to ShedLock in Spring boot.
If you are working with Spring boot, then there is a good chance that you might have heard about ShedLock by now. If not, this article will provide the nitty-grritties of ShedLock.
What is ShedLock?
Simply put, ShedLock is a distributed task locking mechanism. That’s it. If that sounds too complex, here is a small example for you:
Imagine you are at a buffet and this buffet happens to have servers serving you the food items. To cater for large number of customers, the buffet has the same item being served multiple times. Let’s say you’re getting your favourite food items. Now if you are in the line and you just got served with a good portion of Spaghetti, and next item again is Spaghetti again, will the server serve you another portion of Spaghetti? The answer for this is obviously No.
Long story short, in a buffet, by default, a dish is only served once at a time.
This is the fundamental part of ShedLock. Just like the Spaghetti, in real-world applications, there will be some requests that should be executed at most once at a given time.
Why do you need ShedLock?
Let’s understand this with another small example.
In real world applications, there will be some processes which should be executed at most once at a time.
For example, Let’s say you are paying for something using a banking application. Due to human error, your payment request has been sent twice. Now in this case, would you expect your money to be debited twice? Obviously no. Similarly, in real world applications, there would be some processes/requests that must be executed at most once.
ShedLock acts as guardian and it makes sure that our configured methods or functions are executed at most once at a given time.
But when could you possibly have two requests to execute one process?
If your application is deployed to have multiple instances, the request could be taken up by multiple instances, then you will be facing the above scenario.
Scheduled jobs and cron jobs are most vulnerable for this issue.
How to implement ShedLock in Spring boot?
To implement ShedLock in Spring boot, you will need the following dependencies in your application.
For Maven:
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-spring</artifactId>
<version>5.14.0</version>
</dependency>For Gradle:
implementation group: ‘net.javacrumbs.shedlock’, name: ‘shedlock-spring’, version: ‘5.14.0’
Along with this your application needs to connect to a database, where a table called ShedLock is auto-created.
You would require these following configurations also.
@Configuration
public class SchedulerConfiguration {
@Bean
public LockProvider lockProvider(DataSource dataSource) {
return new JdbcTemplateLockProvider(dataSource);
}
}
@SpringBootApplication
@EnableScheduling
@EnableSchedulerLock(defaultLockAtMostFor = "PT30S")
public class ShedoLockExample{
public static void main(String[] args) {
SpringApplication.run(ShedoLockExample.class, args);
}
}
defaultLockAtMostFor should be configured according to your applications needs. It commands to hold the lock for at most for specific time in case the executing instance occurs a failure or crash.
@Component
class TaskScheduler {
@Scheduled(cron = "0 0/15 * * * ?")
@SchedulerLock(name = "example_task_scheduler",
lockAtLeastFor = "PT5M", lockAtMostFor = "PT14M")
public void scheduledTask() {
// ...
}
}
Here @SchedulerLock specifies that the method is eligible for ShedLock.
A unique name has to be given for the process to track down the locking at any particular time.
lockAtLeastFor and lockAtMostFor are to lock the process for minimum and maximum amount of time. This needs to be titrated based on the execution time of the process.