Skip to content
Sign inGet all access
Chapter 2·14 minFree

Keys and Expiry

Naming, TTLs, and the only garbage collection there is.

Chapter 1 said a key is the only way in. So: what to call them, and how to expire them.

Key naming

There is no enforced scheme. The convention everyone uses is colon-separated segments, from general to specific:

SET user:1138:name ada
SET user:1138:session abc123
GET user:1138:name
OutputOKOK"ada"

Pick one shape and keep it. A codebase with two conventions has two namespaces nobody can list.

SET with options

SET takes modifiers that remove a round trip. EX sets a lifetime in seconds:

SET token abc EX 100
TTL token
OutputOK(integer) 100

NX writes only if the key is absent; XX only if it exists:

SET lock 1 NX
SET lock 2 NX
GET lock
SET lock 3 XX
GET lock
SET fresh 1 XX
OutputOK(nil)"1"OK"3"(nil)

One trap worth meeting now. A plain SET over an existing key discards its TTL:

SET k v1 EX 100
SET k v2
TTL k
OutputOKOK(integer) -1

KEEPTTL is the fix:

SET k v1 EX 100
SET k v2 KEEPTTL
TTL k
OutputOKOK(integer) 100

EXPIRE and TTL

EXPIRE adds a lifetime to a key that already exists:

SET city London
TTL city
EXPIRE city 60
TTL city
OutputOK(integer) -1(integer) 1(integer) 60

It returns whether it did anything, so a missing key is a 0 rather than an error:

EXPIRE nothing 60
Output(integer) 0

Minus one and minus two

TTL has three kinds of answer, and two of them are negative:

SET forever v
SET temp v EX 60
TTL forever
TTL temp
TTL missing
OutputOKOK(integer) -1(integer) 60(integer) -2

PERSIST

It removes an expiry, turning a temporary key permanent:

SET k v EX 100
TTL k
PERSIST k
TTL k
PERSIST k
OutputOK(integer) 100(integer) 1(integer) -1(integer) 0

How keys actually expire

No timer per key. A key goes when something reads it, or when a background job samples it:

SET blink v PX 300
EXISTS blink
OutputOK(integer) 1
continues:after=0.6
EXISTS blink
GET blink
DBSIZE
Output(integer) 0(nil)(integer) 0
terminal
redis-cli INFO stats | grep expired_keys
Output$ redis-cli INFO stats | grep expired_keysexpired_keys:1expired_keys_active:1

SCAN, and never KEYS

SCAN walks the keyspace in batches. It replies with a cursor and some keys:

SET only:1 v
SCAN 0
OutputOK1) "0"2) 1) "only:1"

The first element is the cursor for next time. A 0 means finished; anything else is where to resume.

MATCH filters, and belongs with a generous COUNT:

MSET u:1 a other 1
SCAN 0 MATCH u:* COUNT 100
OutputOK1) "0"2) 1) "u:1"

KEYS does the same job in one reply, and that is the problem:

SET u:1 a
KEYS u:*
OutputOK1) "u:1"

RENAME, COPY, TOUCH

SET old v
RENAME old new
EXISTS old
GET new
COPY new dup
GET dup
TOUCH new dup missing
OutputOKOK(integer) 0"v"(integer) 1"v"(integer) 2

TOUCH counts how many exist, and counts as a read — see chapter 10’s LRU.

RENAME overwrites the destination without asking. RENAMENX refuses instead:

SET a 1
SET b 2
RENAME a b
GET b
SET c 3
RENAMENX c b
OutputOKOKOK"1"OK(integer) 0

Choosing a TTL

A key with no TTL is permanent. Give cached data an expiry as you write it:

SET page:home:html "<h1>hi</h1>" EX 300
SET config:version 7
TTL page:home:html
TTL config:version
OutputOKOK(integer) 300(integer) -1