Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Why is "how to do distributed locking" not "setnx in Redis with timeouts/retries"?


Author 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."


I guess I don't understand why setnx locks would fail. Race conditions?


The 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.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: