ShedLock with DynamoDB

Introduction
ShedLock is a distributed lock library designed to ensure that scheduled tasks in a distributed system are executed at most once at any given time. When multiple instances of an application run the same scheduled task, ShedLock prevents simultaneous execution by acquiring a lock in an external persistent store. If one instance acquires the lock, other instances attempting to run the same task will skip execution.
ShedLock with DynamoDB:
ShedLock can utilize Amazon DynamoDB as its external persistent store for managing locks. This involves configuring ShedLock to use a DynamoDB-based LockProvider. When a scheduled task is initiated by an application instance, ShedLock attempts to acquire a lock by creating or updating an item in a designated DynamoDB table. This item typically contains information such as:
name: A unique identifier for the scheduled task.lock_until: The timestamp until which the lock is held.locked_at: The timestamp when the lock was acquired.locked_by: An identifier for the instance that acquired the lock.
If an instance successfully writes or updates the lock item in DynamoDB, it proceeds with executing the scheduled task. Other instances attempting to acquire the same lock will find the item already present with a lock_until time in the future, causing them to skip the task execution. Upon completion of the task, the lock is released, allowing other instances to potentially acquire it in subsequent scheduling cycles. ShedLock also includes mechanisms to handle scenarios where an instance holding a lock crashes, ensuring the lock is eventually released based on the lock_until timestamp.
Conclusion
In this short article we have covered how you can use ShedLock with Amazon DynamoDB to solve scheduling tasks, a common problem found in distributed systems.




