Essential Redis Commands
GET, SET, DEL, KEYS, SCAN, EXPIRE, and TTL
Skills taught: redis.commands
Universal Key Commands
These commands work on any key regardless of type. They manage the key lifecycle: existence, deletion, renaming, type inspection, and expiry.
# Check existence
EXISTS user:1 # 1 if exists, 0 if not
EXISTS user:1 user:2 user:3 # count of existing keys
# Delete
DEL user:1 # deletes one key
DEL user:1 user:2 # deletes multiple
UNLINK user:1 # async delete (non-blocking, prefer for large keys)
# Inspect
TYPE user:1 # string, list, set, zset, hash, stream
OBJECT ENCODING user:1 # internal encoding
# Rename
RENAME old:key new:key # overwrites new:key if exists
RENAMENX old:key new:key # only if new:key does not exist
Expiry and TTL
Every Redis key can have a Time-To-Live (TTL). Redis automatically deletes expired keys. This is the foundation of caching.
# Set with inline expiry (most common)
SET session:abc "data" EX 3600 # expire in 3600 seconds
SET otp:user1 "123456" PX 300000 # expire in 300000 ms (5 min)
# Add expiry to an existing key
EXPIRE session:abc 7200 # 2 hours from now
PEXPIRE session:abc 86400000 # 24 hours in milliseconds
# Expire at a Unix timestamp
EXPIREAT promo:sale 1735689600 # exact Unix timestamp
# Check remaining TTL
TTL session:abc # seconds remaining (-1 = no expiry, -2 = key gone)
PTTL session:abc # milliseconds remaining
# Remove expiry (make key permanent)
PERSIST session:abc
- TTL (Time-To-Live)
- The number of seconds (or milliseconds with PTTL) until Redis automatically deletes a key. Returns -1 if no expiry is set, -2 if the key does not exist.
Scanning Keys Safely
Warning: Never use KEYS * in production. It blocks the entire Redis server while scanning all keys — with millions of keys, this freezes your application for seconds.
# SCAN iterates safely (cursor-based, non-blocking)
# Returns a cursor + a batch of keys
SCAN 0 MATCH user:* COUNT 100
# Returns: 1) "2748" (next cursor)
# 2) ["user:1", "user:2", ...]
# Keep scanning until cursor returns 0 (full iteration)
SCAN 2748 MATCH user:* COUNT 100
# ... repeat with returned cursor ...
SCAN 0 MATCH user:* COUNT 100 # cursor 0 = complete
# Type-specific scan
HSCAN myhash 0 MATCH field:* COUNT 50
SSCAN myset 0 MATCH prefix:*
ZSCAN myzset 0 MATCH a*
Pipelining for Bulk Operations
Every Redis command requires a network round-trip. Pipelining batches multiple commands into one network request, dramatically improving throughput for bulk operations.
// Without pipeline: 100 round-trips
for (const id of userIds) {
await redis.get(`user:${id}`);
}
// With pipeline: 1 round-trip
const pipeline = redis.pipeline();
for (const id of userIds) {
pipeline.get(`user:${id}`);
}
const results = await pipeline.exec();
// results is an array of [error, value] tuples
Tip: Use UNLINK instead of DEL for large keys (big lists, hashes). UNLINK removes the key immediately from the keyspace but frees memory asynchronously in the background.
Frequently Asked Questions
How do I rename a key?
RENAME oldkey newkey. If newkey already exists it is overwritten. Use RENAMENX to only rename if newkey does not exist.
How do I copy a key?
COPY source destination [DB db] [REPLACE]. Available since Redis 6.2.
Can I set expiry on a hash field?
Not in standard Redis. TTL applies to the whole key. Redis 7.4+ added HEXPIRE for field-level expiry, but it is not yet widely available on managed services.