redis
Scope
Covers Redis the server and the code that talks to it: structure selection and the encodings behind it, key naming, expiry and eviction, persistence and replication, transactions and scripting, locks, Pub/Sub and Streams, Cluster, connections and pipelining, the cache patterns that keep a database alive, the Redis Query Engine including vector search and semantic caching, ACLs, and the commands that answer "what is this instance doing right now".
The body is written against Redis 8.10 (current stable; 8.10.2 at time of writing, with
8.8.x, 8.6.x, 8.4.x, 8.2.x, 7.4.x, 7.2.x and 6.2.x still maintained). Rules that need a server
newer than 7.2 carry a gate such as (7.4+). Confidence markers: [verified] measured here
against 8.10.1 (the cluster-bus rules against 8.10.2), [official] from Redis documentation,
[community] otherwise.
Not covered:
- Relational schema, indexes and query plans. Use the
postgresskill. - Document modelling, aggregation pipelines and Atlas Search. Use the
mongodbskill. - A full-text search platform's own index lifecycle, relevance tuning and cluster operations.
Use the
elasticsearchskill. - How a given framework wires a cache, a session store or a queue up: annotations, config
files, DI, ORM second-level caches, migration of cache code. Use the
java-spring,laravel,fastapiornodejs-backendskill. This skill covers what those integrations do to Redis — the key shapes they produce, the TTLs they set or fail to set, the commands they issue per request. - Application-level cache architecture: which layer should hold which data, whether to cache at the CDN or in-process, cache hierarchies across services. That is a design question for the owning service, not a Redis question.
- Memcached. Not covered — the eviction, expiry and data-structure model is different, and nothing here transfers.
- Managed-hosting control planes: instance sizing and SKUs, provider backup and restore,
IAM-based auth, private endpoints, shard scaling through a provider API, Redis Cloud or
Redis Software administration, and vendor products layered on Redis. No skill in this
library covers those; say so rather than improvising provider commands. Everything at the
command and key-design level below still applies on a managed instance, but
CONFIG SET,DEBUG,MODULE LOADand the config file usually will not.
Core rules
- Establish the server before quoting a gated rule:
INFO serverforredis_versionandredis_mode,MODULE LISTfor the query engine and JSON. Hash-field TTLs need 7.4,FT.HYBRIDneeds 8.4, vector sets need 8.0, and aclusterinstance rejects multi-key code that works standalone. - Command execution is single-threaded. Every O(N) command is latency for every other
client, which is why
KEYS,SMEMBERS,HGETALL,LRANGE key 0 -1,SORTandSUNIONSTOREover large containers are dangerous — the problem is head-of-line blocking, not bandwidth. - Read the configuration from the instance, never from memory:
CONFIG GET maxmemory maxmemory-policy appendonly save. The stock values aremaxmemory 0,noeviction,appendonly no,save 3600 1 300 100 60 10000[verified]— an unbounded instance whose only limit is the kernel OOM killer, with a loss window up to an hour. - Choose the structure from the access pattern: how you read it (whole / by field / by rank /
by membership), how it shrinks, whether entries expire individually, and whether two of
these keys must be touched atomically. Data shape alone never decides
(
references/data-structures.md). - A serialised JSON document in a String makes every field update a read-modify-write of the
whole value, and makes concurrent updates lose writes. Use a Hash when fields change
independently; add
HEXPIRE/HGETEX(7.4+) when individual fields must expire. OBJECT ENCODINGis the memory story, and the thresholds are per-instance config, not folklore: on 8.10.1 the defaults arehash-max-listpack-entries 512,zset-max-listpack-entries 128,set-max-listpack-entries 128, value limit 64 bytes[verified]. Read them withCONFIG GET hash-max-listpack-*before claiming a structure has been promoted to a hashtable.- One key lives in one slot and is served by one core, so a single huge key — a 60M-member sorted set, a 2M-member set — is both a latency source and the reason one cluster shard is four times the size of the others. Split by bucket or move it out.
- A plain
SETreplaces the key including its expiry, so a session refresh written asSET key <new payload>silently makes the key immortal.KEEPTTLpreserves it;APPEND,SETRANGE,INCR,HSETandRENAMEpreserve it;GETSETclears it[verified]. - Every cache key gets an explicit TTL at write time, and the TTL gets jitter. A batch warmed with one constant TTL expires as a batch, which is how a warm-up job becomes a database outage an hour later.
volatile-lruand its siblings only consider keys that have a TTL. With no such key the instance answers writes withOOM command not allowed when used memory > 'maxmemory'— byte-identical tonoeviction[verified]. Pure cache:allkeys-lruorallkeys-lfu. Source of truth:noevictionplus an alert.volatile-*only when both live in one instance and every cache key has a TTL.- An expired key keeps its memory until something touches it or the active-expiry cycle
reaches it: with active expiry disabled, 300 keys stayed in
DBSIZEa full second after expiry andused_memorydid not drop; oneEXISTSfreed one key, a fullSCANfreed all of them[verified].DBSIZEcounts keys that are already logically gone. SCANguarantees only that every element present for the whole iteration is returned at least once; it may return duplicates and it may miss keys added mid-iteration.COUNTis a hint per call, andMATCHfilters after the bucket is read, so a selective pattern over a large keyspace still costs a full walk.- On the miss path, concurrent requests for the same key all recompute it. Serialise them
with a single-flight lock (
SET lock:<key> <token> NX EX <n>, the loser waits or serves stale) or refresh ahead of expiry; a longer TTL does not fix it, it only moves it. - Requests for keys that will never exist bypass the cache entirely. Cache the negative
result under a short TTL with a sentinel distinguishable from a real payload, and put a
Bloom filter (
BF.RESERVE/BF.EXISTS, built in on 8.0+) in front when the id space is attacker-controlled. - Never invalidate by pattern.
KEYS product:*blocks the server for the whole keyspace andSCANmerely spreads the same work out. Maintain a reverse index of the keys a change affects, or version the prefix so one write invalidates a whole class (references/caching-patterns.md). MULTI/EXECand Lua give isolation, not rollback. An error while queuing (unknown command, wrong arity) aborts the whole transaction withEXECABORT; an error at execution time does not —MULTI; INCR c; LPUSH str x; INCR c; EXECleavescat 2, and a script that fails halfway keeps the writes it already made[verified].- A pipeline is not a transaction. It is one round trip for N independent commands, and any
of them can fail on its own. Reach for
WATCH+MULTIfor compare-and-set, or a script when the read and the write must not be separated. - A Redis lock is a lease, not mutual exclusion. Store a unique token as the value, release
only via a compare-and-delete script or
FCALL, and accept that a holder which overruns the TTL loses the lock while still working. There is no fencing token, so anything whose correctness depends on exclusivity needs the guard in the resource itself (references/transactions-and-scripting.md). - Pub/Sub is at-most-once with no backlog: a subscriber that is not connected at publish
time never learns the message happened, and a slow one is disconnected on
client-output-buffer-limit. Anything that must survive a restart is a Stream. - Stream consumer groups are at-least-once.
XREADGROUPhands an entry over and keeps it pending untilXACK, so the handler must be idempotent, a dead consumer's entries needXAUTOCLAIM, and the stream needsMAXLEN/XTRIMor it grows forever. - In Cluster mode a key maps to one of 16384 slots by CRC16 of the key name, and every
multi-key command —
MGET,SDIFF,MULTI, a pipeline sent as one unit, a script with twoKEYS— requires all keys in one slot. A hash tag{...}makes only the tagged substring count; scope it to the entity (user:{1001}:profile), never to a bare id, and only where a multi-key operation actually exists. - A cluster client must follow
MOVEDandASKand refresh its slot map; pointing a single-node client at one cluster node works until the first resharding. Replica reads are opt-in and asynchronous, so read-your-own-writes breaks — fine for leaderboards and price lookups, wrong for state a request just wrote. - Share connections: a pool that leases per call, or a single multiplexed connection. Never
one connection per request. Blocking commands (
BLPOP,BLMOVE,XREAD BLOCK) need their own connection, because on a multiplexed one they stall every other caller. Set both a connect timeout and a command timeout, connect the shorter of the two. - Out of the box the
defaultuser ison nopass ~* +@all[verified]— full access with no password. Give each application its own ACL user with the key patterns and command categories it needs, and remove capability by category (-@dangerous,-@admin) rather than by renaming commands, which breaks clients and cluster tooling silently (references/security.md). - Finish by measuring, and quote the numbers:
DBSIZE,INFO memory(used_memory,mem_fragmentation_ratio,evicted_keys,expired_keys),INFO stats(keyspace_hits/keyspace_misses,rejected_connections),SLOWLOG GET, andredis-cli --bigkeysbefore and after. "It should be faster now" is not a result.
Paths below are relative to this skill's directory.
Workflows
choose-structures-and-design-the-key-space
- Write down the read patterns first — by id, by field, by rank, by membership, by time
range — and the write patterns beside them. Then pick per pattern, not per entity
(
references/data-structures.md). - Reject the two defaults people reach for without thinking: a serialised blob in a String (read-modify-write, lost updates) and a List named "queue" (no acknowledgement).
- Fix the naming: lowercase, colon-separated,
service:entity:id:attribute, a tenant segment in front when the instance is shared, and no URLs or free text as keys (references/keyspace-and-expiry.md). - Decide the expiry policy per key class at design time: which keys have a TTL, whether it slides, and what must happen when one is evicted early.
- Size it: estimate members per key and bytes per member, check the resulting encoding against the instance's listpack thresholds, and split anything heading past a few thousand members or a megabyte.
- Note every place two keys must change together — that is either a script, or a hash tag if the instance is or may become a cluster.
- Gate — measured on a real instance: load a representative sample, then confirm
MEMORY USAGE <key>,OBJECT ENCODING <key>andDEBUG SLEEP-freeSLOWLOGare what the design assumed. Quote the per-key bytes and the projected total.
fix-a-cache-that-is-hurting-the-database
- Classify the symptom before changing code: a periodic spike is synchronised expiry, a
spike on one id is a hot-key stampede, a flood of misses on ids that do not exist is
penetration, and a permanent regression is usually eviction or a missing TTL
(
references/caching-patterns.md). - Check the eviction policy against the key population:
CONFIG GET maxmemory maxmemory-policyplusINFO statsforevicted_keysandexpired_keys. Avolatile-*policy with unexpired keys is the whole bug. - Add jitter to every batch-written TTL, and refresh hot keys ahead of expiry rather than on the miss.
- Put a single-flight lock on the recompute path and decide explicitly what the losers do — wait with a bounded retry, or serve the stale value.
- Cache negative lookups with a sentinel and a short TTL; add a Bloom filter if the id space is unbounded.
- Replace pattern-based invalidation with a reverse index or a versioned prefix, and make every write path that mutates the source also touch the cache.
- Audit the refresh writes for dropped expiry: any
SETthat is meant to update a value but keep the TTL needsKEEPTTLor an explicit re-EXPIRE. - Gate — the source load actually fell: re-run the scenario and quote the before/after
of
keyspace_hits/keyspace_misses, the query count on the source, and the p99 of the endpoint. A hit-ratio number is only meaningful for the cache-aside prefixes —INFO statsis instance-wide and includes session, lock and queue traffic.
diagnose-memory-eviction-or-latency
- Separate "slow" from "blocked" first:
INFO clientsforblocked_clients,CLIENT LISTfor a connection stuck in a blocking read,INFO statsforrejected_connectionsagainstmaxclients(references/operations.md). - Get the slow commands rather than guessing:
SLOWLOG GET 25. The threshold isslowlog-log-slower-than, 10000 microseconds by default, and the log holdsslowlog-max-len128 entries[verified]— read it before it rolls. - Turn on latency monitoring if the answer is not in
SLOWLOG:latency-monitor-thresholddefaults to 0, meaningLATENCY DOCTORandLATENCY HISTORYhave nothing to report until you set it[verified]. - Attribute memory:
INFO memoryforused_memoryversusmaxmemoryandmem_fragmentation_ratio,redis-cli --bigkeysfor the shape of the keyspace,MEMORY USAGEon the suspects.MEMORY DOCTORdeclines to answer on a small instance, so do not treat its silence as health[verified]. - Decide whether the growth is data, expired-but-untouched keys, client output buffers or fragmentation — each has a different fix, and only the first is solved by a bigger box.
- Check the command mix with
INFO commandstatsandINFO latencystats: one O(N) command at low rate explains more p99 than a millionGETs. - Gate — the number moved: re-run the load and quote
used_memory,evicted_keys, theSLOWLOGentry count above threshold, and the p99 from--latency-history.
move-code-onto-a-cluster
- Inventory every multi-key operation in the code:
MGET/MSET, set and zset store commands,MULTIblocks, scripts with more than one key, and any pipeline whose commands are assumed to run together (references/cluster-and-connections.md). - For each one, decide between a hash tag and decomposition. Tag when the keys genuinely belong to one entity; decompose when the grouping was incidental — tagging everything rebuilds a single-node bottleneck inside the cluster.
- Choose the tag scope deliberately: the entity, not the raw id, so unrelated namespaces do not collide on one slot.
- Swap the client for a cluster-aware one and confirm it splits pipelines per node and
follows
MOVED/ASK; a single-node client aimed at one node is a latent outage. - Find the keys that cannot shard — one enormous sorted set, set or stream — and split or relocate them before blaming the shard balance.
- Decide replica reads per call site, not globally, and list the call sites that must stay on the primary because they read what they just wrote.
- Gate — proven on a cluster: run the suite against a real 3-primary cluster (not a
single node), confirm zero
CROSSSLOTerrors, and check distribution withCLUSTER KEYSLOTon the tagged keys plusDBSIZEper node.
build-a-queue-that-does-not-lose-messages
- Decide the delivery guarantee out loud. At-most-once is a List; at-least-once needs a
Stream with a consumer group; exactly-once does not exist and has to become
at-least-once plus an idempotent handler (
references/streams-and-messaging.md). - For at-least-once, lay out the four commands together:
XADDto produce,XREADGROUPto claim,XACKafter the side effect succeeded,XAUTOCLAIMto recover a dead consumer's pending entries. - Make the handler idempotent before wiring the ack: a
SET done:<id> 1 NX EX <n>guard or an idempotency key on the downstream call. - Bound the stream:
XADD ... MAXLEN ~ <n>or a periodicXTRIM MINID, and decide what happens to entries nobody ever acked. - Give consumers their own connections — a blocking read on a shared multiplexed connection stalls unrelated traffic.
- If a List has to stay, make it
BLMOVEfrom the queue into a per-consumer processing list and add the reaper that returns abandoned items. - Gate — kill a consumer mid-work: send
SIGKILLto a consumer between claim and ack, then show the entry inXPENDINGand showXAUTOCLAIMhanding it to another consumer and the work completing exactly once from the outside.
design-or-debug-a-search-or-vector-index
- Confirm the engine exists and its version:
MODULE LISTforsearch,ReJSON,vectorset. On 8.x the official image ships them; on 7.x it does not (references/search-and-vectors.md). - Match the field type to the query, not the value's Java type. An exact-match value in a
TEXTfield is tokenised and stemmed, so@sku:ABC-123returns zero results[verified]—TAGis the type for identifiers, statuses and enums. - Use
FT.EXPLAINon any query that returns nothing before touching the schema; it shows how the query was parsed, which is usually the whole answer. - For vectors, make
DIM,DISTANCE_METRICandTYPEfollow the embedding model exactly; a mismatch produces plausible garbage rather than an error. Choose the mechanism deliberately: an RQEVECTORfield when the query also filters on attributes or full text, a vector set (VADD/VSIM) for a standalone KNN collection — its defaultquant-typeisint8, which is lossy[verified]. - Filter before the vector comparison in the same query rather than over-fetching and filtering client-side.
- Change schemas behind an alias (
FT.ALIASUPDATE) so queries never point at an index being rebuilt. - Gate — profiled, not assumed: run
FT.PROFILE ... SEARCH QUERY "..."on the real query and quote the returned counters, plus a recall spot-check against a brute-forceFLATresult for the vector case.
review-a-deployment-config
- Read the four settings that decide whether the instance survives contact with
production:
maxmemory,maxmemory-policy,appendonly,save. Say what each stock value costs here (references/operations.md). - State the durability window explicitly. RDB-only with the default save points loses up
to an hour; AOF with
appendfsync everysecloses about a second;alwayscosts throughput. Then say whether that is acceptable for this data, and whether the data exists anywhere else. - Check the replication and failover story:
INFO replicationfor the role and lag, and whether anything actually promotes a replica. A replica alone is not high availability. - Audit access:
ACL LIST, whetherdefaultstill hasnopass, whether TLS is on, whatbindandprotected-modeare, and which commands the application user can reach. On a cluster, the bus port has no authentication unlesstls-cluster yes; check that andcluster-bus-port-protected-mode(references/security.md). - Check the connection ceiling:
maxclientsagainst pool size times instance count, andINFO statsforrejected_connections. - Confirm observability exists before the incident: metrics exported from
INFO, aslowlog-log-slower-thanthat matches the latency budget, andlatency-monitor-thresholdset to something other than 0. - Gate — the failure was rehearsed: kill the primary on a staging copy and record what the application does, how long the gap is, and what data is missing afterwards.
Topic router
| Topic | Read when | File |
|---|---|---|
| Structure selection per access pattern, Hash versus JSON, encodings and the measured listpack thresholds, big-key detection and splitting, Bitmap, HyperLogLog, geospatial, Bloom, Stream and vector-set storage, memory accounting | Deciding how to store something, or auditing what is already stored | references/data-structures.md |
Key naming and namespaces, the full TTL-preservation table, EXPIRE family and hash-field TTLs, lazy versus active expiry, every maxmemory-policy and its failure mode, LRU/LFU sampling, keyspace notifications | Keys vanish, keys never vanish, or writes start failing with OOM | references/keyspace-and-expiry.md |
| Cache-aside and write paths, TTL jitter, single-flight and stale-while-revalidate, negative caching and Bloom filters, versioned-prefix and reverse-index invalidation, client-side caching, rate limiting, semantic caching for LLM responses | Building or repairing a cache in front of another system | references/caching-patterns.md |
MULTI/WATCH semantics, pipelines versus transactions, Lua versus FUNCTION, script replication and determinism, lock leases with token release, why Redlock does not give what people think | Two operations must not interleave, or a lock is misbehaving | references/transactions-and-scripting.md |
Pub/Sub delivery and output-buffer limits, keyspace notifications as an event source, Stream anatomy, consumer groups, XPENDING/XAUTOCLAIM recovery, trimming, List queue patterns and their limits | Messages are lost, duplicated or piling up | references/streams-and-messaging.md |
Slot mapping and hash tags, the full CROSSSLOT surface, MOVED/ASK and slot-map refresh, resharding, connection pooling versus multiplexing, pipelining, timeouts and retries, replica reads | Moving to Cluster, or a client is the bottleneck | references/cluster-and-connections.md |
RDB save points and AOF fsync policies with their loss windows, rewrite behaviour, replication and failover, WAIT, the INFO fields worth alerting on, SLOWLOG, LATENCY, MEMORY, --bigkeys, --latency-history | Deciding durability, or answering "what is it doing right now" | references/operations.md |
ACL users, selectors and command categories, the default user's stock permissions, TLS, bind and protected-mode, cluster bus authentication and its real port, why rename-command is the fallback, script and injection surface, audit checklist | Hardening an instance or reviewing one against a finding | references/security.md |
FT.CREATE schema design, field types and the exact-match trap, query-syntax and tokenisation gotchas, FT.EXPLAIN/FT.PROFILE, vector fields versus vector sets, HNSW parameters, hybrid retrieval, index aliases | Working on a Redis Query Engine index or vector search | references/search-and-vectors.md |
Output format
When reviewing Redis usage — code, redis.conf, or both — group findings by file and lead each
with the location and the blast radius:
worker.py:22 - data-loss - RPOP removes the job before ship() succeeds, so a worker killed mid-flight drops the order. Replace with XREADGROUP + XACK after ship(), and XAUTOCLAIM to recover a dead consumer's pending entries. redis.conf:4 - outage - maxmemory-policy volatile-lru with no TTL on any key behaves exactly like noeviction: writes fail with OOM. Set allkeys-lru for a pure cache.
Order findings by blast radius: data loss first, then availability (OOM, blocking commands,
connection exhaustion), then correctness (lost updates, stale reads), then cost and style. For
a performance finding, quote the evidence — a SLOWLOG entry, a MEMORY USAGE, an
OBJECT ENCODING, a --bigkeys line — rather than asserting that something is slow, and give
the before/after command.
Environment
- Reproduce against the same major version as production.
docker run -d --name r redis:8-alpineis enough to settle expiry, eviction, encoding, transaction and cluster-slot questions, and running the question beats quoting a blog post. Addredis-server --enable-debug-command yeswhen a test needsDEBUG, which is refused by default. MODULE LISTtells you whether the query engine, JSON, Bloom and time-series are present. The 8.x official image bundles them; older images and self-compiled servers do not.redis-clicarries the diagnostics worth knowing:--bigkeys,--memkeys,--hotkeys(needs an LFU policy),--latency-history,--stat, and--cluster check.DEBUG,CONFIG SET,MODULE LOADandFLUSHALLare commonly disabled or ACL-blocked on managed instances. CheckACL WHOAMIandACL GETUSER <user>before proposing a command the application's credentials cannot run.


