6

In the StackExchange.Redis docs for scripting, it says a LoadedLuaScript can be used to avoid retransmitting the Lua script to redis each time it is evaluated. It then gives an example where the script is loaded into the server. I assume this shouldn't be done everytime you call the script, so where should the scripts be loaded?

Scripts are not persitent if redis server is restarted, so how should this be handled? Should they be loaded in ConnectionRestored event of the ConnectionMultiplexer? Presumably you would need to store them in a ConcurrentDictionary?

3
  • Script loaded to the redis itself using SCRIPT LOAD command. This command returns sha1 hash, so this hash can be used with th EVALSHA command to call this script. In fact this script can be loaded by any one and your application can just use this hash. Commented Jun 22, 2020 at 8:42
  • 1
    Thanks, but I know this information. The question is about when and how to run the SCRIPT LOAD command. You could do it on app startup - but then what happens if redis is restarted? Commented Jun 23, 2020 at 6:57
  • 1
    From doc The script is guaranteed to stay in the script cache forever. Basic logic is just use EVALSHA and in case of NOSCRIPT error call SCRIPT LOAD and try again Commented Jun 27, 2020 at 6:28

1 Answer 1

5

StackExchange.Redis handles Lua script caching internally. Normally you don't have to care about loading Lua scripts manually to redis.

StackExchange.Redis automatically transmits the Lua script to redis on the first call to 'ScriptEvaluate'. For further calls of the same script only the hash is used:

var prepared = LuaScript.Prepare(script);
prepared.Evaluate(someDb); // loads the script to redis and calls it

var prepared2 = LuaScript.Prepare(script);
prepared2.Evaluate(someDb); // calls the script by it's hash

LoadedLuaScript might be useful for edge cases like loading without executing.

I also clarified the official doku.

Background information

The library tries to call the script by it's hash and if the script is missing it gets a NOSCRIPT error and then transmits the script.

See ScriptEvalMessage:GetMessages in https://github.com/StackExchange/StackExchange.Redis/blob/main/src/StackExchange.Redis/RedisDatabase.cs

The behavior is also discussed and explained in the official StackExchange.Redis github repo:

Sign up to request clarification or add additional context in comments.

3 Comments

Please provide an explanation of your answer so that the next user knows why this solution worked for you. Also, sum up your answer in case the link stops working in the future.
Thank you @Elydasian for your feedback. I extended the answer,
If LuaScript is a new instance will be it still evaluated as a loaded script to the server?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.