Skip to the page

Redis in depth · senior

Redis Cluster, memory and hot keys

Cluster shards keys over hash slots. Multi-key work needs keys in one slot. A cluster spreads keys, not the load of one key, and client buffers can fill memory.

The notes

What you must know

  • Cluster: 16,384 hash slots spread across primaries, each with replicas. The slot is CRC16 of the key modulo 16,384. Clients cache the slot map and follow MOVED and ASK redirects.
  • Multi-key commands, transactions and scripts need all keys in one slot, otherwise you get CROSSSLOT. A hash tag (only the part in braces is hashed) forces keys together, for example {sku:123}:stock. Do not use one tag for everything: all data lands on one node. MULTI and EXEC is not a rollback transaction and does not check stock. Use one Lua script with the keys passed in KEYS.
  • Choose Sentinel when the data fits one machine. Choose Cluster when you need more memory or write throughput than one machine.
  • A hot key lives on one node, so adding nodes does not help. Use a short in-process cache, client-side caching with invalidation, several copies of the key on different slots, replica reads, a smaller value, or a CDN for static parts. Find hot keys with redis-cli --hotkeys (needs an LFU policy) and network graphs. Use MONITOR only briefly, because it is expensive.
  • used_memory includes more than data: client input and output buffers, replica buffers, the AOF buffer. Slow pub/sub subscribers with unlimited buffers can use gigabytes. Eviction removes only keys, so it does not free client buffers. Keep memory headroom above maxmemory for buffers and for fork copy-on-write. Look at INFO memory, CLIENT LIST (omem) and set output buffer limits.
  • Memcached versus Redis for a pure cache: Memcached is multi-threaded and simple, with no replication or types. Redis has types, replication and failover, and more per-key overhead. Decide by a benchmark with real sizes and key skew.

The red pen

Common traps: mistake → fix

  • Mistake: MULTI is a transaction that rolls back

    Fix: it only queues commands.

  • Mistake: Add cluster nodes to fix one hot key

    Fix: the key stays on one node.

  • Mistake: Hash tags on everything

    Fix: one hot slot.

  • Mistake: Raise maxmemory when memory is full

    Fix: find the buffers first.

Easy to hard

Questions interviewers ask, easy to hard

  1. Why does the code fail with CROSSSLOT after moving to Cluster?
  2. One primary is at 100% CPU during a sale. Why and what do you do?
  3. Redis is full at 12 GB but the data is 6 GB. Where is the rest?
  4. Redis, Valkey or Memcached for a pure cache?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview