Locks are used to provide mutually exclusive access to a resource. For example, you can use a lock to:
Any resource that is shared where updates occur can be the target for a lock. Locks are usually used on operations that mutate state, not on reads.
Each lock has a name. The application determines the resources that the named lock accesses. Typically, multiple instances of the same application use this named lock to exclusively access the resource and perform updates.
For example, in the competing consumer pattern, multiple instances of an application access a queue. You can decide that you want to lock the queue while the application is running its business logic.
In the diagram below, two instances of the same application, App1
, use the Redis lock component to take a lock on a shared resource.
*This API is currently in Alpha
state.
At any given moment, only one instance of an application can hold a named lock. Locks are scoped to a Dapr app-id.
Dapr distributed locks use a lease-based locking mechanism. If an application acquires a lock, encounters an exception, and cannot free the lock, the lock is automatically released after a period of time using a lease. This prevents resource deadlocks in the event of application failures.
Watch this video for an overview of the distributed lock API:
Follow these guides on: