8

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

Michael
  • 8,357
  • 20
  • 58
  • 86

1 Answers1

12

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

Didier Spezia
  • 70,911
  • 12
  • 189
  • 154
  • Thanks. Based on this, I added a new question that is related to using INCR but for having a daily counter: http://stackoverflow.com/questions/26443804/redis-reset-counter-every-day – Michael Oct 18 '14 at 19:45