Scale with Redis Cluster
- Shard placement, direct client routing and recovery when one primary fails.
Select a component to trace its direct connections. Drag to pan · Ctrl + wheel or pinch to zoom.
- The client hashes a key into one of 16,384 slots. Each shard has a primary that serves its slots and a replica that copies that primary.
- In this layout, cart:{17}:items and cart:{17}:revision share slot 12304 on C. Grouping them allows an atomic multi-key operation and concentrates their load on C.
- If C fails, its eligible replica can win a vote from the surviving primaries and take C’s slots. Clients refresh their slot maps and reconnect to the new owner.
Reading
- The 16,384 slots divide key ownership among primaries. Each replica follows a single shard.
- Hash tags place related keys together. A busy tag concentrates its traffic on one shard even when the other shards have spare capacity.
Scale with Redis Cluster
Scale with Redis Cluster
Redis Open Source 7.2 architecture
Route keys from the client
- The three-primary example assigns contiguous slot ranges. Redis Cluster supports different assignments and ownership changes.
- Run
node redis-slots.cjs. Bothcart:{17}:itemsandcart:{17}:revisionmap to slot 12304, owned by C. The item key tagged {18} maps to 511 on A. - The hash uses the bytes between the first opening brace and its following closing brace when that substring is nonempty. Otherwise it hashes the entire key.
- A same-slot atomic update can touch the two {17} keys. A multi-key command spanning slots can receive CROSSSLOT even when one primary happens to own both slots.
Calculate three keys’ slots offlinejavascript
'use strict';
// Run: node redis-slots.cjs
// This offline example hashes UTF-8 keys using Redis Cluster's CRC16/XMODEM.
function slot(key) {
const bytes = Buffer.from(key, 'utf8');
const open = bytes.indexOf(0x7b);
const close = open < 0 ? -1 : bytes.indexOf(0x7d, open + 1);
const input = close > open + 1 ? bytes.subarray(open + 1, close) : bytes;
let crc = 0;
for (const byte of input) {
crc ^= byte << 8;
for (let bit = 0; bit < 8; bit++) {
crc = (crc & 0x8000) ? ((crc << 1) ^ 0x1021) & 0xffff : (crc << 1) & 0xffff;
}
}
return crc % 16384;
}
function owner(number) {
if (number < 5461) return 'A';
if (number < 10923) return 'B';
return 'C';
}
if (require.main === module) {
for (const key of ['cart:{17}:items', 'cart:{17}:revision', 'cart:{18}:items']) {
console.log(key, slot(key), owner(slot(key)));
}
}
module.exports = {slot, owner};
Separate redirect handling from election
| Condition | Client action | Topology consequence |
|---|---|---|
| MOVED for a slot | Retry at the indicated owner and refresh slot topology | Future requests should follow the refreshed mapping. |
| ASK during classic slot migration | Retry that command at the target, preceded by ASKING on the same connection | Keep the existing slot map for later requests until ownership changes. |
| C fails with A and B reachable | Reconnect and learn the new owner after an eligible replica wins election | The promoted replica takes C’s slots. Recent unreplicated writes may be missing. |
- Cluster nodes exchange failure and ownership state over the cluster bus. A replica normally needs authorization from a majority of slot-serving primaries for automatic failover.
- With full-slot coverage required, loss of a shard without an eligible replacement can stop the cluster serving normal requests. Partial-coverage operation is a separate availability policy and still depends on majority-side reachability.
Size for the hottest owner
- If the {17} keys alone generate 12,000 operations/s, moving other slots cannot split that tag’s work. Choose a smaller co-location domain when atomic operations allow it.
- A fourth primary adds capacity to the system after slot redistribution. Test redirect handling and multi-key operations while migrating.
- Include replica memory, replication traffic and failover-zone headroom. Three shards with one replica each need roughly two dataset copies before allocator, buffering and persistence overhead.