Lua Introduction

Apply for Redis Developer Course Redis Regular Inspection / Technical Support Redis Enterprise Server

Introduction

Since Redis version 2.6.0, a Lua interpreter is built in so you can run Lua scripts.   It uses Lua 5.1.   This document is based on Redis version 5.0.7.

Command description

EVAL

  • Runs a Lua script. When used with no argument
    > EVAL "return redis.call('set', 'key', 'value')" 0
    OK
    > EVAL "return redis.call('get', 'key')" 0
    value
    Here the last 0 is the number of keys passed as arguments. Use 0 if there are none.

SCRIPT LOAD, EVALSHA

  • When you reuse a script, LOAD returns a SHA value. You can run the script repeatedly with that value. The script is cached in Redis.
    With EVALSHA, using arguments makes repeated use efficient.
    > SCRIPT LOAD "return redis.call('set', KEYS[1], ARGV[1])"
    "55b22c0d0cedf3866879ce7c854970626dcef0c3"
    > EVALSHA 55b22c0d0cedf3866879ce7c854970626dcef0c3 1 key10 value10
    OK
    > EVALSHA 55b22c0d0cedf3866879ce7c854970626dcef0c3 1 key11 value11
    OK
    When there are multiple keys
    > SCRIPT LOAD "return redis.call('mset', KEYS[1], ARGV[1], KEYS[2], ARGV[2])"
    "e290a9e5f8bdb9b954e332de5e6b661e4ea31c08"
    > EVALSHA e290a9e5f8bdb9b954e332de5e6b661e4ea31c08 2 key10 key11 value10 value11
    OK
  • When the script is large, LOAD helps reduce network traffic compared with running EVAL every time.

SCRIPT INFO

  • Shows memory used by the Lua interpreter, memory used by cached scripts, and the script count. This information is the same as the Lua info shown by info memory.

SCRIPT LIST

  • Lists cached scripts. Shows SHA and up to 70 bytes of the script. With the FULL option you can view up to 32767 bytes.

SCRIPT GET

  • Gets a script by SHA. The full script is returned with no size limit.

SCRIPT EXISTS

  • Checks whether a script is cached (loaded) by SHA.
    > SCRIPT EXISTS e290a9e5f8bdb9b954e332de5e6b661e4ea31c08
    1) (integer) 1

SCRIPT FLUSH

  • Flushes all Lua scripts cached in memory.

SCRIPT KILL

  • Kills a running Lua script.
    The condition is that lua-time-limit has passed and the script has not executed a write command.
    For details, see the SCRIPT KILL command.

INFO MEMORY

  • You can view Lua script related information.
    > info memory
    ... omitted ...
    used_memory_lua:90112   -> memory used by the Lua engine
    used_memory_lua_human:88.00K
    used_memory_scripts:3976   -> memory used by scripts (added in 5.0)
    used_memory_scripts_human:3.88K
    number_of_cached_scripts:27   -> number of scripts (added in 5.0)

AOF, Replication

  • From Redis 5.0, it is recorded with MULTI/EXEC and propagated to replica nodes.
    > EVAL "return redis.call('set','key','value')" 0
    OK
    AOF
  • Through Redis 4.x, the script is recorded and propagated.
  • Even in this version, if you run redis.replicate_commands() the commands are recorded.
    > EVAL "redis.replicate_commands() return redis.call('set','key','value')" 0
  • AOF REWRITE: Not recorded.
  • RDB: Recorded. Propagated to replica nodes.

SELECT

  • The select command to choose a DB is available. It applies only inside the script.
    > EVAL "redis.call('select',2) return redis.call('set','key','value')" 0

Commands that cannot be used inside a SCRIPT

  • Most commands can be used, but the following cannot.
  • Blocking commands such as BLPOP, BRPOP, BZPOPMIN, BZPOPMAX cannot be used.
  • PUBLISH can be used, but SUBSCRIBE cannot.
  • EVAL, SCRIPT, MULTI, EXEC cannot be used.
  • Admin commands not allowed: SAVE, BGSAVE, BEREWRITEAOF, LATENCY, SLOWLOG, REPLICAOF, DEBUG, ROLE, AUTH, SHUTDOWN, ...

Lua related parameters

  • lua_time_limit 5000 (5 seconds): After this time you can kill the running script.

Using Lua Script

  • keys myset* : list keys
    > EVAL "return redis.call('keys','myset*')" 0
    1) "myset1"
    2) "myset2"
  • Count: prefix with # to get the count.
    > EVAL "return #redis.call('keys','myset*')" 0
    (integer) 2
  • If the listed keys are SET, get the members of those keys.
    > EVAL "local members = redis.call('keys','myset*') local results = {} for index,key in ipairs(members) do results[index] = redis.call('smembers',key) end return results" 0
    1) 1) "BBB"
        2) "AAA"
    2) 1) "DDD"
        2) "EEE"
        3) "CCC"
  • If a listed key is not SET, it returns "(empty list or set)". To list only SET data types, the Enterprise version can add the 'set' option.
    > EVAL "local members = redis.call('keys','myset*','set') local results = {} for index,key in ipairs(members) do results[index] = redis.call('smembers',key) end return results" 0
  • Sum the values of STRING keys.
    > set key10 10
    > set key20 20
    > set key30 30
    > EVAL "local members = redis.call('keys','key*','string') local total = 0 for index,key in ipairs(members) do total = total + redis.call('get',key) end return total" 0
    (integer) 60
  • Insert Hash sample data
    script load "local num = 0 while num < tonumber(ARGV[1]) do num = num + 1 redis.call('hset','myhash','field-'..num, 'value-'..num) end return num"

Lua command list

CommandsVersionSyntaxDescription
EVAL2.6.0ScriptRun a script
EVALSHA2.6.0SHARun a stored script
SCRIPT LOAD2.6.0ScriptLoad a script
SCRIPT INFOEnt 6.0.0Show script info
SCRIPT LISTEnt 6.0.0FullList scripts
SCRIPT GETEnt 6.0.0SHAGet a script
SCRIPT EXISTS2.6.0SHACheck
SCRIPT FLUSH2.6.0 Remove scripts from memory
SCRIPT KILL2.6.0 Kill a running script
SCRIPT DEBUG3.2.0 Debug

Total : 10


<< Pub/Sub Intro Lua Script Intro EVAL >>

Email 답글이 올라오면 이메일로 알려드리겠습니다.