Redis 8 cheatsheet

Redis 8: Keys and strings

11 common Redis 8 tasks.

Set a value

Store a string value under a key, replacing any existing value.

SET greeting "hello"

Get a value

Read a string value. Returns nil if the key does not exist.

GET greeting

Set a value that expires

Store a value that is deleted after 3600 seconds.

SET session:42 abc123 EX 3600

Set only if the key does not exist

Useful as a simple lock. Returns nil if the key exists.

SET lock:job1 1 NX EX 30

Delete a key

Remove a key and its value. Returns the number of keys deleted.

DEL greeting

Check if a key exists

Returns 1 if the key exists, 0 if not.

EXISTS greeting

Check time to live

Seconds until expiry, -1 for no expiry, -2 if the key does not exist.

TTL session:42

Set an expiry

Make an existing key expire after 60 seconds.

EXPIRE greeting 60

Increment a counter

Atomically add 1 to a numeric value, creating it at 0 if missing.

INCR page:views

Find keys by pattern

Iterate over keys without blocking the server.

SCAN 0 MATCH user:* COUNT 100

Caution: Avoid KEYS * on a production server. It blocks Redis while it scans every key.

Check a key's type

Show whether a key holds a string, list, hash and so on.

TYPE user:1

Was this page helpful?