2

I have a list and I'm adding elements using RPUSH which returns the updated length of the list. My question is that, is it reliable to use the returned length - 1 as the index of the newly inserted item?

By reliable I mean, If I have multiple connections to Redis doing the same operation is it guaranteed there won't be any overlapping like the returned length actually returning after two RPUSH that occurred simultaneously or is that Redis' list operation atomic by default or do I need transactions?

Marconi
  • 3,601
  • 4
  • 46
  • 72

1 Answers1

2

Considering the atomicity of redis commands, you can be 100% sure that every RPUSH will be followed by the corresponding integer reply, and thus it is safe to assume that the index of your element is (integer_reply - 1). I have answered a somehow related question here.

Community
  • 1
  • 1
hymloth
  • 6,869
  • 5
  • 36
  • 47
  • Hey thanks, but does that mean I don't need a transaction for that? – Marconi Jun 12 '12 at 07:05
  • Ah yes, I just wrote a test in python and in fact integer_reply - 1 is reliable but only if you have one connection. Whenever my test starts spawning multiple parallel connection, redis starts processing whoever on those connection gets to push first but if I add transaction then everything works as expected. Here's the link to my test code https://gist.github.com/2915849 – Marconi Jun 12 '12 at 07:15