Redis · Chapter 1 of 26

Redis, and your first keys

By the end of this chapter you can say what Redis is and why keeping data in memory makes it fast, talk to it with redis-cli and with files of commands, store and read strings with SET and GET, name keys the way GullyXI does, check for keys and delete them with EXISTS and DEL, count with INCR, and explain why KEYS has no place on a busy server. 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

GullyXI is a fantasy cricket app from Pune. Fans pick eleven real players for a match, join contests with an entry fee, and climb a leaderboard as the match is played. On a match night of the T20 league, about two lakh fans open the app in the ten minutes before the toss, and every one of them lands on the match centre: the match, both squads and the contests. GullyXI's main database takes about 800 ms to build that page, and it builds the same page again for every fan who opens it.

At last season's final the database gave up at 7:31 pm, a minute after the toss. Gurpreet, who was on call, was paged; the app showed a spinner for eleven minutes; and Venkat, who runs finance and operations, spent the next day refunding entry fees for contests fans could not join. Rhea, the founder, wants this season's opener to hold.

A bigger database would cost lakhs a month and still do the same work two lakh times. What GullyXI needs is a place to keep the finished page, and the counts and lists the app reads all night, that answers in well under a millisecond. Farhan, GullyXI's staff engineer, picks Redis for it, and you build it with him. You do not need to have used Redis before: this lab starts at the beginning. The first class is Redis on its own, typed in redis-cli; from chapter 9 you use it from Node.js.

The lesson

A slow page and a fast one

Your lab has GullyXI's database (as slow as it is on match night) and your own Redis. lab page opens the match centre the way the app will: it asks Redis for the page first, and only builds it from the database when Redis does not have it. Run it twice:

$ lab page
Redis has no page:match:2001: built it from the database in 808 ms (3552 bytes), then saved it in Redis.
$ lab page
Redis has page:match:2001: read 3552 bytes from Redis in 0.7 ms.

The first fan waited 808 ms. The second got the same 3,552 bytes a thousand times faster, and the database did nothing at all. Delete the copy from Redis and the next open is slow again:

$ redis-cli DEL page:match:2001
(integer) 1
$ lab page
Redis has no page:match:2001: built it from the database in 816 ms (3552 bytes), then saved it in Redis.

Keeping a copy of slow answers like this is called a cache. With it, two lakh fans cost the database one build of the page, not two lakh. Chapter 3 teaches how the copy expires on its own, and chapter 10 builds this cache in Node.js.

What Redis is

Redis is a server, redis-server, that keeps its data in memory, in the computer's RAM, instead of on a disk. Everything in it is stored under a key, a name like page:match:2001, and each key holds one value. This chapter's values are strings: text or numbers. Chapters 4 to 7 meet the other kinds a key can hold (hashes, lists, sets and sorted sets).

Memory is what makes it fast. Reading RAM takes a fraction of a microsecond, and Redis's commands are small and simple, so Redis does a command's work in a few microseconds. Most of the 0.7 ms above was the trip to Redis and back. A database does far more for each query: it plans it, reads pages from disk, and joins tables.

Memory also has two costs, and you must know both:

  • It is limited, and dearer than disk. A million small keys took 101 MB of this lab's memory (below).
  • It is lost when the server stops, unless Redis is told to save it to disk (chapter 21). So GullyXI's database stays the real record: Redis holds copies and counters that can be rebuilt.

Redis runs one command at a time, one after another, never two at once. Each command is quick, so this is fast, and it means a command never sees another one half done. Remember it for the last task.

Talking to Redis

Your lab starts your own Redis. redis-cli in the terminal opens it, and its prompt waits for commands; PING asks whether Redis is there:

127.0.0.1:6379> PING
PONG

A command is a line: the command's name, then its arguments, separated by spaces. Names work in any case (ping, PING); keys and values do not. Leave redis-cli with quit.

In this class you also write commands in files, one command a line, exactly as you would type them at the prompt; a line that starts with # is a comment. lab run file.redis runs a file in your Redis and prints every command and its reply as redis-cli would. lab fresh file.redis runs it in a fresh, empty Redis, the way your Check does.

SET and GET

SET key value stores a string under a key, and GET key reads it back:

127.0.0.1:6379> SET player:P101:name "Sai Verma"
OK
127.0.0.1:6379> GET player:P101:name
"Sai Verma"
127.0.0.1:6379> GET player:P999:name
(nil)
127.0.0.1:6379> SET player:P101:name "Sai Verma, opener"
OK
127.0.0.1:6379> GET player:P101:name
"Sai Verma, opener"
127.0.0.1:6379> TYPE player:P101:name
string
  • SET answers OK, and a second SET of the same key replaces the value.
  • GET shows a string in double quotes, and (nil) for a key that does not exist.
  • TYPE says what kind of value a key holds.

Careful with spaces. redis-cli splits a line at every space, so a value with spaces needs quotes. Without them, SET gets extra arguments it reads as options:

127.0.0.1:6379> SET player:P101:name Sai Verma
(error) ERR syntax error
127.0.0.1:6379> get player:p101:name
(nil)

The second line finds nothing: the command works in lower case, but the key player:p101:name is not player:P101:name.

Naming keys

All of GullyXI shares one Redis, and every key lives in one big space of names, so the names carry the structure. The habit everyone uses is parts joined by colons: what the thing is, its id, then the fact. player:P101:name is player P101's name, match:2001:status is match 2001's status, and page:match:2001 is the finished page for match 2001.

  • Keep one fact under one key, so each can change on its own.
  • Use the same words everywhere: match, never match here and game there.
  • Names are case-sensitive and may hold any characters, but stay readable and leave out spaces.

