cammil
cammil

Reputation: 9939

How to update dependent key values in redis?

I would like to know if there is a recommended mechanism for updating one or more keys, based on the values of others, should those values change.

This is kind of like properties, or getters and setters, but for redis keys.

Why do I need this?

Suppose I have perhaps multiple (10-100 in my case) clients that all need to know a value dep1 which is a (pure) function of indep1, ... , indepN. One way to do this is have all the clients watch for changes in indep1, ... , indepN, and calculate the value of dep1. This seems to me to duplicate work, and increase network traffic. However, if dep1 is "automatically" updated, they all only need to listen for changes to dep1.

Current Thinking

The only solution I can think of is to have one monitoring client, that does this work. However, I am wondering if there is a better way. Perhaps with Lua. I am a complete Redis and Lua noob by the way.

Upvotes: 1

Views: 461

Answers (1)

Adam
Adam

Reputation: 2570

Using Lua seems like a decent idea. You could use Lua scripts for updating any of indep1 ... indepN and the scripts can automatically calculate dep1 and set its value in the database, in a single transaction (a Lua script runs to completion before any other commands are processed). Reading dep1 is then guaranteed to be correct as it was updated atomically alongside any of indepX. You would then only need to watch for keyspace changes on dep1 in your client.

Note: this may not be the most efficient way to do this, as the calculations for determining dep1 may be expensive and duplicated any time one of indepX is updated.

Upvotes: 1

Related Questions