Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Redis' set command an atomic operation?

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?

like image 525
Hunter Zhao Avatar asked Apr 06 '17 15:04

Hunter Zhao


People also ask

What is Redis set?

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

Is Redis cluster atomic?

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.

Is Redis Setnx Atomic?

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.

What are atomic operations in OS?

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.


1 Answers

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.

like image 112
Marc Gravell Avatar answered Oct 04 '22 21:10

Marc Gravell