Skip to main content
← PROBLEMS / ARCHITECTURE
An end-to-end domain learning journey

Solving Redis Cluster Cross-Slot
Errors with Hash Tags.

Migrating multi-key MGET, MULTI/EXEC, or Lua scripts to a 16,384-slot Redis Cluster fails with CROSSSLOT errors whenever keys hash to different shard primaries.
Start the journey5 stages
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

When a single bottleneck threatens end-to-end availability, colocate strictly atomic entity keys on one slot using `{hash_tags}` (e.g., `{user:1042}:balance`), and replace cross-entity MGET/MSET with slot-grouped parallel scatter-gather pipelines.

TL;DR30-second simplified summary
▾

Core Mechanics: Redis Cluster partitions the keyspace into 16,384 hash slots (CRC16(key) & 16383) across 3 primaries (0–5460, 5461–10922, 10923–16383) with no distributed two-phase commit.

Visual & Structural Flow: Depicts Untagged Keys Scatter Across Shards and Fail Lua/MGET (Payments Svc -> Primary 1 (0..5460) -> Primary 2 (5461..10922)); Fine-Grained {user:1042} Colocation Across 3 Primaries (P1 -> P2 -> P3 -> R1..3 -> 0..5460).

Trade-Offs & Production: Wrapping a static prefix in braces—such as {payments}:user:1042:balance—forces Redis Cluster to hash only "payments" for every key in the system. Solve both atomic per-entity scripts and cross-entity batches cleanly: Entity-Scoped {hash_tags}: Name keys {user:1042}:balance, {user:1042}:ledger, and {user:1042}:nonce.

Failure Modes & Application: Slot-aware key design is foundational when scaling Redis Cluster: Rate Limiters & Sliding Windows: Colocate {tenant:42}:tokens and {tenant:42}:ts for atomic Lua token-bucket updates.

Full walkthrough · 5 stages
01
STAGE 01 · WHY IT MATTERSL4 · FUNDAMENTALS

Why standalone multi-key Redis commands fail in Redis Cluster.

Why this matters: The mechanism: understand how Why standalone multi-key Redis commands fail in Redis Cluster works from first principles.

Think of Why standalone multi-key Redis commands fail in Redis Cluster as the intuition layer for Solving Redis Cluster Cross-Slot Errors with Hash Tags: The mechanism: understand how Why standalone multi-key Redis commands fail in Redis Cluster works from first principles.

Redis Cluster partitions the keyspace into 16,384 hash slots (CRC16(key) & 16383) across 3 primaries (0–5460, 5461–10922, 10923–16383) with no distributed two-phase commit. Migrating a standalone Lua transfer script (user:1042:balance, user:1042:ledger, user:1042:nonce) or a 50-key MGET immediately throws CROSSSLOT Keys in request don't hash to the same slot.

02
STAGE 02 · IMPACTL5 · APPLIED DESIGN

Broken atomic ledger scripts and 25 ms sequential MGET fallbacks.

Why this matters: The problem: see what breaks without Broken atomic ledger scripts and 25 ms sequential MGET fallbacks under real concurrency or scale.

Two critical payment workflows break on cluster cutover:

  1. Atomic Lua / MULTI/EXEC Failure: EVAL cannot touch balance on Shard 1 and nonce on Shard 2, failing all transfers.
  2. 50-Key MGET Latency Cliff: Falling back to 50 sequential GET calls at 0.5 ms RTT turns a < 1 ms batch read into 25 ms.
CROSSSLOT Error vs. Slot Grouping

Untagged Keys Scatter Across Shards and Fail Lua/MGET

Payments SvcPrimary 1 (0..5460)Primary 2(5461..10922)1. EVAL balance (1204) + nonce(9811)2. ERR CROSSSLOT (differentshards)3. Sequential 50x GET = 25ms4. {user:1042}:* -> same slot (1RTT)

Entity hash tags colocate atomic per-user keys onto one slot, while slot-grouped pipelines execute cross-user MGET in 1 parallel RTT.

Engineering Decision Framework
Problem
How to implement Solving Redis Cluster Cross-Slot Errors with Hash Tags without hitting unbounded tail latency, state divergence, or operational fragility.
Naive Solution
Use a single unpartitioned path with unbounded retries and synchronous global coordination.
Why It Fails
Global coordination serializes throughput under load, while unbounded retries amplify transient stalls into full outages.
Alternatives
Compare synchronous strict-consistency paths against bounded asynchronous replication, local caching, or partitioned ownership.
Engineering Decision
When a single bottleneck threatens end-to-end availability, colocate strictly atomic entity keys on one slot using {hash_tags} (e.g., {user:1042}:balance), and replace cross-entity MGET/MSET with slot-grouped parallel scatter-gather pipelines.
Trade-Offs
Gains predictable p99 latency and fault isolation in exchange for explicit bounding, observability, and slightly higher implementation complexity.
Decision Exercise · Make the Call

