Keys & redis-cli
Key naming, SCAN vs KEYS, inspecting keys, and command complexity
Everything in Redis is a key pointing to a value of some type. There are no tables and no schemas; the key names are your schema. This lesson covers naming them, finding them, and inspecting them safely.
Key naming
Use colon-separated namespaces, from general to specific:
app:product:1001 # hash: one product
app:category:peripherals # set: product IDs in a category
cache:product:1001 # string: cached JSON, has a TTL
session:7f3a9c... # hash: one web session
rate:fixed:user-42:179178 # string: a rate-limit window counter
lb:weekly # sorted set: a leaderboardConventions that pay off:
- Prefix by purpose (
cache:,session:). It makes ACLs possible (theappuser is restricted to these prefixes), and lets you find and measure each kind of data. - Keep keys short but readable. Every key name is stored in memory;
u:42vsuser:profile:42matters at 100 million keys, not at 10,000. - Include the ID last, so
SCAN MATCH app:product:*finds a whole family. - In Redis Cluster, use hash tags like
{user:42}:cartto keep related keys on one node. See Redis Cluster.
Keys are binary-safe up to 512 MB. Don't come anywhere near that.
Seed some data
docker compose -f compose_prod.yaml run --rm labs python seed.py
docker exec -it redis redis-cli --user app --pass app123Finding keys: SCAN, never KEYS
127.0.0.1:6379> SCAN 0 MATCH app:product:* COUNT 100
1) "0" # next cursor; "0" means the iteration is complete
2) 1) "app:product:1003"
2) "app:product:1001"
...SCAN walks the keyspace incrementally: each call does a bounded
amount of work and returns a cursor for the next call. COUNT is a hint
of how much work per call, not how many results. Keep calling until the
cursor comes back as 0.
KEYS pattern does the same thing in one blocking O(N) call. On a
production instance with millions of keys it freezes Redis for seconds.
The app user here can't run it at all, because KEYS is in the
@dangerous ACL category.
SCAN guarantees every key that exists for the whole iteration is
returned at least once, possibly more than once, and keys added or
removed during the scan may or may not appear. Deduplicate on the client
if it matters. There are type-specific versions too: HSCAN, SSCAN,
ZSCAN iterate inside one big key.
Inspecting a key
Generic key commands
Complexity is the API
Redis documentation lists the time complexity of every command. It matters more here than in most databases, because one slow command blocks every client:
| Command | Complexity | Safe on a huge key? |
|---|---|---|
GET, SET, HGET, SADD, SISMEMBER | O(1) | Yes |
ZADD, ZRANK, ZINCRBY | O(log N) | Yes |
LRANGE k 0 9, ZRANGE k 0 9 | O(log N + M) for M returned | Yes, if M is small |
HGETALL, SMEMBERS, LRANGE k 0 -1 | O(N) | No |
DEL on a big hash/set/zset | O(N) | No; use UNLINK |
KEYS, FLUSHALL (sync) | O(N) over the keyspace | Never in production |
SUNION, SINTER, ZUNIONSTORE | Depends on input sizes | Careful |
A useful habit: before using a command in code, check its complexity and ask "how big can N get?"
redis-cli tricks
redis-cli --user default --pass redis123 --bigkeys # biggest key of each type (uses SCAN)
redis-cli ... --memkeys # keys using the most memory
redis-cli ... --latency # continuous PING latency
redis-cli ... --stat # live ops/s, memory, clients
redis-cli ... --scan --pattern 'cache:*' | head # SCAN from the shell
redis-cli ... -r 5 -i 1 INFO stats | grep ops_per_sec # repeat a command
redis-cli ... --eval script.lua key1 , arg1 # run a Lua fileLab
- Run
seed.py, then useSCANto list everyapp:*key. - For each key, run
TYPEandOBJECT ENCODING. Which types appear? - Run
redis-cli --bigkeys(asdefault). Which key is biggest? - As the
appuser, tryKEYS *. Read the error. - Find the TTL of a
cache:product:*key after runningcache_aside.py. RunTTLa few times and watch it count down.