Can RPUSH and LPUSH in Redis race?

1.9k views Asked by At

The docs at redis.io on RPUSH and LPUSH aren't clear on whether or not these operations are still O(1) and transational when pushing multiple items. For example, if two clients perform an RPUSH to the same list with multiple items, can their items become interspersed and thus out of order?

tadman's comment on his answer cleared this up the best for me: Can RPUSH and LPUSH in Redis race?

1

There are 1 answers

2
tadman On BEST ANSWER

Since Redis is single threaded, there is no such thing as them happening at the "same time", one will always arrive ahead of the other, though the timing is often beyond your control.

Now if you have two processes can be coordinated in some way you can have one defer to the other until that operation is completed. You could create some kind of pessimistic lock if you were concerned about this, though that's only one solution here.

So the short answer is, yes, they will arrive in random order.

That's why Redis implements the MULTI command to batch things in transactions. Those transactions will get applied atomically.