Redis · Chapter 2 of 26
Strings, and finding keys
By the end of this chapter you can store and read many keys in one command with MSET and MGET, set a key only when it is missing (or only when it is there) and get its old value back, work inside a string, rename keys safely, and find keys on a busy Redis with SCAN instead of KEYS. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Redis from the command line
- 4 tasks
- Redis
- Free chapter
Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.
Why you need this
The match centre shows both squads: twenty-two names. With chapter 1's commands that is twenty-two GETs, and every command is a trip from the app to Redis and back. On match night two lakh fans open the page, so the trips, not Redis's work, are what add up.
Fans also pick a handle when they sign up, and two fans can ask for the same handle in the same second. Reading the key with GET and then writing it with SET is chapter 1's lost count all over again: both app servers read "free", both write, and the second fan quietly takes the first fan's handle. Rhea has had that support ticket before.
Then there is last season's code, which named keys like m2001_status. This season's code uses match:2001:status, and Gurpreet must move the old keys over without losing anything newer. And when she needs one match's keys on the live server, chapter 1 has already told her: never KEYS.
The lesson
Many keys in one trip
MSET takes a key, its value, the next key, its value, and so on, and stores them all. MGET takes many keys and answers their values in the order you named them, with (nil) in the place of a key that does not exist:
127.0.0.1:6379> MSET player:P101:name "Sai Verma" player:P102:name "Vihaan Thakur" player:P103:name "Shaurya Pillai"
OK
127.0.0.1:6379> MGET player:P101:name player:P102:name player:P999:name player:P103:name
1) "Sai Verma"
2) "Vihaan Thakur"
3) (nil)
4) "Shaurya Pillai"
The reply is a list, numbered from 1, one line for each key you asked for. Because the order matches, the app knows that the third name is missing without asking again.
The quotes matter even more here. Without them, the key and value pairs stop lining up:
127.0.0.1:6379> MSET player:P104:name Gurpreet Naidu
(error) ERR wrong number of arguments for 'mset' command
Three arguments cannot be pairs, so Redis refuses. With an even number of words it would be worse: Redis would happily store Naidu as a key.
Is one trip really faster? redis-cli can repeat a command with -r. Here are a thousand GETs of a name, against two hundred MGETs of five names, which is the same thousand values:
$ time redis-cli -r 1000 GET player:P101:name > /dev/null
real 0m0.050s
$ time redis-cli -r 200 MGET player:P101:name player:P102:name player:P103:name player:P104:name player:P105:name > /dev/null
real 0m0.013s
The same thousand names in a quarter of the time, and that is with Redis on the same machine. Across a real network each trip costs more, so the gap grows. MSET and MGET still run as one command: Redis does them whole, one at a time, like every other command.
Only if missing, only if there
SET has options that make it careful. NX stores the value only if the key does not exist yet; XX only if it does. When the option says no, SET stores nothing and answers (nil) instead of OK:
127.0.0.1:6379> SET handle:sai10 u1001 NX
OK
127.0.0.1:6379> SET handle:sai10 u1002 NX
(nil)
127.0.0.1:6379> GET handle:sai10
"u1001"
Fan u1001 got the handle, and fan u1002's attempt changed nothing. This is the fix for two fans signing up at once: the check and the write are one command, and Redis runs one command at a time, so the second fan can never slip in between them. Whoever arrives first wins, and the other one hears (nil) and picks another handle.
XX is the opposite, for updating something that must already exist:
127.0.0.1:6379> SET match:2002:status upcoming XX
(nil)
127.0.0.1:6379> EXISTS match:2002:status
(integer) 0
127.0.0.1:6379> SET match:2002:status upcoming
OK
127.0.0.1:6379> SET match:2002:status delayed XX
OK
127.0.0.1:6379> SET match:2002:status live NX XX
(error) ERR syntax error
The first XX did nothing because the key was not there yet. NX and XX together make no sense, and Redis says so.
The GET option makes SET answer the value it replaced (or (nil) if there was none), so you learn the old value and write the new one in one step:
127.0.0.1:6379> SET match:2002:status upcoming GET
"delayed"
127.0.0.1:6379> GET match:2002:status
"upcoming"
Read and delete together
GETDEL answers a string's value and deletes the key in the same command. It suits something that must be used once, like a message waiting for a fan:
127.0.0.1:6379> SET match:2002:toss "Chennai Cyclones chose to bowl"
OK
127.0.0.1:6379> GETDEL match:2002:toss
"Chennai Cyclones chose to bowl"
127.0.0.1:6379> GETDEL match:2002:toss
(nil)
The second GETDEL found nothing: the first one took it.
Inside a string
A string can be changed and read in pieces. APPEND adds text to the end (starting from nothing if the key is missing) and answers the new length. STRLEN answers the length, and GETRANGE answers a piece, from one position to another, counting from 0. Negative positions count from the end, so -1 is the last character:
127.0.0.1:6379> SET match:2002:note "Rain at"
OK
127.0.0.1:6379> APPEND match:2002:note " Marina Park"
(integer) 19
127.0.0.1:6379> STRLEN match:2002:note
(integer) 19
127.0.0.1:6379> GETRANGE match:2002:note 0 3
"Rain"
127.0.0.1:6379> GETRANGE match:2002:note -11 -1
"Marina Park"
These count bytes, which is the same as characters for plain English text. Most of the time you will GET and SET whole strings; these are for when a value grows bit by bit.
Renaming keys
RENAME old new gives a key a new name and keeps its value. It is one command, so nobody ever sees the key under both names or under neither:
127.0.0.1:6379> SET m2002_status upcoming
OK
127.0.0.1:6379> RENAME m2002_status match:2002:status
OK
127.0.0.1:6379> GET match:2002:status
"upcoming"
127.0.0.1:6379> RENAME m2002_status match:2002:status
(error) ERR no such key
Renaming a key that is not there is an error. The bigger trap is the other way round: when the new name already exists, RENAME overwrites it without a word. RENAMENX renames only when the new name is free, and answers 1 if it renamed and 0 if it did not:
127.0.0.1:6379> SET m2002_venue "Marina Park"
OK
127.0.0.1:6379> SET match:2002:venue "Marina Park, Chennai"
OK
127.0.0.1:6379> RENAMENX m2002_venue match:2002:venue
(integer) 0
127.0.0.1:6379> GET match:2002:venue
"Marina Park, Chennai"
127.0.0.1:6379> RENAME m2002_venue match:2002:venue
OK
127.0.0.1:6379> GET match:2002:venue
"Marina Park"
RENAMENX kept the newer value. The plain RENAME after it replaced the newer value with last season's: exactly what must not happen when old and new code have both been writing.
Finding keys with SCAN
SCAN walks through the keyspace a few slots at a time. You give it a cursor, starting at 0; it answers two things: the cursor to use next, and the keys it found in this step. When it answers cursor 0 again, the walk is over. MATCH pattern keeps only the names that match (with * as in KEYS), and COUNT n asks it to look at about n slots per call (the usual is 10).
Here is a Redis with 509 keys: 500 fantasy teams from lab fill 500, four keys for each of matches 2002 and 2003, and a cached page. First, one step with only a pattern:
127.0.0.1:6379> DBSIZE
(integer) 509
127.0.0.1:6379> SCAN 0 MATCH match:2002:*
1) "416"
2) (empty array)
An empty list, though four keys match. This step looked at about ten slots and none of them held a match:2002 key. The cursor 416 says the walk is not over. Following the cursors with a bigger step finds them:
127.0.0.1:6379> SCAN 0 MATCH match:2002:* COUNT 200
1) "262"
2) 1) "match:2002:away"
127.0.0.1:6379> SCAN 262 MATCH match:2002:* COUNT 200
1) "211"
2) (empty array)
127.0.0.1:6379> SCAN 211 MATCH match:2002:* COUNT 200
1) "0"
2) 1) "match:2002:venue"
2) "match:2002:home"
3) "match:2002:status"
The cursors jump around (0, 262, 211, 0): they are positions in Redis's own table, not counts, so always use the one Redis gave you. Keys come in no order, and a key can turn up twice if Redis grows its table during a walk, so treat what you collect as a set.
On a small Redis, one SCAN with a big COUNT looks at every slot in one call and answers cursor 0 straight away:
127.0.0.1:6379> SCAN 0 MATCH match:2002:* COUNT 1000
1) "0"
2) 1) "match:2002:away"
2) "match:2002:venue"
3) "match:2002:home"
4) "match:2002:status"
That is fine for a file in this class. On a big Redis you keep COUNT modest and follow the cursor in a loop, which chapter 9 does in Node.js.
Why is this safe when KEYS is not? Each SCAN call does a small, bounded piece of work and returns. On a million keys, one SCAN call took 4 ms, where chapter 1's KEYS took over 100 ms:
$ time redis-cli SCAN 0 MATCH "team:99999*" COUNT 1000
1) "392192"
2) (empty array)
real 0m0.004s
A full walk still looks at every key, but in a thousand short steps, and other fans' commands run in between them.
UNLINK
UNLINK deletes keys like DEL and answers how many it removed, but frees their memory in the background, so a very big value does not hold Redis up. For small strings the two are the same; chapter 22 comes back to it.
127.0.0.1:6379> UNLINK match:2002:status match:2002:venue
(integer) 2
Your turn
Four tasks, one file each. Task 1 stores five Mumbai batters' names with one MSET and reads them with one MGET (mind the quotes). Task 2 claims a fan's handle only if it is free; a (nil) there is the right answer. Task 3 moves last season's keys to the new names without overwriting the newer venue. Run lab run setup.redis first for tasks 2 and 3. Task 4 is the interview question: replace KEYS with a complete SCAN (run lab fill 500 and setup.redis first), and Farhan will ask why SCAN is safe and why it can answer an empty list. Try each file with lab run, and with lab fresh to run it exactly as the Check will.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
squad.redis: five names in one trip
The match centre shows Mumbai Monsoons' top five batters. Store their names as strings with one MSET command: player:P123:name holds Anirban Singh, player:P124:name holds Dhruv Rathore, player:P125:name holds Yash Patil, player:P126:name holds Dhruv Chauhan and player:P127:name holds Sai Menon.
Then read all five back with one MGET command, in that order (P123 first, P127 last), so it answers the five names in that order.
Use no SET for these keys: one MSET and one MGET. Names are case-sensitive, and each one has a space in it.
- Task 2
handle.redis: claim a handle only if it is free
A fan's handle is a key handle:<handle> that holds the fan's id. The handle ananya347 already belongs to fan u1001 (the key handle:ananya347 holds u1001). Fan u1047, Mohit Desai, signs up.
First he tries to take ananya347: set handle:ananya347 to u1047, but only if the key does not exist yet. Redis must answer (nil), and the key keeps u1001.
Then he takes his own handle, mohit372: set handle:mohit372 to u1047, only if it does not exist. Redis answers OK.
End with a GET of handle:ananya347 (it answers "u1001") and a GET of handle:mohit372 (it answers "u1047").
Every SET on a handle: key must use the option that sets only when the key is missing. The Check makes handle:ananya347 first; setup.redis does the same in your own Redis.
- Task 3
rename.redis: move last season's keys to the new names
Last season's code named match keys with underscores. Move tonight's three old keys to this season's names: m2001_status becomes match:2001:status and m2001_toss becomes match:2001:toss, each with one command that renames a key (their values stay the same: live, and Mumbai Monsoons chose to bat).
This season's code has already written match:2001:venue, with the newer value Pune Cricket Ground, Pune. Move m2001_venue to match:2001:venue only if match:2001:venue does not exist yet: use RENAMENX, which answers 0 here and leaves both keys alone. Then delete m2001_venue.
At the end no m2001_ key is left, and match:2001:venue still holds Pune Cricket Ground, Pune. Do not write the new keys with SET or MSET: rename them. The Check makes the four keys first; setup.redis does the same in your own Redis.
- Task 4 · interview question
Interview: scan.redis, find a match's keys without KEYS
Gurpreet needs every key of match 2003, the keys whose names start with match:2003: (there are four). scan.redis finds them with KEYS, which looks at every key while the whole server waits. Replace it with SCAN: start at cursor 0 and ask Redis to keep only names that match the pattern match:2003:*.
The walk must be complete: the last SCAN must answer cursor 0, and the SCANs together must answer exactly match 2003's four keys and no others. In the Check's Redis there are 509 keys (500 fantasy teams and a few other matches' keys), so one SCAN with Redis's usual step does not finish: ask for a bigger step, or follow the cursors until Redis answers 0. Do not use KEYS.
The Check makes the 509 keys first. In your own Redis run lab fill 500 and then setup.redis.
Farhan will ask: why is SCAN safe on a busy server when KEYS is not, and why can a SCAN answer an empty list when matching keys exist?
What you will learn
- MSET and MGET: many keys in one command, one trip.
- SET with NX, XX and GET: store only if missing, only if present, and get the old value back.
- GETDEL: read a key and delete it in one step.
- APPEND, STRLEN and GETRANGE: working inside a string.
- RENAME and RENAMENX: new names for keys, without losing newer ones.
- SCAN with MATCH and COUNT: finding keys a few at a time, and UNLINK.
More in Redis from the command line
Build "Strings, and finding keys" yourself
GullyXI, a Pune fantasy cricket app, falls over on match night. You make it fast with Redis. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.