Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EVAL vs. WATCH/GET/MULTI/EXEC

Tags:

redis

I've read the documentation on EVAL which states "Redis guarantees that a script is executed in an atomic way: no other script or Redis command will be executed while a script is being executed."

I've also read about using WATCH/GET/MULTI/EXEC for what is essentially an optimistically locked transaction. I have a question now about the difference in concurrency between these two constructs.

It sounds to me like if I want to read a number of keys, do a lengthy computation (say, parsing a few hundred KB of json, and then making some decisions) and then write back the results, if I use EVAL I'll block ALL requests to the redis database, even those that interact with keys not part of the set I'll be interacting with.

On the other hand, if I use the WATCH approach, I will need to build retry logic, but if I am watching 100 keys out of a million I only have to worry about other clients interacting with those 100 keys, I won't actually block the database for the entire period, just during the EXEC call?

Please let me know if this understanding is correct, or offer me any clarification if I'm missing something here.

like image 429
William Thurston Avatar asked Nov 01 '25 02:11

William Thurston


1 Answers

Your understanding is flawless :)

One point you should consider, however, is that EVALing lets you save on network bandwidth and latency because everything is done server-side. I'm not suggesting that this alone should tip the scales in its favor, especially if you're doing heavy processing on the data as you question suggests, but you should keep that consideration in mind.

like image 166
Itamar Haber Avatar answered Nov 02 '25 23:11

Itamar Haber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!