Skip to content

Redis and Valkey

Redis 8.10 · Valkey 9.1

Redis and Valkey run every command on one main thread, one command at a time, against data structures held in memory. That makes each command atomic for free, and it makes every command’s cost its time complexity times the size of the key it touches — one O(N) call on a big key stalls every other client. Choose the structure whose commands are cheap for your access pattern, and treat the server as a cache unless you have configured it to be more.

TypeCore commandsCost to watch
StringSET, GET, INCRO(1)
HashHSET, HGET, HGETALLHGETALL is O(N)
ListLPUSH, RPOP, LRANGELRANGE is O(S+N)
SetSADD, SISMEMBER, SMEMBERSSMEMBERS is O(N)
Sorted setZADD, ZRANGE … BYSCOREO(log N) per write
StreamXADD, XREADGROUP, XACKO(1) append

A key holds exactly one type, and commands for the wrong type fail with WRONGTYPE. Strings double as counters (INCR is atomic); hashes are objects; sorted sets are leaderboards, rate-limit windows and priority queues; streams are append-only logs with consumer groups.

Terminal window
redis-cli HSET user:42 name Ann plan pro # 2
redis-cli HGET user:42 plan # "pro"
redis-cli ZADD board 310 ann 120 bob # 2
redis-cli ZRANGE board 0 0 REV WITHSCORES
# 1) "ann" 2) "310"
redis-cli INCR hits:2026-11-02 # 1

Gotcha: KEYS pattern is O(N) over the whole database and blocks every client while it runs; the docs warn it “may ruin performance”. Iterate with SCAN 0 MATCH user:* COUNT 100 instead.

SettingEffect
SET k v EX 60Expire in 60 s
HEXPIRE k 60 FIELDS 1 fPer-field TTL (Redis 7.4+, Valkey 9.0+)
maxmemory-policy noevictionDefault: writes fail when full
allkeys-lru / allkeys-lfuEvict any key, approx. LRU / LFU
volatile-*Evict only keys that have a TTL
allkeys-lrmLeast recently modified (Redis 8.6+)

Eviction only starts once maxmemory is reached. The default policy, noeviction, returns an error on writes instead of evicting, which is right for a primary store and wrong for a cache. For a pure cache, allkeys-lru is the documented “good default”.

Terminal window
redis-cli CONFIG SET maxmemory 2gb
redis-cli CONFIG SET maxmemory-policy allkeys-lru
redis-cli SET session:abc '{"u":42}' EX 1800
redis-cli TTL session:abc # 1800
redis-cli PERSIST session:abc # 1 — TTL removed

Gotcha: the volatile-* policies “behave like noeviction” when no key has a TTL. A cache written without EX under volatile-lru fills up and then rejects every write with an OOM error.

PatternWrite pathRisk
Cache-asideApp writes DB, deletes keyBrief stale reads
Write-throughApp writes DB and cacheCache holds unread data
Short lock on missOne caller recomputesOthers wait or serve stale
TTL jitterRandom spread on expiryNone worth naming

Cache-aside is the default: read the cache, fall back to the database on a miss, then populate with a TTL. On write, delete the key rather than updating it, so a concurrent reader cannot overwrite fresh data with a stale value. A stampede is many callers missing the same hot key at once; a short SET NX lock lets one caller recompute.

import Redis from "ioredis";
const redis = new Redis();
async function cached(key: string,
fn: () => Promise<string>): Promise<string> {
for (;;) {
const hit = await redis.get(key);
if (hit !== null) return hit;
if (await redis.set(`lock:${key}`, "1",
"PX", 5000, "NX")) break; // we recompute
await new Promise((r) => setTimeout(r, 100));
}
const v = await fn();
await redis.set(key, v, "EX", 300); // + jitter
return v;
}

Gotcha: if the loader takes longer than the lock’s PX, the lock expires mid-load and a second caller starts recomputing too. Size the lock TTL above your slowest load, not your average one.

StepCommand
AcquireSET lock:k <token> NX PX 30000
Release (Redis 8.4+)DELEX lock:k IFEQ <token>
Release (Valkey 9.0+)DELIFEQ lock:k <token>
Release (older)Lua: GET then DEL if equal