Keys for the same thing then share a start (match:2001:), which is how you find them, and how chapter 20 designs a whole keyspace.

EXISTS and DEL

EXISTS answers how many of the keys you name exist, and DEL deletes keys and answers how many it really deleted:

127.0.0.1:6379> SET match:2002:status upcoming
OK
127.0.0.1:6379> EXISTS match:2002:status
(integer) 1
127.0.0.1:6379> EXISTS match:2002:status match:2002:toss
(integer) 1
127.0.0.1:6379> DEL match:2002:status match:2002:toss
(integer) 1
127.0.0.1:6379> EXISTS match:2002:status
(integer) 0
127.0.0.1:6379> DEL match:2002:status
(integer) 0

Both take several keys at once. Deleting a key that is not there is not an error: DEL just counts it as nothing deleted.

Counting with INCR

INCR adds 1 to the number under a key and answers the new number. A missing key counts as 0, so the first INCR makes it 1:

127.0.0.1:6379> INCR page:match:2001:views
(integer) 1
127.0.0.1:6379> INCR page:match:2001:views
(integer) 2
127.0.0.1:6379> INCRBY page:match:2001:views 10
(integer) 12
127.0.0.1:6379> DECR page:match:2001:views
(integer) 11
127.0.0.1:6379> GET page:match:2001:views
"11"
127.0.0.1:6379> TYPE page:match:2001:views
string
127.0.0.1:6379> INCR player:P101:name
(error) ERR value is not an integer or out of range

INCRBY adds any whole number, and DECR takes 1 away. The count is still a string, "11": Redis reads it as a number for INCR and stores the result as text again. A value that is not a whole number cannot be counted.

Finding keys, and why not KEYS

KEYS pattern lists every key that matches a pattern, where * stands for anything. It is handy on your own Redis, and dangerous on a busy one. To see why, give your Redis a million keys, about as many fantasy teams as GullyXI holds for a big match:

$ lab fill 1000000
Added 1000000 keys, team:1 to team:1000000, in 1.6 s.
$ redis-cli INFO memory | grep used_memory_human
used_memory_human:101.12M
$ time redis-cli KEYS "team:99999*"
 1) "team:999995"
 2) "team:999999"
 3) "team:99999"
 ...
11) "team:999992"

real	0m0.113s

Eleven keys came back, in no particular order, but Redis had to look at all million names to find them: 113 ms. And since Redis runs one command at a time, everything else waits. A PING takes 3 ms on its own; sent from a second terminal while KEYS "team:*" was running, it took 176 ms:

$ time redis-cli PING
PONG

real	0m0.176s

On match night that pause hits every fan at once. So never run KEYS on a busy server; chapter 2 teaches SCAN, which walks the keys a few at a time. lab reset empties your Redis again.

Your turn

Four tasks, one file each, at the top. Task 1 stores tonight's match status and toss (mind the spaces in the toss). Task 2 deletes the keys a load test left, with one DEL: run lab run setup.redis first to get the Check's keys in your own Redis. Task 3 counts fans joining the Mega Contest without ever setting the count. Task 4 is an interview question: two app servers lost a count, and Farhan will ask why, and why INCR cannot lose one. 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.

  1. Task 1

    match-status.redis: tonight's match in two keys

    Store two facts about tonight's opener, match 2001, as strings. The key match:2001:status holds the word live. The key match:2001:toss holds the text Mumbai Monsoons chose to bat, all of it, spaces included.

    Then read both keys back with GET, so the file's replies show them: one GET for each key.

    Key names and values are case-sensitive: write them exactly as here.

  2. Task 2

    cleanup.redis: clear out the test match

    Gurpreet's load test left three keys for a match that never happened, match 1999: match:1999:status, match:1999:toss and match:1999:score. Delete all three with one DEL command (Redis answers 3).

    Then ask Redis, with one EXISTS command, how many of match:1999:status and match:1999:toss still exist. It must answer 0.

    Tonight's key match:2001:status is in the same Redis and must stay as it is. The Check makes these four keys before it runs your file; run setup.redis in the terminal to get them in your own Redis.

  3. Task 3

    contest-count.redis: count the Mega Contest

    Venkat wants a live count of fans in the Mega Contest, kept under the key contest:71:joined, which does not exist yet. Count, one command each: three fans join (add 1 three times), one fan leaves (take 1 away), then a group of five friends joins at once (add 5 with one command).

    End with a GET of contest:71:joined, which answers 7.

    Never SET the counter: let Redis do the counting. The Check runs your file in an empty Redis.

  4. Task 4 · interview question

    Interview: two-servers.redis, two app servers, one counter

    GullyXI counts app opens during a match in stats:2001:app_opens, which stands at 41. Two app servers each counted one open by reading the count with GET and writing it back plus one with SET. two-servers.redis replays what they sent, in the order Redis received it, and the count ends at 42, not 43: an open was lost.

    Fix the file: count each of the two opens with one command that adds 1, so the count ends at 43 in whatever order the two servers' commands arrive. Do not use SET on stats:2001:app_opens. The Check sets the count to 41 first; setup.redis does the same in your own Redis.

    Farhan will ask: why does reading and then writing lose a count when two servers do it at once, and why can the command that adds 1 not lose one?

What you will learn

  • What Redis is: a server that keeps keys and their values in memory.
  • redis-cli: typing commands to Redis, and running files of them.
  • SET and GET: strings under keys, and how to name the keys.
  • EXISTS and DEL: whether keys are there, and removing them.
  • INCR, INCRBY and DECR: counting, inside Redis.
  • KEYS, and why not to use it on a busy server.

More in Redis from the command line

Build "Redis, and your first 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.