ShedLock
Application Development
Software Troubleshooting
Database Management
Java Applications

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.

Practice system design

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:

 
1   Attempting to acquire lock for task 'task-name'
2   Lock for task 'task-name' acquired
3   Executing scheduled task 'task-name'
4   Lock for task 'task-name' released

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 lock
  • lock_until: Time until which the lock is held
  • locked_at: When the lock was acquired
  • locked_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:

MethodDetailsUse-case
Log MonitoringCheck application logs for lock status entries.Development, debugging, and low-scale production monitoring.
Database QueriesDirectly query the lock storage.Deep debugging, audit, and compliance.
Custom CallbacksImplement 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.