Luainsider - manage your Redis Lua scripts like a pro
A methodology, approach and apparatus for semi-persisting and aliasing Redis Lua scripts.
FAIR WARNING: this is WIP (i.e. could blow in your face)
Being able to run Lua scripts is a helluava Redis feature. Managing these scripts from code can become tricky, and while there are several open source projects that tackle this, each of them is programming-language- and client-specific.
This proposal describes an approach for implementing generic script management in Redis. Since it uses only the standard Redis API it can be easily added to/with any client library/application.
Why doesn't Redis have script aliases already?
Antirez gives a very good reason for that - scripts are part of the application, not the database. Consider all that is wrong with the RDBMS stored procedure and you'll come to the same conclusion.
Redis Lua scripts generic usage
- Scripts are stored in text files alongside the application code, or as embedded strings in it.
- Once-off execution with
EVALis usually used only in development.
- Caching scripts, first by using Redis'
SCRIPT LOADcommand ,and then executing them with
EVALSHAis the recommended mode of operation.
The aches with cached scripts
Cached scripts work extremely well, but working in this manner tightly couples the script's source with the application because:
- A script may be absent from the cache. This happens if the script wasn't loaded in the first place, after the database is restarted or as the outcome of a successful call to
SCRIPT FLUSH. To handle such cases, every caller to the cached script must also be able to perform the loading operation if needed.
- Even if a script is already cached, the caller must have its sha1 to execute it. The script's sha1 can be obtained either by loading the script to Redis (recommended) or by computing the sha1sum independently. This can be repeated before every invocation (wasteful), or by the caller storing the sha1 once and reusing it.
The persistence of Redis' cached scripts is, therefore, left for the clients to handle. The major ache with this approach the explicit binding of script resources with application code. When used this way, every change to a script requires redeployment of scripting resources and, if they aren't packaged with the application's code, notifying the application instances about the update.
Another ache, albeit less major, is sha1's unfriendliness to humans - we're more comfortable calling our scripts with meaningful identifiers, whereas seemingly-random strings tend to confuse us. For example, consider the following script:
127.0.0.1:6379> SCRIPT LOAD "return tonumber(ARGV) == 42" "1041fb7065092daf01ed47f50c3457ed4fd322ac"
That script's sha1 is
1041fb7065092daf01ed47f50c3457ed4fd322ac, but that identifier doesn't tell us anything about the script that it represents. Moreover, every change to the script - whether of code, comments or formatting - will result in a different sha1 although the script's purpose will most likely remain the same. Such changes make hardcoding the sha1 in the application's code an ill-advised practice.
The gist of it
The ickiest aspects of managing cached scripts can be addressed with a per-database central script repository. Luckily, we have Redis to act as such and we can use it to provide several interesting capabilities:
- "Light" clients - by storing the script's source once in Redis, we can ensure that it is available to any client that connects to the database, regardless whether the client has local copy of the script.
- Semi-persistency - cached scripts can be loaded from the repository after being flushed and, if the database itself is persistent, after a restart.
- Centralized management - by having all clients refer to Redis as the scripts' repository, the task of managing these scripts is decoupled from that of the application's core code and assets.
- Aliasing - because clients use the repository to access cached scripts, it is trivial to implement a dereferencing mechanism.
- Redis with support for Lua scripts (v2.6 and above)
- For script persistence, Redis' data persistence must be enabled
- Nice to have:
noevictionor one of the
volatile-policies (otherwise aliases may be evicted)
The following describes how scripts can be loaded, cached and executed. As an abstract description, future work will be required to add this behavior to existing clients and applications.
Loading aliased scripts
Every script is made up of 3 elements: the code itself, its sha1 and an alias. To load and create/update an alias for the script:
SCRIPT LOAD <script>
HMSET <alias> script <script> sha1 <sha1>
127.0.0.1:6379> HMSET isAnswer script "return tonumber(ARGV) == 42" sha1 1041fb7065092daf01ed47f50c3457ed4fd322ac OK
Executing aliased scripts
Exclusively client side
A client can use a flow such as:
HGET <alias> sha1
- If nil, return an error and load the script
EVALSHA <sha1> <n> <key1> ... <keyn> <arg1> ... <argn>
- If got a
NOSCRIPTerror (e.g. reboot)
HGET <alias> script
SCRIPT LOAD <script>
- Goto 2
Some server side
Major caveat: this is totally incompatible w/ cluster mode :sad:. The reason for that is that scripts can only access keys inside the same slot and there's no sane way to make sure of that in case of an alias and the keys it accesses.
Note: this basically bypasses the scripts' cache mechanism, for better or worse. This means that the client doesn't even have to bother with
An alternative approach is also possible with Lua's
loadstring using this helper (who's sha1 is
local script = redis.call('HGET', KEYS, 'script') if script then local func = loadstring(script) if func then table.remove(KEYS, 1) return func() else error('NOLOAD: Could not load script') end else error('NOALIAS: Script alias not found') end
With the Luasider helper loaded to Redis, the client-side flow becomes:
EVALSHA 30e1... <1+n> <alias> <key1> ... <keyn> <arg1> ... <argm>
- If errored:
NOALIASerror, same as Loading aliased scripts
NOLOADerror, means that the script can't be loaded
127.0.0.1:6379> EVALSHA 30e1b129be900acc2418c190b136ab7cec6078d6 1 isAnswer 43 (nil)
Now the only thing you need to worry about is how to alias the Luasider helper.
Side note: The same method can be slightly altered to use Redis' undocumented behavior in which cached scripts can be called from another Lua script using the
f_<sha1> notation. This is, however, unadvisable as this is undocumented as therefore could change in the future w/o notice.
- Script versioning (assert min version, backward compatible scripts, rollback, ...)
- Last script upload time (auditing)
- Script expiration
- Central npm like repository via @dvirsky (but w/o LEFTPAD-gate)
- Include the helper in core & allow SCRIPT LOAD from scripts :P