Skip to content

Redis - Partitioning

Partitioning is the strategy of splitting your data across multiple Redis instances. This allows you to overcome the memory limitations of a single server and scale computational power and network throughput. The primary and recommended way to achieve this in modern Redis is by using Redis Cluster.

  • Larger Datasets: Scale your database beyond the RAM capacity of a single machine by combining the memory of multiple nodes.
  • Higher Throughput: Scale read/write operations by distributing the load across multiple CPUs, cores, and network interfaces.
  • High Availability: When combined with replication (as in Redis Cluster), partitioning provides fault tolerance. If one node fails, others can continue to serve traffic.

Since Redis 3.0, Redis Cluster is the official, built-in solution for sharding and high availability. It automates most of the complexities of partitioning.

  • Hash Slots: The entire keyspace is divided into 16,384 hash slots. Redis determines which slot a key belongs to by hashing the key (using CRC16) and performing a modulo operation: HASH_SLOT = CRC16(key) mod 16384.
  • Node Distribution: Each primary (master) node in the cluster is responsible for a subset of these hash slots. For example, in a 3-node cluster, Node A might hold slots 0-5500, Node B 5501-11000, and Node C 11001-16383.
  • Client Redirection: Smart clients (which are most modern Redis clients) can fetch the cluster’s slot map. When a command is sent, the client calculates the key’s hash slot and sends the command directly to the correct node, minimizing latency.
  • Rebalancing: Redis Cluster supports adding or removing nodes to the cluster. When this happens, it can automatically and transparently migrate hash slots (and the data within them) between nodes with minimal downtime.
  • Multi-key Operations: Operations involving multiple keys (like SUNION or transactions) are only supported if all keys belong to the same hash slot. To enforce this, you can use a feature called ‘hash tags’. By placing a portion of a key in curly braces {}, you ensure that only that portion is hashed. For example, user:{123}:profile and user:{123}:orders will map to the same slot.
  • Management Overhead: While Redis Cluster automates much, self-hosting a cluster still requires configuration, monitoring, and maintenance of multiple instances.
  • Data Handling: Backing up a cluster involves creating RDB/AOF snapshots on each individual node. Restoration requires careful coordination.

While Redis Cluster is the standard, it’s useful to understand other conceptual approaches, which are often managed by client libraries or proxy services.

This involves mapping ranges of keys to specific instances. For example, user IDs 1-10000 go to Node R0, 10001-20000 go to R1. This is simple to understand but can lead to uneven data distribution (‘hot spots’) and is difficult to rebalance.

In this model, the client application (or a library it uses) is responsible for hashing the key and deciding which Redis node to send the request to. This is less robust than Redis Cluster as it requires all clients to share the same hashing algorithm and cluster map, and rebalancing is a manual, complex process.

For many production applications, the most practical approach is to use a managed Redis service from a cloud provider (e.g., AWS ElastiCache, Google Cloud Memorystore, Azure Cache for Redis). These services handle the setup, management, scaling, and backups of a Redis cluster, allowing development teams to focus on their application logic.