Distributed locks with Nats? #713
Replies: 1 comment
-
Just realized this was more/less described in the docs here: https://docs.nats.io/nats-concepts/jetstream/key-value-store/kv_walkthrough#create-aka-exclusive-locking |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I was wondering if there's an established way of doing distributed locks with Nats.
Currently, I'm using kv and this seems to work, but I'm not too sure about timing/edge cases. Lock order/sequence isn't needed for my application and I presume to correctly do this in FIFO, we'd need to use a queue or something.
To acquire, I'm creating a key-value pair in a bucket, then deleting it on release. If there's an existing lock, it'll watch til the lock is deleted before trying again. The
_locked
attribute here is mostly to prevent double releasing/acquiring. On startup, I'm deleting & recreating the kv bucket.My class looks like:
Beta Was this translation helpful? Give feedback.
All reactions