Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there are KEYS and ARGV arrays when calling Redis Lua scripts?

Tags:

redis

When calling Lua scripts from within Redis, it is possible to pass values in two arrays: KEYS and ARGV. What is the difference? How does Redis treat values in the KEYS array? What if, in my script, I have to use keys dynamically generated at run time?

like image 216
user606521 Avatar asked Aug 19 '15 07:08

user606521


People also ask

Why Lua script is used in Redis?

Redis lets users upload and execute Lua scripts on the server. Scripts can employ programmatic control structures and use most of the commands while executing to access the database. Because scripts execute in the server, reading and writing data from scripts is very efficient.

Are Lua scripts Atomic?

Lua scripts are executed atomically, that is, no other script or command will run while a script is running, which gives us the same transactional semantics as MULTI / EXEC .

Which Lua command causes script to terminate when Redis returns?

call() will raise a Lua error that in turn will force EVAL to return an error to the command caller, while redis. pcall will trap the error returning a Lua table representing the error." So according to the documentation , in the case of using redis.


1 Answers

All your questions are answered at the EVAL page, but since you asked...:

What is the difference?

KEYS is used to pass key names whereas ARGS should be for anything else. This isn't exactly enforced (i.e. most times you'd be ok mixing them) but could lead to potential problems if not followed.

How does Redis treat values in the KEYS array?

The contents of KEYS are checked to verify that all keys are available to the Redis shard that's running the script. This mechanism is in place to allow running scripts in a Redis cluster deployment.

What if, in my script, I have to use keys dynamically generated at run time?

See previous answers - that's doable but you'd going against the recommendations. Your script will be safe to run only on a stand alone Redis instance and since this behavior isn't specified, it may break in future releases.

like image 132
Itamar Haber Avatar answered Sep 23 '22 18:09

Itamar Haber