00 /Getting in
docker exec -it <container> redis-cliYou 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
| Command | What it does |
|---|---|
SET key value | Store a value. Add EX 30 (seconds), PX 150 (milliseconds), or NX (only if the key does not exist). |
GET key | Read it. A missing key gives (nil). |
DEL key | Delete. Replies with how many keys were deleted. |
EXISTS key | 1 if it exists, 0 if not. |
INCR key, INCRBY key n, DECR key | Change a number in one atomic step. |
EXPIRE key 60 | Give an existing key a 60 second life. |
TTL key | Seconds left. -1 means no expiry, -2 means no such key. |
PERSIST key | Remove the expiry. |
02 /Lists (queues)
| Command | What it does |
|---|---|
RPUSH key a b c | Add to the right end. |
LPOP key | Take from the left end. First in, first out. |
LRANGE key 0 -1 | Show everything. |
LLEN key | How many items. |
LMOVE src dst LEFT RIGHT | Take from one list and add to another in one atomic step. Used for "in progress" lists. |
LREM key 1 value | Remove one matching item. |
03 /Hashes, sets and sorted sets
| Type | Commands | Think of it as |
|---|---|---|
| Hash | HSET k f v, HGET k f, HGETALL k, HINCRBY k f n | One row with several fields |
| Set | SADD k a b, SCARD k, SISMEMBER k a, SINTER k1 k2 | A bag of unique things |
| Sorted set | ZADD k score member, ZREVRANGE k 0 9 WITHSCORES, ZINCRBY k n m, ZREVRANK k m | A scoreboard |
04 /Finding keys safely
| Command | Use |
|---|---|
SCAN 0 MATCH user:* COUNT 100 | Returns 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 key | Which type a key holds. |
DBSIZE | How 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
| Command | What it does |
|---|---|
INFO memory | How much RAM the data uses. |
CONFIG GET maxmemory | Read a setting. 0 means no limit. |
MONITOR | Print every command the server receives. Slow. Never leave it running. |
redis-cli --latency | Measure round trip time to the server. |
06 /Lua in Redis
EVAL "return redis.call('GET', KEYS[1])" 1 mykeyAfter 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 piece | Meaning |
|---|---|
local x = 5 | A variable. Always write local. |
if a > b then ... else ... end | A choice. |
for i = 1, 4 do ... end | A loop from 1 to 4. |
t = {10, 20}; t[1] | A list. The first item is t[1], not t[0]. |
#t | The length of a list. |
a .. b | Join 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 Redis | Becomes |
|---|---|
| A Lua number such as 3.7 | The integer 3 (decimals are cut) |
true | 1 |
false | (nil) |
| A table (list) | A Redis array |
| Script command | What it does |
|---|---|
SCRIPT LOAD "..." | Store a script, get its 40 character fingerprint. |
EVALSHA sha numkeys ... | Run a stored script by fingerprint. |
SCRIPT EXISTS sha | Is it still stored? Redis forgets scripts on restart. |
SCRIPT KILL | Stop a long running script, if it has not written anything. |