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?

1

2 Answers 2

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.

0

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

2
  • 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 4, 2015 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 4, 2015 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.