AP model based
Last updated
Was this helpful?
Last updated
Was this helpful?
Example: SET resourceName randomValue NX PX 30000
Succeed only if key does not exist; Otherwise fail the operation. Use the atomic property of NX to guarantee that only one client could configure it successfully.
Definition: Used for validation when releasing lock among different threads. Only release lock if the randomValue is the same. Typically set as UUID.
Automatic expiration time in case there are some exceptions happening (Thread crash)
Is actually an AP model. Applicable for scenarios that prioritize efficiency over correctness.
If failed to acquire the lock,
Retry every certain period (typically set to 99 percentile of lock usage duration)
Monitor deletes events of the key
The lock is about to expire but business logic needs more time, resume lock to rescue.
If failed to acquire lock, one option is to use interrupt
For scenarios below, it could be improved with optimistic lock.
Use consistent hashing to guarantee the same key is always routed to the same node. There will be no need for distributed lock.
There is an obvious race condition with this model:
Client A acquires the lock in the master.
The master crashes before the write to the key is transmitted to the slave.
The slave gets promoted to master.
Client B acquires the lock to the same resource A already holds a lock for. SAFETY VIOLATION!
Redlock does not have any facility to generate fencing tokens. And it is not straightforward to repurpose Redlock for generating fencing tokens.
Relying on expiration time to avoid deadlock is not reliable.
What if the lock owner dies? The lock will be held forever and we could be in a deadlock. To prevent this issue Redis will set an expiration time on the lock, so the lock will be auto-released. However, if the time expires before the task handled by the owner isn't yet finish, another microservice can acquire the lock, and both lock holders can now release the lock causing inconsistency.
Redlock depends on a lot of timing assumptions
All Redis nodes hold keys for approximately the right length of time before expiring
The network delay is small compared to the expiry duration
Process pauses are much shorter than the expiry duration
References: https://martin.kleppmann.com/2016/02/08/how-to-do-distributed-locking.html
Relationship with Redis could be thought as similar to Curator to Zookeeper
A fencing token needed to be used to avoid race conditions. Please see for details.
.