Learn

Getting started with Redis Functions

Elena Kolevska
Author
Elena Kolevska, Technical Enablement Manager, EMEA

The most impactful addition to Redis version 7.0 is Redis Functions - a new programmability option, improving on scripts by adding modularity, reusability, and better overall developer experience.

Functions are, in contrast to scripts, persisted in the .rdb and .aof files as well as automatically replicated to all the replicas, which makes them a first-class citizen of Redis.

Redis has the capability of supporting multiple execution engines so in one of the future releases we’ll be able to write Redis Functions in Lua, Javascript, and more languages, but at the moment (Redis v7.0) the only supported language is Lua.

A common pain point for developers is to maintain a consistent view of data entities through a logical schema. Redis Functions are ideally suited for solving this problem and in this tutorial, we will demonstrate just that - we’ll create a library with two functions; the first one will make sure that we can automatically set _created_at and _updated_at timestamps for hash keys and the second one will simply update the _updated_at timestamp without changing the other elements, simulating the “touch” Unix function. Let's go!

Environment setup#

First, let’s set up a working environment with Redis 7. You can follow the installation instructions in the guides below, according to your operating system:

Alternatively, you can spin up a Docker container with Redis Stack:

Code Example
$ docker run -p 6379:6379 --name redis-7.0 -it --rm redis/redis-stack:7.0.0-RC4
NOTE

In the rest of this tutorial we’ll use the $ character to indicate that the command needs to be run on the command prompt and redis-cli> to indicate the same for a redis-cli prompt.

Warm-Up#

Now that we have our Redis server running, we can create a file named mylib.lua and in it create a function named hset that would receive the keys and arguments we pass on the command line as parameters.

Functions in Redis are always a part of a library, and a single library can have multiple functions.

For starters, let's create a simple function that returns "Hello Redis 7.0" and save it in the mylib.lua file.

#!lua name=mylib

local function hset(keys, args)
   return "Hello Redis 7.0"
end

Maintaining a consistent view of data entities through a logical schema#

We're now ready to start working on the requirement. First, let's implement the part that adds an _updated_at timestamp:

#!lua name=mylib
local function hset(keys, args)
   local hash = keys[1]  -- Get the key name
   local time = redis.call('TIME')[1]  -- Get the current time from the Redis server

   -- Add the current timestamp to the arguments that the user passed to the function, stored in `args`
   table.insert(args, '_updated_at')
   table.insert(args, time)

   -- Run HSET with the updated argument list
   return redis.call('HSET', hash, unpack(args))
end

redis.register_function('my_hset', hset)