Ben Ben - 4 months ago 34
Node.js Question

Node.js - Scaling with Redis atomic updates

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 Ben

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:

GET key
SET key

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