Introduction
In this blog, we will discuss Redis functions. But before going into that first, I would like you to know what Redis stack is and why it is popular.
Redis is defined as an open-source (BSD licensed), stored data structure in memory and used as a database, cache, message broker, and streaming the engine. Redis provides us with data structures like strings, lists, hashes, sets, bitmaps, streams, hyper logs etc. Functions like Replication, Lua Scripting, IRU eviction, Transaction and on-disk persistence of different levels are built-in in Redis. Redis is written in ANSI C language, and it works on most of the POSIX systems like Linux, *BSD, and Mac OS X.
Redis Functions refers to the API used for managing code that needs to be executed on the server. Redis Functions is an extension of EVAL in previous versions of Redis.
Extending Redis With Lua Scripting
Redis helps us provide the interface for programming that lets us execute our custom scripts on the server. In Redis 6.2 and its version below, we use Lua scripting with the EVAL command to program the server. After Redis 7, we use Redis Functions to manage and run our scripts.
Programmability in Redis is the ability to execute arbitrary logic defined by the user in the server. Such pieces of logic are known as scripts. In our case, scripts enable the data processing where it lives, a.k.a, data locality.
Running Scripts
Redis provides us with two means for running the scripts.
Firstly, and ever since the Redis 2.6.0, the EVAL command enables us to run server-side scripts. It provides us with a quick and straightforward way to have Redis run into our scripts ad-hoc.
Secondly, added in v7.0 and afterwards, Redis Functions are essentially scripts that are first-class database elements. Such functions decouple scripting from application logic and enable independent development, testing, and deployment of scripts. To use functions, they need to be loaded first, and then they are available for use by all connected clients.
Sandboxed script context
Sandbox helps us prevent accidental misuse and reduce potential threats from the server's environment. Redis places the engine that executes user scripts inside a sandbox. Scripts should operate solely on data stored in Redis and data provided as arguments for their execution.
Maximum Execution Time
There is a default timeout for scripts to run. Scripts are subjected to a maximum execution time (usually the default value set by five seconds). The default timeout is usually enormous since most scripts run in less than a millisecond. The timeout limit is placed to handle accidental infinite loops.