v0.2.0 now supports multiple locks
The new LockDB release supports a feature request common for complex locking necessities: The ability to lock, unlock, and check multiple locks.
tl;dr; You can try out LockDB for free for 30 days and see if it can help you out!
LockDB is a powerful cross-platform tool for handling process/event locking and avoiding race conditions, and v0.2.0 was just released. This new release introduces a highly anticipated feature: support for multiple locks. With this update, developers can now easily lock, unlock, and check multiple locks simultaneously, making it even more versatile for complex locking necessities, especially in distributed systems.
Before diving into the new feature, let's briefly recap what LockDB is and why it is essential in computer science. A lock, also known as a mutex (mutual exclusion), is a synchronization primitive used to enforce limits on access to a shared resource in the presence of multiple threads or processes. It ensures that only one thread or process can access the resource at a time, preventing race conditions and maintaining data integrity.
LockDB provides a simple and efficient solution for implementing locks in various programming environments, including Node.js, browsers, and CLI. It is designed to be lightweight, dependency-free, and easy to use, making it an ideal choice for managing concurrency in applications.
The Need for Multiple Locks
In complex applications, there are often scenarios where multiple resources need to be locked simultaneously. For example, consider a system that generates multiple reports for multiple customers. It may require locking both the reports module and the specific customer to ensure data consistency and prevent conflicts, or simply to reduce the use of too many resources while generating other reports.
Previously, LockDB only supported locking, unlocking, and checking a single lock at a time. While this was sufficient for most use cases, it posed limitations for applications with more intricate locking requirements. The v0.2.0 release addresses this limitation by introducing support for multiple locks.
How to Use Multiple Locks
check() methods with an array of lock names. For example, to lock both the "sales" report and "user_id" customer, you can use the following code (for a full example, check out the docs):
await locker.lock(['sales', 'user_id']);
This syntax ensures that both locks are acquired before proceeding with the critical section of code. It provides a straightforward and intuitive way to handle complex locking scenarios.
For those using the LockDB CLI, the process of locking multiple resources is equally straightforward. Simply provide a comma-separated list of lock names when using the
lock command. For example (for a full example, check out the docs):
lockdb lock sales,user_id
This command will lock both the "sales" report and "user_id" customer, allowing you to manage concurrency effectively from the command line or any application calling a system process.
One of the key advantages of the v0.2.0 release is that it maintains full backwards compatibility. This means that existing code that uses LockDB's single lock functionality will continue to work seamlessly. The introduction of multiple locks does not disrupt or require modifications to existing implementations.
You can get started with a 30-day free trial right now!