Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis INCR and multi processes?

Tags:

redis

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

like image 668
Michael Avatar asked Oct 18 '14 15:10

Michael


1 Answers

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

like image 78
Didier Spezia Avatar answered Sep 20 '22 00:09

Didier Spezia