NATS KV locking behavior

14 views
Skip to first unread message

Maksim Dmitrichenko

unread,
Apr 8, 2025, 1:45:45 PMApr 8
to nats
Hi!

I don't quite understand how KV create/update feature interacts with revisions. I need to create a distributed mutex using KV functionality. I suppose to do it creating a key that has some TTL and contains hostname of owning instance (instance A). As far as I understand I need to make instance A to update this key periodically to reset TTL. But IIRC update controls only the revision - it doesn't check for the expected value as it is done in traditional CAS.

What if the owning instance A, for example, halts for some period of time during which the key will expire and meanwhile backup instance (instance B) would get a notification of key deletion and would acquire the lock by recreating the key with the value of its hostname? Suppose that during this period the revision number will reach the same revision which was the last for instance A - then it would be possible for the woken up instance A to acquire the lock overwriting the key with its own hostname. Of course this chance is quite low but when we talking about locking there is usually a question of guaranty but not a possibility.

Or maybe I missed the point and NATS saves the last revision for the deleted keys and revision will continue after the key is recreated by instance B? Or maybe the staring revision is randomized upon each time the key is (re)created? 

--
Best regards,
   Max
Reply all
Reply to author
Forward
0 new messages