Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Fast lookups by key (key-value stores)

After this lesson: I can choose a key-value store for the right job.

50 min lesson10 commandsValkey lab: learn a Redis-compatible key-value store with keys, expiry, lists and counters

A shop that must answer in a blink

A shop's website must know who is logged in on every click. It must also count visitors and show the last five pages each one saw.

A normal database could do this, but it is slow when thousands of people click at once. A key-value store keeps these small facts in memory and answers in a fraction of a millisecond.

By the end of this lesson

Note

I can choose a key-value store for the right job.

Key words

WordWhat it meansExample
Key-value storea database that keeps a value under a name
Keythe name you use to find a valuesession:ada
Expirya time after which a key is removed by itself
Atomicdone in one step that cannot be cut in half
Listmany values in order under one key
Hashnamed fields under one key

The cells students fill in are left blank in this preview.

A key-value store works like a dictionary

A key-value store keeps a value under a key and gives it back when you ask for the key. It works much like a Python dictionary, but on a server that many programs share.

Valkey is a free store that keeps everything in memory. You send it a command such as SET or GET. Each reply starts with a symbol: + for a short reply, $ for text, : for a number and * for a list of replies.

Worked example: reading replies

You sendReplyIt means
SET stock:mug 40+OKStored
GET stock:mug$2, then 40Text of length 2, which is 40
INCR visits:1A number, the new value
GET nosuchkey$-1Nothing is there

The cells students fill in are left blank in this preview.

Make a tiny client called v

Command
v() ( printf '%s\r\n' "$*" | nc -w 1 valkey 6379 )

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Store the stock of a mug

Command
v SET stock:mug 40

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Ask for it back

Command
v GET stock:mug

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A key can expire by itself

A login session must not last for ever. With EX 60 a key lives for 60 seconds, then the store removes it. You write no cleanup code.

TTL asks how many seconds are left. When a key has gone, GET answers $-1.

Store a session for a minute

Command
v SET session:ada token123 EX 60

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Ask how long it has left

Command
v TTL session:ada

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

When does a key stop existing?

Code: python
# A key with an expiry is a value and the time it stops existing.
entry = ["token123", 1060]
for now in [1000, 1059, 1060, 1100]:
    if now < entry[1]:
        print(now, "still there:", entry[0])
    else:
        print(now, "expired")
# Change 1060 to 1090 and run again.

Counters and lists do work for you

INCR adds 1 to a number and returns the new value. It is atomic: a thousand visitors at once are all counted. A normal read, add and write could lose some.

A list keeps values in order under one key. RPUSH adds to the end and LLEN gives its length. A hash keeps named fields, such as a profile.

Counting views by hand

Code: python
# Counting page views in a dictionary: one read, one add, one write.
visits = {}
for page in ["home", "cart", "home", "home"]:
    if page in visits:
        visits[page] = visits[page] + 1
    else:
        visits[page] = 1
print(visits)
# A key-value store does this add in one step, so many writers cannot clash.

Count a visit to the home page

Command
v INCR visits:home

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Record three pages Ada saw

Command
v RPUSH recent:ada home cart pay

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Ask what kind of value a key holds

Command
v TYPE recent:ada

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Keep two fields of Ada's profile

Command
v HSET user:ada name Ada city Leeds

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Read every field back

Command
v HGETALL user:ada

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think a key-value store is a safe home for all data. Memory is limited, and a key can expire. Keep the master record in a database. Use the store for fast, short-lived facts.

Going further: five tries a minute

A login page allows five tries a minute for each user. Build that limit from two commands you know: INCR on a key such as tries:bo, and EXPIRE tries:bo 60 after the first try.

Run your commands six times. Write down the sixth reply and what the page should do with it.

About this lab and its source: Valkey lab: learn a Redis-compatible key-value store with keys, expiry, lists and counters

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.