Lesson 6 / 18

Working with Keys

Check, inspect, and safely scan keys.

EXISTS, DEL, TYPE

The everyday key-management trio: check presence, remove a key, and inspect its data type.

EXISTS user:1:name
TYPE user:1:name
DEL user:1:name

Output:

(integer) 1
string
(integer) 1

KEYS is dangerous in production

KEYS * scans every key in one blocking call and can freeze a busy server. Use SCAN instead — it walks the keyspace in small, non-blocking batches.

SCAN 0 MATCH "user:*" COUNT 100

Quick check: Which command should you use to list keys in a production system?

  • KEYS *
  • SCAN
  • GETALL
Answer

SCAN — `SCAN` iterates incrementally without blocking the server; `KEYS *` blocks until the whole keyspace is scanned.