Hi,
I am trying to create lock-protected Key/Value store for a system with many apps. The values in the store are a list. Different apps running on different machines must all be able to write to the list Values in append-mode. For example, say the state of the Key/Value store looks like this:
animals: dogs|cats
fruits: grapes|apples|pears
An app might need to append frogs to the list of animals. To do this, they must query for the current animals, then create a new value with dogs|cats|frogs and then write this new Value to the animals Key. Then the key/value store would look like this:
animals: dogs|cats|frogs
fruits: grapes|apples|pears
If their is no lock for the animals Key, another app may initiate a similar operation around the same time. Maybe they try to append spiders to the list. For a brief moment, the Key/Value store may have dogs|cats|spiders, but this will be overwritten with dogs|cats|frogs when the second app finishes writing to the store. I need to implement locks for each key to prevent situations like this.
I've been doing lots of reading on this topic...
Do they need to know the session ID ahead of time? If so, any suggestions on how to share it? Or any suggestions on how to achieve same requirements with a different approach?
Also, based on the "single key for coordination" section of
https://learn.hashicorp.com/consul/developer-configuration/semaphore, I was inspired to come up with another solution that doesn't use the Consul
acquire/release functionality. Whenever someone wants to append to the
animals list, first they must successfully
put to a key called
animals_lock with
cas=0. After they are done updating
animals, they then delete
animals_lock key. That way, if another app starts up and wants to read and then write to
animals, they first must try and
Put the
animals_lock key with
cas=0. They will not successfully be able to do this until that key is removed. When the
put succeeds, they now have ownership of the
animals Key.
Does this solution seem like it could work? One potential pitfall I see is if the current owner of animals_lock dies, then nobody else can get it. I figure this can be solved by associating animals_key with a session that that owner creates. Are there any other pitfalls to watch out for?
Thanks,
Andrew