Databases 15 min read

Integrating Lua Scripts with Redis: Concepts, Benefits, Commands, and Installation Guide

This article provides a comprehensive overview of using Lua scripts with Redis, covering Lua basics, advantages of Lua scripting, detailed Redis‑Lua integration syntax, command usage, return‑type mappings, script management commands, and step‑by‑step instructions for installing Lua and executing example scripts.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Integrating Lua Scripts with Redis: Concepts, Benefits, Commands, and Installation Guide

Redis can execute Lua scripts atomically, allowing complex operations to be performed server‑side with reduced network overhead and improved performance.

Lua Introduction – Lua is a lightweight, embeddable scripting language written in C, designed for extensibility. It runs on virtually any platform, offers a small footprint (≈200 KB interpreter), and provides optional JIT compilation.

Benefits of Using Lua with Redis

Reduces network round‑trips by bundling multiple commands into a single script.

Ensures atomic execution, eliminating race conditions without explicit transactions.

Enables script reuse across clients; scripts are cached on the server.

Offers high speed; Lua’s interpreter is among the fastest script engines.

Portable across platforms that support an ANSI‑C compiler.

Small source size (≈20 k lines of C, ~182 KB compiled binary).

Redis‑Lua Integration Details

1. Calling a Lua script uses the EVAL command:

$ redis-cli -- eval /path/to/redis.lua KEYS[1] KEYS[2] , ARGV[1] ARGV[2] ...

KEYS holds the keys accessed by the script, ARGV holds additional arguments. Spaces around commas must be preserved.

2. Calling Redis commands inside a script is done via redis.call :

redis.call('set', 'foo', 'bar')
local value = redis.call('get', 'foo')  -- value will be "bar"

Redis returns five reply types; redis.call converts them to Lua types as follows:

Redis reply type

Lua type

Integer reply

number

String reply

string

Bulk string reply

string

Array reply

table (array)

Status reply

table with

ok

field

Error reply

table with

err

field

3. EVAL syntax :

EVAL script numkeys key [key …] arg [arg …]

Even if a script takes no arguments, numkeys must be set to 0.

4. EVALSHA allows executing a cached script by its SHA1 hash, reducing bandwidth for large scripts. If the script is not cached, Redis returns NOSCRIPT and the client typically falls back to EVAL .

5. Script management commands :

SCRIPT LOAD <script> – loads a script and returns its SHA1.

SCRIPT EXISTS <sha1> … – checks if a script is cached.

SCRIPT FLUSH – clears the script cache.

SCRIPT KILL – aborts a running script unless it has performed write operations.

6. lua‑time‑limit (default 5 s) prevents runaway scripts; exceeding the limit returns a BUSY error while still preserving atomicity.

Installing Lua

# yum install -y readline
# yum install -y readline-devel
# wget http://www.lua.org/ftp/lua-5.3.4.tar.gz -P /root/software/download/lua/
# cd /root/software/download/lua/
# tar zxvf lua-5.3.4.tar.gz
# cd lua-5.3.4
# make linux   # requires gcc, install with yum install gcc
# make install

After installation, verify with:

# lua
> print('lua')
lua

Example Lua scripts and execution

03.lua (updates a key if a condition is met):

local name = redis.call('get', KEYS[1])
local age  = redis.call('get', KEYS[2])
if name == 'LLL' then
  redis.call('set', KEYS[1], ARGV[1])
  redis.call('incr', KEYS[2])
end

Execute with:

# redis-cli -h 192.168.127.128 -p 6379 --eval /path/03.lua name age , patrickLiu

04.lua (returns a hash as a Lua table):

local b1 = redis.call('hgetall', KEYS[1])
return b1

Run after populating a hash:

# redis-cli -h 192.168.127.128 -p 6379 --eval /path/04.lua myhash

The command returns the hash fields and values as a Lua table, which Redis converts back to a multi‑bulk reply.

Overall, the guide demonstrates how to embed Lua scripts in Redis for atomic, efficient data manipulation, how to manage script lifecycle, and how to set up the Lua environment on a Linux system.

DatabaseRedisscriptinginstallationLuaeval
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.