1

I have a Node.js app that preforms the following:

  • get data from Redis
  • preform calculation on data
  • write new result back to Redis

This process may take place several times per second. The issue I now face is that I wish to run multiple instances of this process, and I am obviously seeing out of date date being updated due to each node updating after another has got the last value.

How would I make the above process atomic?

I cannot add the operation to a transaction within Redis as I need to get the data (which would force a commit) before I can process and update.

Can anyone advise?

Ben
  • 6,026
  • 11
  • 51
  • 72
  • Perhaps this SO thread is relevant to your situation: http://stackoverflow.com/questions/15776955/redis-watch-multi-exec-by-one-client If not, please rephrase the question in a clearer manner. – Itamar Haber Jan 04 '15 at 23:22

2 Answers2

4

Apologies for the lack of clarity with the question.

After further reading, indeed I can use transactions however the area I was struggling to understand was that I need separate out the read from the update, and just wrap the update in the transaction along with using WATCH on the read. This causes the update transaction to fail if another update has taken place.

So the workflow is:

WATCH key
GET key
MULTI
SET key
EXEC

Hopefully this is useful for anyone else looking to an atomic get and update.

Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
Ben
  • 6,026
  • 11
  • 51
  • 72
0

Redis supports atomic transactions http://redis.io/topics/transactions

jtmarmon
  • 5,727
  • 7
  • 28
  • 45
  • Thanks for the reply, but unless im missing something, I hit the problem I described in the question i.e. when i do the get, I am forced to commit to actually get the result. This leaves me in the same situation i.e. another process can pick the data. – Ben Jan 04 '15 at 22:09
  • Ah perhaps I misread. Can you provide a more concrete example of what you're trying to do? It's hard to visualize where this would leave you in a rough spot – jtmarmon Jan 04 '15 at 22:11