spacemilkman
spacemilkman

Reputation: 973

Redis INCR concurrency

I am using Redis' INCR to generate an ID for objects. And then use ZADD to add the object using the ID as key.

Do I need to worry about if there are multiple connections executing this same block of code? Say after id:12 if two connections connect at the same time and both add object using id:13, then one of them would be lost.

Upvotes: 6

Views: 4070

Answers (2)

Eli
Eli

Reputation: 38929

As Jonatan Hedborg stated, Redis is single threaded so you never need to worry about two clients doing something at the same time. If, on the other hand, your worry is that you want to run the INCR and ZADD commands sequentially, and want to make sure no other commands are run in between them, you can use transactions, and be guaranteed your commands are run as a single unit with nothing in between.

Upvotes: 7

Jonatan Hedborg
Jonatan Hedborg

Reputation: 4432

Since redis is single threaded, this can never happen - only one client can make a change to the database at a time.

Upvotes: 12

Related Questions