Acquire with a random token, a TTL and NX; release only if the key still holds your token, atomically. A plain DEL can delete a lock another client acquired after yours expired. The two engines added different commands for compare-and-delete, so check which one you run.

import Redis from "ioredis";
import { randomUUID } from "node:crypto";
const redis = new Redis();
const token = randomUUID();
const ok = await redis.set("lock:invoice:9", token,
"PX", 30000, "NX");
if (ok === "OK") {
try { /* critical section */ }
finally {
await redis.call("DELEX", "lock:invoice:9",
"IFEQ", token); // Valkey: DELIFEQ key token
}
}

Warning: a Redis lock is a lease, not a mutex. A GC pause longer than the TTL lets two holders act at once, and replication is asynchronous, so a failover can hand the lock out twice. The Redis docs tell you to “implement fencing tokens” when correctness matters.

Pub/subStreams
DeliveryAt-most-onceAt-least-once with groups
Offline subscriberMisses messagesReads from its last ID
StorageNonePersisted, trimmed by MAXLEN
Ack and retryNoneXACK, XAUTOCLAIM

Pub/sub fans a message out to whoever is connected right now and keeps nothing. Streams append entries with IDs; a consumer group tracks each consumer’s pending entries until it acknowledges them, so a crashed worker’s messages can be claimed by another.

Terminal window
redis-cli XGROUP CREATE orders billing '$' MKSTREAM
redis-cli XADD orders MAXLEN '~' 100000 '*' id 9
redis-cli XREADGROUP GROUP billing w1 COUNT 10 \
BLOCK 5000 STREAMS orders '>'
# ack the entry ID that XREADGROUP returned
redis-cli XACK orders billing 1762128000000-0
redis-cli XPENDING orders billing # unacked

Gotcha: with > you only ever get new entries. After a crash, a consumer must read its own backlog with ID 0 (or another worker must XAUTOCLAIM), or those messages sit in the pending list forever.

ToolAtomicRound trips
PipelineNo1 for N commands
MULTI … EXECYes, no rollback1 if pipelined
WATCH + MULTIOptimistic check-and-set2+
Lua / FUNCTIONYes1

A pipeline only batches network round trips; other clients’ commands can interleave. MULTI/EXEC queues commands and runs them back to back with nothing in between. WATCH aborts the EXEC (returns null) if a watched key changed, so you retry.

import Redis from "ioredis";
const redis = new Redis();
const res = await redis.multi()
.incr("stock:sku1:sold")
.decrby("stock:sku1", 1)
.exec();
// [[null, 1], [null, 99]] — [error, result] pairs

Gotcha: “Redis does not support rollbacks”. If one queued command fails at run time (say INCR on a hash), the others in the transaction still execute. exec() reports it per command.

ModeDefaultData at risk on crash
RDB snapshotsOn: 3600 1, 300 100, 60 10000Since last snapshot
AOFappendonly no~1 s with appendfsync everysec
Both offsave ""Everything

RDB forks and writes a compact point-in-time file; the default rules save after an hour with 1 change, 5 minutes with 100, or a minute with 10000. AOF logs every write and is replayed on restart; with the default everysec fsync you can lose about a second of writes. Use both when the data matters; disable both for a pure cache.

Terminal window
redis-cli BGSAVE # fork + RDB
redis-cli INFO persistence | grep rdb_last_bg
redis-cli CONFIG SET appendonly yes # starts AOF
redis-cli CONFIG GET appendfsync # everysec
Redis 8.10Valkey 9.1
LicenceRSALv2, SSPLv1 or AGPLv3BSD 3-Clause
OriginRedis Ltd.Fork from before the licence change
Built inQuery engine, JSON, time seriesCore data structures
Lock releaseDELEX … IFEQDELIFEQ
LRM evictionYes (8.6+)No

Redis 8 moved to a choice of three licences, AGPLv3 among them; 7.2 and earlier stay BSD. Valkey forked “right before the transition” and stays BSD; Amazon ElastiCache now makes it the default engine for new clusters. The core commands, protocol and clients are shared, so ioredis and redis-cli work against both; newer commands diverge.

Terminal window
redis-cli INFO server | grep _version
redis-cli COMMAND INFO DELEX DELIFEQ # nil = absent