I'm trying to use Redis' set
command to implement a simplest distributed lock component, but I can't find any exact basis about atomicity through the official document, is Redis' SET key value [EX seconds] [PX milliseconds] [NX|XX]
command an atomic operation?
Redis sets are unordered collections of unique strings that act like the sets from your favorite programming language (for example, Java HashSets, Python sets, and so on). With a Redis set, you can add, remove, and test for existence O(1) time (in other words, regardless of the number of set elements).
1 Answer. Show activity on this post. For those commands supported by cluster mode, they are atomic. However, some commands are limited supported by cluster mode, e.g. commands that take multiple keys.
Yes, SETNX is atomic and will do what you ask regardless of how many callers there are. Individual Redis commands are essentially always atomic, since Redis is single-threaded. So the documentation doesn't bother to specify that for every single command.
Atomic operations are sequences of instructions that guarantee atomic accesses and updates of shared single word variables. This means that atomic operations cannot protect accesses to complex data structures in the way that locks can, but they provide a very efficient way of serializing access to a single word.
Yes. The core is single threaded, so nothing will run until the SET
has completed; that makes SET {key} {value} EX {expiry} NX
ideal for simple locking.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With