- Documentation
Cache for Redis
Best practices
Lua scripting guide
Best practices
Lua scripting guide
Lua scripting guide
Cache for Redis supports processing CAS (compare-and-swap) commands through Lua scripting, thus satisfying the atomic operation requirements of Redis and enhancing its performance. This topic introduces you to basic syntax, usage restrictions, and recommendations for using Lua scripts with Redis.
Lua script command
The following table lists the Lua script commands and features supported by Cache for Redis.
Lua script command syntax
- Execute the given script with the specified parameters and return the result.
EVAL script numkeys [key [key ...]] [arg [arg ...]]
- The specific parameters are described as follows.
tip
- When executing a Lua script using the EVAL command, the system simultaneously caches the script in the Redis instance. For more information on the EVAL command, refer to EVAL.
- It is recommended to explicitly pass the keys accessed by the script through the KEYS[] parameter instead of encoding them into the script. Excessive similar behavior can lead to an increase in instance memory usage and a reclaiming failure. In extreme cases, it may result in instance primary and secondary memory overflow (Out of Memory), causing data loss.
- Example 1: Execute the following Lua script using the EVAL command to set the string Key=department and value=Game. The specific command is as follows.
EVAl "return redis.call('SET',KEYS[1],ARGV[1])" 1 department Game
- An example of the response is as follows:
- Example 2: Execute the following Lua script using the EVAL command to obtain the string value of Key=department. The specific command is as follows.
EVAl "return redis.call('GET',KEYS[1])" 1 department
- An example of the response is as follows:
- Executing longer Lua scripts through the EVAL command entails considerable bandwidth consumption. To avoid this, you can use the EVALSHA command to execute such scripts by its value of sha1.
EVALSHA sha1 numkeys [key [key ...]] [arg [arg ...]]
- The specific parameters are described as follows.
tip
When using the EVALSHA command, Redis generates the NOSCRIPT No matching script. error if the script corresponding to the value of sha1 is not cached in Redis. Please cache the target script in Redis using the EVAL or SCRIPT LOAD command before executing the EVALSHA command. For more information on the EVALSHA command, refer to EVALSHA. EVALSHA 620cd258c2c9c88c9d10db67812ccf663d96bdc6 1 department
- Examples of back results are as follows.
- Load a Lua script into the Redis instance without immediately executing it and return the value of sha1 of the script.
- For more information on the SCRIPT LOAD command, refer to SCRIPT LOAD.
SCRIPT LOAD "return redis.call('GET',KEYS[1])" 1 department
"return redis.call('GET',KEYS[1])" 1 department
- Examples of back results are as follows.
"620cd258c2c9c88c9d10db67812ccf663d96bdc6"
- Check if the script corresponding to the specified value of sha1 is cached in the Redis instance.
SCRIPT EXISTS sha1 [sha1 ...]
tip
- Supports passing in the sha1 values of multiple scripts for confirmation simultaneously, separating the values of sha1 by spaces. The range of returned values is as follows:
- 1The script already exists.
- 0The script does not exist.
- For more information on the SCRIPT EXISTS command, refer to SCRIPT EXISTS.
SCRIPT EXISTS 620cd258c2c9c88c9d10db67812ccf663d96bdc6 620cd258c2c9c88c9d10db67812ccf663d96bdc7
- Examples of back results are as follows.
- Terminate the currently running Lua script. This command is primarily used to stop scripts that have been running for too long. For example, scripts stuck in an infinite loop due to an error.
tip
- To ensure data atomicity, the SCRIPT KILL command is executed only if the specified script has not performed any write operations.
- After executing the SCRIPT KILL command, errors generated from the script command will appear in clients that are executing Lua scripts. For more information on the SCRIPT KILL command, refer to SCRIPT KILL.
- Examples of back results are as follows.
- Clear all Lua script caches from the current Redis instance.
SCRIPT FLUSH [ASYNC | SYNC]
tip
- Redis supports specifying the cache refresh mode after executing the SCRIPT FLUSH command. Valid values:
- SYNC (Default): Synchronously refresh the cache.
- ASYNC: Asynchronously refresh the cache.
- This command clears all the Lua script caches in the current Redis instance. We recommend that you back up the Lua scripts before clearing them. For more information on the SCRIPT FLUSH command, refer to SCRIPT FLUSH.
- Examples of back results are as follows.
Limits for using Lua scripts
Suggestions for using Lua scripts
- We recommend explicitly passing the keys accessed by the script through the KEYS[] parameter instead of encoding them into the script. Excessive similar behavior can lead to an increase in instance memory usage and a reclaiming failure. In extreme cases, it may result in instance primary and secondary memory overflow (Out of Memory), causing data loss.
- We recommend that you add the redis.replicate_commands(); parameter in the Lua script to specify the data synchronization mode as an actual command-based mode to avoid inconsistencies between primary and secondary data.
- Loading Lua scripts entails consumption of considerable computing resources, so Lua scripts should be reused wherever feasible to reduce network overhead and the number of scripts that need to be cached.
- We recommend entering only simple commands in Lua scripts. Use transaction commands for complex operations involving atomicity.
- We recommend not entering too many loops in Lua scripts to prevent script execution timeouts or errors, which can even cause Redis instances to freeze.
- We recommend not using certain unofficial SDKs (such as Redisson) in Lua scripts to prevent unknown problems and even risks.
Last updated: 2025.11.03 18:43:45