Lesson 6 / 20

Working with Keys

Check, inspect and delete keys with EXISTS, TYPE and DEL, and scan production keyspaces safely with SCAN.

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 *
  • GETALL
  • FLUSHALL
  • SCAN
Answer

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