How to understand when shedlock was acquired and released?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
ShedLock is a library designed to prevent the execution of concurrent scheduled task executions across multiple nodes. This feature is crucial when running scheduled tasks in a distributed environment where multiple instances of an application might attempt to execute the same task simultaneously.
Understanding ShedLock Mechanisms
ShedLock works by using a storage (like SQL databases, MongoDB, Redis, etc.) as a lock provider. When a scheduled task is about to run, ShedLock attempts to create a lock record in the designated storage. If the record can be created (or updated), it means the lock was successfully acquired, and the task can execute. If not, it means another instance has already acquired the lock.
How to Check Lock Acquisition and Release
To effectively manage and debug applications using ShedLock, understanding when and how locks are acquired or released is crucial. Here are practical ways to determine this:
1. Log Monitoring
Most straightforward implementations of ShedLock include logging the acquisition and release of locks. By configuring the logging level of your application correctly, you can capture entries that indicate when locks are acquired and released. Here's an example of what log entries might look like:
Ensure that your logger is configured to capture info level logs from the package or class that integrates ShedLock.
2. Database Queries
Since ShedLock uses a storage mechanism to maintain lock status, you can directly query this storage to see the status of a lock. For example, if using a SQL database, you might find a table named shedlock where each row represents a lock. The columns typically include:
name: Name of the locklock_until: Time until which the lock is heldlocked_at: When the lock was acquiredlocked_by: Identifier of the instance that holds the lock
You can query this table to understand the current state of locks.
3. Adding Custom Callbacks
If you need more sophisticated handling or notifications around lock acquisition and release, consider implementing custom callbacks in your scheduling configuration. Here, you can add the necessary logging or even integrate with monitoring systems to alert when lock states change.
Monitoring and Alerts
Setting up monitoring and alerts for lock acquisition and release can help in proactively managing issues in a distributed system. You can integrate with tools like Prometheus, Grafana, or even simple email notifications depending on the criticality of the tasks involved.
Summary Table
Here's a quick summary of common ways to understand lock status in ShedLock:
| Method | Details | Use-case |
| Log Monitoring | Check application logs for lock status entries. | Development, debugging, and low-scale production monitoring. |
| Database Queries | Directly query the lock storage. | Deep debugging, audit, and compliance. |
| Custom Callbacks | Implement callbacks in scheduling to handle lock events. | Advanced monitoring and custom integrations. |
Conclusion
Understanding when ShedLock acquires and releases locks is crucial for maintaining the integrity and reliability of scheduled tasks in distributed systems. By effectively using logs, direct database queries, and custom callback mechanisms, developers and system administrators can gain visibility into the behavior of their scheduled tasks and troubleshoot issues more effectively. Integration with monitoring tools and setting up proper alerts can further enhance the operational aspects of using ShedLock in production environments.
Related reading
- How to unload a table on RedShift to a single CSV file?
- How to update a record using sequelize for node?
- How to update an item in Dynamodb of type String Set SS?
- How to update an item in Dynamodb of type String Set SS?
- How to unit test a Spring Boot MongoRepository?
- How to update a value, given a key in a hashmap?
- how to undo a kubectl port-forward
- how to uninstall minikube from ubuntu, i get an ''Unable to load cached images'' error

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.