Databases 10 min read

Understanding Redis Cluster Partitioning Implementation

This article explains how Redis Cluster automatically distributes data across nodes, how clients locate the correct node using slot hashing and hash tags, and how resharding and slot migration are performed without downtime, providing a detailed overview of the partitioning mechanism.

Architect
Architect
Architect
Understanding Redis Cluster Partitioning Implementation

Redis Cluster is a distributed system composed of multiple Redis instances that together serve a single logical data set, making the location of any particular key transparent to the user.

The cluster uses 16,384 virtual slots; each master node owns a subset of these slots, and the mapping from slots to nodes is stored in a compact bit‑sequence and an array for fast lookup.

Each master maintains a bit‑sequence of 16384/8 bytes where each bit indicates ownership of a slot. For example, a bit set at position 1 means the master owns slot 1.

Key distribution relies on a hash function: HASH_SLOT = CRC16(key) mod 16384 , which in Redis source is expressed as crc16(key) & 0x3FFF . This maps any key deterministically to a slot.

Redis also supports hash tags, allowing users to force multiple keys onto the same slot by enclosing a common substring in curly braces, e.g., abc{userId}def and ghi{userId}jkl . The cluster hashes only the substring inside the braces.

When a client contacts a node that does not own the target slot, the node replies with a redirection message such as -MOVED 254 127.0.0.1:6381 , indicating the correct master for that slot. Clients are encouraged to cache these mappings to reduce round‑trips.

Resharding changes the slot‑to‑node mapping without moving the key‑to‑slot mapping. Slots to be moved are marked as MIGRATING on the source master and IMPORTING on the destination master. During this state, clients may receive ASK or TRYAGAIN responses, guiding them to the new node.

Actual key migration uses the MIGRATE command, which performs a three‑step process: DUMP the key, RESTORE it on the target node, and DEL it from the source. After all keys are moved, the slot states return to normal.

The article concludes that understanding these three aspects—automatic data distribution, client redirection, and key‑space migration—provides a solid grasp of Redis Cluster’s partitioning capabilities.

RedisclusterpartitioningSlotsReshardingKey Hashing
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.