Redis Key Commands
Every piece of data in Redis lives under a key. Key commands let you check, rename, copy, search, and delete keys no matter which data type they hold. Strong key habits keep a growing database clean and fast.
The Filing Cabinet Analogy
A filing cabinet holds folders with labels on the tabs. You find a folder by its label, rename the label, copy a folder, or shred it. Key commands do the same jobs for Redis data. The folder content can be a String, List, Hash, Set, or any other type.
Redis Keyspace ┌─────────────────────────────────────────┐ │ user:1001 → Hash │ │ cart:1001 → List │ │ views:article:7 → String │ │ tags:article:7 → Set │ └─────────────────────────────────────────┘
Naming Keys with Colons
Redis does not enforce a naming style. Most teams use colons to split a name into readable parts: object type, ID, and field.
Good: user:1001:profile Good: order:2026:884:items Weak: data1 Weak: u1001p
Short names save memory. Clear names save debugging time. Aim for both.
EXISTS and TYPE
127.0.0.1:6379> EXISTS user:1001 (integer) 1 127.0.0.1:6379> EXISTS user:9999 (integer) 0 127.0.0.1:6379> TYPE user:1001 hash
EXISTS returns the number of listed keys that exist. TYPE reveals the data type behind a key.
DEL and UNLINK
127.0.0.1:6379> DEL temp:1 temp:2 (integer) 2 127.0.0.1:6379> UNLINK big:report (integer) 1
DEL frees memory immediately and blocks other clients while it works. UNLINK removes the key name at once and frees the memory in the background. Prefer UNLINK for large Lists, Sets, and Hashes.
RENAME and COPY
127.0.0.1:6379> RENAME draft:5 post:5 OK 127.0.0.1:6379> COPY post:5 backup:post:5 (integer) 1
RENAME replaces the destination key without warning. Use RENAMENX to rename only when the new name is free.
Finding Keys Safely: KEYS versus SCAN
KEYS accepts a pattern and returns every match in one reply.
127.0.0.1:6379> KEYS user:*
On a database with millions of keys, KEYS freezes the server while it searches. Avoid it on live systems. SCAN walks the keyspace in small steps and keeps Redis responsive.
127.0.0.1:6379> SCAN 0 MATCH user:* COUNT 100 1) "2048" 2) 1) "user:1001" 2) "user:1002" 127.0.0.1:6379> SCAN 2048 MATCH user:* COUNT 100 1) "0" 2) 1) "user:1003"
SCAN Loop cursor 0 ──► batch 1 ──► cursor 2048 ──► batch 2 ──► cursor 0 (start) (finished)
Keep calling SCAN with the returned cursor. A returned cursor of 0 means the walk is complete. SCAN may return the same key twice, so your code must handle duplicates.
Other Useful Commands
- RANDOMKEY returns one random key for sampling.
- DBSIZE counts the keys in the current database.
- OBJECT ENCODING shows how Redis stores a value internally.
- FLUSHDB deletes every key in the current database. Use it with great care.
Practical Example – Cleaning Old Sessions
Goal: Remove all keys that start with "session:old:".
Wrong: KEYS session:old:* then DEL each key
(blocks the server on large data)
Right: Loop SCAN 0 MATCH session:old:* COUNT 500
Send UNLINK for each batch of keys
Stop when the cursor returns to 0
Key Points
- Colon-separated names keep keys readable.
- EXISTS checks presence and TYPE reports the data type.
- UNLINK deletes large keys without blocking.
- SCAN replaces KEYS on live servers.
- RENAME overwrites the target, so use RENAMENX when overwriting is unsafe.
