Distributed locks
Distributed locks are a highly beneficial feature for mitigating race conditions in distributed systems. With the ability of your consumers to scale and operate in parallel, there are scenarios where simultaneous processing of events with the same identifier occurs. For instance, processing multiple instances of Order ID 10452 in parallel can lead to conflicts and race conditions.
To address this challenge, the Distributed Locks provider can be utilized to ensure that only one process at a time consumes events related to the same entity with the same ID.
Steps to Implement Distributed Locks
- Enable the environment variable
GR_IS_DISTRIBUTED_LOCKS_ENABLED
to activate the distributed locks feature. - Specify one of the two switches in the Namespace Schema to activate the locks:
- $atomicEntity: This switch will lock based on
Entity Name and Entity ID
. - $atomicEvent: This switch will lock based on
Entity Name, Event Name, and Entity ID
.
- $atomicEntity: This switch will lock based on
- The typical use case involves locking based on
Entity Name and Entity ID
. - To activate the
$atomic*
switches across all events, they should be specified at the namespace level.
By implementing distributed locks, you can effectively synchronize access to resources, thereby preventing race conditions and ensuring data consistency in your distributed applications.