Redis LUA Scripting

Redis is a free, open-source in-memory database widely adopted as a caching mechanism or message broker.

Although it is very versatile and extensive by default, it does allow you to extend functionality using its embedded Lua interpreter. This means that you can write scripts written in Lua to perform additional operations on your Redis database.


To better follow along with this tutorial, we recommended having the latest version of the Redis server on your system.

Basic Redis knowledge is beneficial to understand the commands illustrated in this tutorial.

Redis EVAL Command

To run Lua commands in Redis, you need to use the EVAL command. The command tells Redis to execute the code as a server-side Lua script.

An example is as shown:> EVAL "'SET', KEYS[1], ARGV[1])" 1 key value

In the example above, we start with the EVAL command followed by a Lua script. The script uses the function. We then pass the command to run, key, and value as the arguments. This is similar to running SET key values.

Note that KEYS and ARGV denote the arguments of the script. We specify the number of keys as 1 in this case.

Argument parsing is done via the ARGV, which holds the argument table. In our case, we pass the value of the key.

We can then access the keys starting from index 1.

NOTE: It is recommended to specify all the keys used in the script under KEYS and all other arguments under ARGV.

We can fetch the value of a key using the get command:> EVAL "'GET', KEYS[1])" 1 key

This should return the value of the specified key.

Redis EVALSHA command

Redis also provides you with the EVALSHA command to recall a command based on its hash value.

This removes the need to retype the script every time you need it.


SCRIPT LOAD "return'get', KEYS[1])"

The command above should return a SHA value as:


You can then use this sha value to recall a command.> EVALSHA 4e6d8fc8bb01276962cce5371fa795a7763657ae 1 key

To remove all loaded scripts, run the command:> SCRIPT FLUSH

The command above should flush the script cache.


This article describes the most basic commands to use when working with LUA scripting in Redis. Check the Lua documentation to discover more.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list