When Solving Redis Cluster Cross-Slot Errors with Hash Tags experiences a sudden 5x traffic spike alongside a slow downstream dependency, which engineering choice best protects system stability?

03
STAGE 03 · NAIVE SOLUTIONL5 · APPLIED DESIGN

Why coarse hash tags like {global} destroy cluster sharding.

Why this matters: The problem: see what breaks without Why coarse hash tags like {global} destroy cluster sharding under real concurrency or scale.

Think of Why coarse hash tags like {global} destroy cluster sharding as the intuition layer for Solving Redis Cluster Cross-Slot Errors with Hash Tags: The problem: see what breaks without Why coarse hash tags like {global} destroy cluster sharding under real concurrency or scale.

Wrapping a static prefix in braces—such as {payments}:user:1042:balance—forces Redis Cluster to hash only "payments" for every key in the system. All keys land on a single hash slot on Primary 1, turning a 3-shard cluster into a single-node bottleneck.

Design Trade-Off

Why coarse hash tags like {global} destroy cluster sharding

Before · Fixed TTL + Direct DB
  • Synchronized cache avalanche
  • 1,000 duplicate DB queries
  • Big-key event-loop freeze
After · Jittered TTL + Singleflight
  • 1 origin fetch per expired key
  • Randomized TTL spreads expiry
  • O(1) HashMap + DLL eviction
04
STAGE 04 · ENTERPRISE SOLUTIONL6 · SCALE & TRADE-OFFS

Enterprise solution: Fine-Grained {entity} Hash Tags + Scatter-Gather Pipelines.

Why this matters: Operational control: configure bounds, failure handling, and safe production defaults.

Solve both atomic per-entity scripts and cross-entity batches cleanly:

  • Entity-Scoped {hash_tags}: Name keys {user:1042}:balance, {user:1042}:ledger, and {user:1042}:nonce. Redis hashes only user:1042, colocating all three keys on one slot for single-RTT EVAL atomicity while spreading millions of users uniformly across all 16,384 slots.
  • Client-Side Slot Scatter-Gather for MGET/MSET: For 50 unrelated users, compute CRC16(key) & 16383 in the client, group keys by owning primary node (3 groups), and dispatch 3 parallel pipelines in ~0.9 ms (< 2 ms p99).
16,384-Slot Hash Partitioning

Fine-Grained {user:1042} Colocation Across 3 Primaries

1. Deterministic Hash-Slot Routing (16,384 Slots)
Key: {user:1042}CRC16({tag}) & 16383P10–4095Replica 0P24096–8191Replica 1P38192–12287Replica 2R1..312288–16383Replica 316,384 fixed slots partitioned across primaries
2. Hash-Tag Co-Location & Failover Mechanics
✓ Single-Slot Atomic Lua
Use hash tags {u:8841}:tokens and {u:8841}:ts so both keys map to the exact same slot for EVALSHA.
↻ Explicit Slot Migration (Not Ring)
Scaling moves discrete slots (0–16383) between primaries; clients cache the slot map and follow MOVED/ASK redirects.
! Primary Failover Window
During 10–30 s replica promotion, gateways enforce <2 ms timeout + local fallback / fail-open.
05
STAGE 05 · SYSTEM DESIGN APPLICATIONSL7 · STAFF / PRINCIPAL

Where hash tags and scatter-gather apply in distributed caches.

Why this matters: Scope boundary: identify where this pattern fits best and where simpler or async alternatives win.

Slot-aware key design is foundational when scaling Redis Cluster:

  • Rate Limiters & Sliding Windows: Colocate {tenant:42}:tokens and {tenant:42}:ts for atomic Lua token-bucket updates.
  • Payment Ledgers & Nonce Guards: Keep balance, ledger, and idempotency nonce on one slot.
  • Batch Profile Hydration: Use slot-grouped parallel pipelines instead of naive MGET.
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

What happens if you name keys `{global}:user:1042:balance` and `{global}:user:9999:balance` in Redis Cluster?

Recommended Learning Flow