SoFunction
Updated on 2025-04-14

In-depth understanding of Redis hash slots

1. What is a Redis hash slot?

Redis Cluster is a distributed architecture of Redis that distributes data across multiple Redis instances (nodes). In order to implement data sharding, Redis Cluster uses a hash slot mechanism. The entire Redis Cluster is divided into 16384 hash slots, each of which can store several key-value pairs. Each node is responsible for managing a portion of the hash slot and its corresponding data.

1.1 Definition of hash slots

  • A total of 16384 slots: Redis Cluster divides all data into 16384 slots (numbers 0 to 16383).
  • Key mapping: Each key is mapped into a hash slot through a hash function. Redis Cluster uses the CRC16 algorithm to calculate the hash value for the key, and then modulo 16384 to obtain the slot number corresponding to the key.
  • Slot-to-node mapping: Each node in the Redis Cluster is responsible for managing several hash slots. Each node will save the key-value pair data of a portion of the slot. When the amount of data in the cluster increases, the slots can be reassigned by adding nodes to achieve horizontal scaling of the cluster.

2. How hash slots work

Redis Cluster realizes distributed storage and load balancing of data through a hash slot mechanism. Here is how hash slots work:

2.1 Key to slot mapping

When there is a new key-value pair in the Redis Cluster that needs to be stored, the cluster first calculates the hash value of the key and determines which hash slot it belongs to based on the hash value. For example, for keymykey

  • Redis Cluster ComputingmykeyCRC16 hash value.
  • Modify the hash value pair 16384 to get the hash slot number.
  • Based on the slot number, Redis Cluster determines the node where the key-value pair is stored.

2.2 Slot to node mapping

Each node in the Redis Cluster is responsible for managing several hash slots. When the client requests a key, the cluster will route the request to the corresponding node based on the key's hash slot number. If the hash slot of the key is not on the requesting node, the node will returnMOVEDResponse and inform the client of the correct target node. Client-basedMOVEDResponse to the resend request to the target node.

2.3 Node expansion and reduction

When you need to add a new node to the Redis Cluster or delete an existing node, Redis Cluster redistributes the slots. By redistributing slots, the cluster can dynamically adjust the data distribution and load while maintaining the data evenly distributed.

3. Use Redis Cluster and hash slots in Java

In Java, you can use Redis client libraries such as Jedis or Redisson to interact with Redis Cluster. These client libraries support Redis Cluster's hash slot mechanism, which automatically handles node routing and redirection.

3.1 Introducing Jedis dependencies

On the Maven projectAdd Jedis dependencies to the file:

<dependency>
    <groupId></groupId>
    <artifactId>jedis</artifactId>
    <version>4.0.0</version>
</dependency>

3.2 Interact with Redis Cluster using JedisCluster

JedisClusteris a class provided by Jedis for interaction with Redis Cluster. It can automatically handle hash slot calculations and node routing.

import ;
import ;

import ;
import ;

public class RedisHashSlotExample {
    public static void main(String[] args) {
        // Define Redis Cluster node        Set&lt;HostAndPort&gt; clusterNodes = new HashSet&lt;&gt;();
        (new HostAndPort("127.0.0.1", 7000));
        (new HostAndPort("127.0.0.1", 7001));
        (new HostAndPort("127.0.0.1", 7002));
        
        // Create JedisCluster object        try (JedisCluster jedisCluster = new JedisCluster(clusterNodes)) {
            // Insert data, JedisCluster automatically handles hash slot calculations and node routing            for (int i = 0; i &lt; 10; i++) {
                String key = "mykey" + i;
                (key, "value" + i);
                (key + ": " + (key));
            }

            // Handle data distribution and redirection            String redirectedKey = "mykey11";
            (redirectedKey, "value11");
            (redirectedKey + ": " + (redirectedKey));
        } catch (Exception e) {
            ();
        }
    }
}

In this example, we useJedisClusterConnect to Redis Cluster. JedisCluster calculates the hash slot based on the hash value of the key and routes the request to the correct node. Clients do not need to manually handle hash slot calculations and node routing.

4. Application scenarios of Redis hash slots

The hash slot mechanism has the following important application scenarios in Redis Cluster:

4.1 Data distribution and load balancing

The hash slot mechanism enables Redis Cluster to evenly distribute data across multiple nodes, enabling load balancing. By adding or deleting nodes, Redis Cluster can dynamically adjust slot allocation to ensure even data and load distribution.

4.2 High availability and data redundancy

Redis Cluster enables high availability and redundancy of data by assigning slots to multiple master nodes and configuring one or more slave nodes for each master node. When a master node fails, Redis Cluster can automatically promote the slave node of the corresponding slot to the master node and continue to provide services.

4.3 Data expansion and reduction

In Redis Cluster, adding or deleting nodes only requires adjusting the allocation of slots without modifying the client. The hash slot mechanism allows the cluster to scale and reduce smoothly without affecting data access and operation.

5. Advanced features of Redis hash slots

5.1 Hash tags

In some cases, developers want to map multiple keys into the same hash slot, for example, when multiple keys need to be batched. Redis Cluster supports the hash tag mechanism, which is used in keys.{}Tag the part to ensure that the keys of the same label are mapped to the same hash slot.

For example, the following keys will be mapped to the same hash slot:

{user:1000}:name
{user:1000}:age
{user:1000}:address

5.2 Manual slot migration

During cluster maintenance, it may be necessary to manually migrate the slots. Redis providesCLUSTERCommand, you can manually migrate slots from one node to another. Jedis and other client libraries also support the execution of slot migration commands.

6. Advantages and limitations of Redis hash slots

6.1 Advantages

  • Distributed Storage: Through the hash slot mechanism, Redis Cluster implements distributed storage of data, which can easily scale and reduce cluster size.
  • high performance: Since the data is scattered across multiple nodes, read and write operations can be carried out in parallel, significantly improving performance.
  • High availability: Through master-slave replication and failover mechanisms, Redis Cluster can automatically recover in the event of a node failure, ensuring high data availability.

6.2 Limitations

  • Transaction support limited: Redis Cluster does not support transaction operations across nodes, because different slots may be distributed on different nodes.
  • Increased complexity: The configuration and maintenance of Redis Cluster are more complex than the single-node mode, and developers need to consider issues such as slot allocation, migration and node failure recovery.
  • Data consistency issues: In some scenarios, there may be data inconsistency problems, especially during data synchronization between nodes and master-slave switching.

7. Summary

The Redis hash slot mechanism is the core technology in Redis Cluster to achieve data distribution and high availability. By allocating data to 16384 slots and mapping slots to different nodes, Redis Cluster enables distributed storage, read-write separation and automatic failover.

This is the end of this article about in-depth understanding of Redis hash slots. For more related Redis hash slot content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!