Why i can't run lua script in redis after redi

2019-07-24 16:22发布

问题:

Im having issues running a Lua script, here is what i am doing:

  1. I load script to redis with LOAD SCRIPT and get SHA of my script.
  2. I test my script with SHA and it's OK.
  3. I do a SAVE(BGSAVE), SHUTDOWN and run the redis server again but i can't execute my script with SHA, which i could before.

Why is this ?

回答1:

Server-side Lua scripts are not saved or stored by Redis. They are not similar to the stored procedures you can find in RDBMS.

The clients are supposed to provide the text of the script at least for the first execution of the script (i.e. use EVAL for the first execution, and EVALSHA for the next calls). Alternatively, you can also use SCRIPT LOAD and SCRIPT EXISTS, it may be more convenient depending on the situation.

This is a bit more complex to handle for the application, but there are benefits: it makes the Redis server stateless regarding Lua scripting. The code of the application (including Lua scripts) is managed on application side. You do not need to apply something on the Redis server before loading a new version of the application (even if the Lua scripts have changed).

This property is very useful in the context of a distributed infrastructure to support on-the-fly application loads, or client-side sharding, or Redis cluster.



标签: lua redis