I used to use PostgreSQL sequence SELECT nextval('number');
to make sure I get a new number even if there are several clients since nextval
is guaranteed to return distinct and increasing values.
I wanted to use the same mechanism with Redis.
I thought using the INCR
command but I want to make sure I well understand that it will give the same guarantee than the nextval
command?
There is no risk of getting the same number when several processes run this command or do I have to use a Redis lock?
Thanks
Redis is a single-threaded engine, so the execution of all commands is serialized. It always provides atomicity and isolation (in the sense of ACID) for each individual command.
The consequence is applying a single INCR command and exploiting its result is safe (even if multiple connections do it concurrently).
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