The Desi Architect
Notes / Docker Sandboxes / Redis and Lua Cheat Sheet
Track 06 · Docker Sandboxes · Reference

Redis and Lua Cheat Sheet

Redis commands grouped by data type, plus the few Lua pieces used in scripts. Every command here appears in a lab.

Free referenceKeep this tab open while you do a lab

00 /Getting in

docker exec -it <container> redis-cli

You get a prompt 127.0.0.1:6379>. Type exit to leave. Commands are not case sensitive, but everyone writes them in capitals. A reply starting with (integer) is a number, (nil) means "nothing there", and text is shown in quotes.

01 /Strings, counters and expiry

CommandWhat it does
SET key valueStore a value. Add EX 30 (seconds), PX 150 (milliseconds), or NX (only if the key does not exist).
GET keyRead it. A missing key gives (nil).
DEL keyDelete. Replies with how many keys were deleted.
EXISTS key1 if it exists, 0 if not.
INCR key, INCRBY key n, DECR keyChange a number in one atomic step.
EXPIRE key 60Give an existing key a 60 second life.
TTL keySeconds left. -1 means no expiry, -2 means no such key.
PERSIST keyRemove the expiry.

02 /Lists (queues)

CommandWhat it does
RPUSH key a b cAdd to the right end.
LPOP keyTake from the left end. First in, first out.
LRANGE key 0 -1Show everything.
LLEN keyHow many items.
LMOVE src dst LEFT RIGHTTake from one list and add to another in one atomic step. Used for "in progress" lists.
LREM key 1 valueRemove one matching item.

03 /Hashes, sets and sorted sets

TypeCommandsThink of it as
HashHSET k f v, HGET k f, HGETALL k, HINCRBY k f nOne row with several fields
SetSADD k a b, SCARD k, SISMEMBER k a, SINTER k1 k2A bag of unique things
Sorted setZADD k score member, ZREVRANGE k 0 9 WITHSCORES, ZINCRBY k n m, ZREVRANK k mA scoreboard

04 /Finding keys safely

CommandUse
SCAN 0 MATCH user:* COUNT 100Returns a few keys and a cursor. Repeat with the new cursor until it is 0. Safe on a busy server.
KEYS user:*Returns all matches in one go. Blocks everyone else on a big server. Practice only.
TYPE keyWhich type a key holds.
DBSIZEHow many keys.
Never on a live server

KEYS * and FLUSHALL (delete everything) are fine in a lab. On a real server one blocks everybody, and the other deletes everything.

05 /Looking at the server

CommandWhat it does
INFO memoryHow much RAM the data uses.
CONFIG GET maxmemoryRead a setting. 0 means no limit.
MONITORPrint every command the server receives. Slow. Never leave it running.
redis-cli --latencyMeasure round trip time to the server.

06 /Lua in Redis

EVAL "return redis.call('GET', KEYS[1])" 1 mykey

After the script comes a number: how many of the following words are key names. They arrive as KEYS. The rest arrive as ARGV. Both count from 1.

Lua pieceMeaning
local x = 5A variable. Always write local.
if a > b then ... else ... endA choice.
for i = 1, 4 do ... endA loop from 1 to 4.
t = {10, 20}; t[1]A list. The first item is t[1], not t[0].
#tThe length of a list.
a .. bJoin text.
tonumber(ARGV[1])Turn text into a number. Arguments always arrive as text.
redis.call(cmd, ...)Run a Redis command. An error stops the script.
redis.pcall(cmd, ...)Run a command and get the error back as a value.
Returning to RedisBecomes
A Lua number such as 3.7The integer 3 (decimals are cut)
true1
false(nil)
A table (list)A Redis array
Script commandWhat it does
SCRIPT LOAD "..."Store a script, get its 40 character fingerprint.
EVALSHA sha numkeys ...Run a stored script by fingerprint.
SCRIPT EXISTS shaIs it still stored? Redis forgets scripts on restart.
SCRIPT KILLStop a long running script, if it has not written anything.