Skip to main content

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

  1. Enable the environment variable GR_IS_DISTRIBUTED_LOCKS_ENABLED to activate the distributed locks feature.
  2. Specify one of the two switches in the Namespace Schema to activate the locks:
    1. $atomicEntity: This switch will lock based on Entity Name and Entity ID.
    2. $atomicEvent: This switch will lock based on Entity Name, Event Name, and Entity ID.
  3. The typical use case involves locking based on Entity Name and Entity ID.
  4. 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.