3 ms·
Why is "how to do distributed locking" not "setnx in Redis with timeouts/retries"?
by MuffinFlavored 7y ago
Why is "how to do distributed locking" not "setnx in Redis with timeouts/retries"?
- tyingq 7y agoAuthor says this about setnx: "If you need locks only on a best-effort basis (as an efficiency optimization, not for correctness), I would recommend sticking with the straightforward single-node locking algorithm for Redis (conditional set-if-not-exists to obtain a lock, atomic delete-if-value-matches to release a lock), and documenting very clearly in your code that the locks are only approximate and may occasionally fail."
- MuffinFlavored 7y agoI guess I don't understand why setnx locks would fail. Race conditions?
- GauntletWizard 7y agoThe redis server reboots. Simple as that. Redis is very very rarely run in a transaction mode that guarantees consistency across reboots (and it's rare enough to doubt the code works even when it is set to the necessary mode). If you're using redis in this mode, it's significantly slower than a proper database, so don't do that either.