consulconsul-kvdistributed-lock

Consul: increment a counter in a KV atomically with multiple nodes


I'm using Consul for service discovery and the KV store, with more than 10 nodes. Let's say that each of these nodes has to increment a counter on the same KV, but has to do it atomically. The way I think this should work is the following (using 2 nodes to keep it simple):

Is this possible using Hashicorp Consul? I think it can be achieved using sessions, but I didn't find a lot of resources on it.


Solution

  • Consider using Consul's check-and-set operation over locks. Assuming all of the apps writing to this KV path are written to use CAS, it will provide protections you're looking for without the added complexity of managing locks.