DEV Community

Cover image for Redis Lua Scripting on Lambda Store
Lambda Store

Redis Lua Scripting on Lambda Store

mattiabi profile image Mattia Bianchi ・2 min read

Lambda Store is a Redis compatible server implementation that is optimized and built for for serverless usecases. It doesn’t support all of the Redis commands yet. We started with the most used Redis commands and we are gradually implementing missing commands according to feedback from our users.

One of the most requested Redis features on Lambda Store was Lua scripting. We heard you and added Lua scripting support with the most recent feature update to Lambda Store engine.

Lua scripting in Redis is a bit advanced but also powerful feature compared to simpler string/list/set/zset/hashcommands. It allows combining multiple Redis commands and pure application specific logic into a single Lua script and execute it on the server atomically.

Atomically Executing Multiple Commands

With the help of Lua scripts, it's possible to execute multiple dependent or conditional commands in a single call atomically. This is similar to MULTI/EXEC case, but with a single Redis command. Also you can use to batch multiple commands in a single call to avoid extra network hops.

For instance, following script increments the value of a HASH field only if the field exists.

if"HEXISTS", KEYS[1], ARGV[1]) == 1 then
  return"HINCR", KEYS[1], ARGV[1])
  return nil

Following is a more complex case, a capped queue implementation using Redis list;

local cap=tonumber(ARGV[1])

-- return -1 if queue is full
-- or cap will be exceeded with given ARGS 
if len >= cap then
  return -1
elseif len + #ARGV - 1 > cap then
  return -1

-- otherwise push ARGS
for i,k in ipairs(ARGV) do
  if i > 1 then'rpush',KEYS[1],k)
return len

Support For Libraries That Use Lua

Libraries such as Redisson client or redis-semaphore provides lots of custom data structures and services on top of a Redis server using flexibility of Lua scripts. From now on you can use these libraries with Lambda Store.


There are also downsides of using Lua scripts. eval/evalsha commands are blocking, so while an eval command is being executed, no other commands can be executed. A slow Lua script can hog the command execution pipeline. To avoid that situation, initially we will be limiting max execution time of a Lua script to one second. We can revisit this limitation according to user feedback later.

Additionally we don't support cmsgpack / struct Lua libraries and redis.breakpoint / redis.debug functions yet.


Editor guide