As we build Lua scripts to perform a set of operations, it’s good to start with a short example that isn’t terribly complicated or structure-intensive. In this case, we’ll start by writing a Lua script combined with some wrapper code to post a status message.
LUA SCRIPTS—AS ATOMIC AS SINGLE COMMANDS OR MULTI/EXECAs you already
know, individual commands in Redis are atomic in that they’re run one at a
time. With MULTI/EXEC, you can prevent other commands from running
while you’re executing multiple commands. But to Redis, EVAL and EVALSHA
are each considered to be a (very complicated) command, so they’re executed
without letting any other structure operations occur.
LUA SCRIPTS—CAN’T BE INTERRUPTED IF THEY HAVE MODIFIED STRUCTURESWhen
executing a Lua script with EVAL or EVALSHA, Redis doesn’t allow any other read/
write commands to run. This can be convenient. But because Lua is a generalpurpose
programming language, you can write scripts that never return, which
could stop other clients from executing commands. To address this, Redis offers
two options for stopping a script in Redis, depending on whether you’ve performed
a Redis call that writes.
If your script hasn’t performed any calls that write (only reads), you can execute
SCRIPT KILL if the script has been executing for longer than the configured
lua-time-limit (check your Redis configuration file).
If your script has written to Redis, then killing the script could cause Redis to
be in an inconsistent state. In that situation, the only way you can recover is to
kill Redis with the SHUTDOWN NOSAVE command, which will cause Redis to lose
any changes that have been made since the last snapshot, or since the last
group of commands was written to the AOF.
Because of these limitations, you should always test your Lua scripts before
running them in production.
As you may remember from chapter 8, listing 8.2 showed the creation of a status message.
A copy of the original code we used for posting a status message appears next.
Generally speaking, the performance penalty for making two round trips to Redis in
order to post a status message isn’t very much—twice the latency of one round trip.
But if we can reduce these two round trips to one, we may also be able to reduce the
number of round trips for other examples where we make many round trips. Fewer
round trips means lower latency for a given group of commands. Lower latency means less waiting, fewer required web servers, and higher performance for the
entire system overall.
To review what happens when we post a new status message: we look up the user’s
name in a HASH, increment a counter (to get a new ID), add data to a Redis HASH, and
increment a counter in the user’s HASH. That doesn’t sound so bad in Lua; let’s give it
a shot in the next listing, which shows the Lua script, with a Python wrapper that
implements the same API as before.
This function performs all of the same operations that the previous all-Python version
performed, only instead of needing two round trips to Redis with every call, it should
only need one (the first call will load the script into Redis and then call the loaded
script, but subsequent calls will only require one). This isn’t a big issue for posting status
messages, but for many other problems that we’ve gone through in previous chapters,
making multiple round trips can take more time than is necessary, or lend to
WRITING KEYS THAT AREN’T A PART OF THE KEYS ARGUMENT TO THE SCRIPTIn the
note in section 11.1.1, I mentioned that we should pass all keys to be modified
as part of the keys argument of the script, yet here we’re writing a HASH based
on a key that wasn’t passed. Doing this makes this Lua script incompatible with the future Redis cluster. Whether this operation is still correct in a noncluster
sharded server scenario will depend on your sharding methods. I did this to
highlight that you may need to do this kind of thing, but doing so prevents you
from being able to use Redis cluster.
SCRIPT LOADERS AND HELPERSYou’ll notice in this first example that we have
two major pieces. We have a Python function that handles the translation of
the earlier API into the Lua script call, and we have the Lua script that’s
loaded from our earlier script_load() function. We’ll continue this pattern
for the remainder of the chapter, since the native API for Lua scripting (KEYS
and ARGV) can be difficult to call in multiple contexts.
Since Redis 2.6 has been completed and released, libraries that support Redis scripting
with Lua in the major languages should get better and more complete. On the
Python side of things, a script loader similar to what we’ve written is already available
in the source code repository for the redis-py project, and is currently available from
the Python Package Index. We use our script loader due to its flexibility and ease of
use when confronted with sharded network connections.
As our volume of interactions with Redis increased over time, we switched to using
locks and semaphores to help reduce contention issues surrounding WATCH/MULTI/
EXEC transactions. Let’s take a look at rewriting locks and semaphores to see if we
might be able to further improve performance.
TRY REDIS ENTERPRISE CLOUD FREE
Redis Enterprise Cloud provides complete automation of day-to-day database operations. Start now with 30MB of free storage.
Click on the link we sent you to activate your account and complete your signup process