Redis Cluster Management

RedisRedisBeginner
Practice Now

Introduction

In this lab, you will learn how to manage a Redis cluster. The lab focuses on essential cluster management tasks, including initializing a cluster, adding nodes, checking cluster health, and resharding slots.

We'll start by initializing a Redis cluster using the redis-cli --cluster command, which automates the initial configuration. Then, you'll learn to add a new node using CLUSTER MEET. Next, you'll check the cluster's health with CLUSTER INFO. Finally, you'll reshard slots between nodes using CLUSTER SETSLOT to balance the cluster.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL redis(("Redis")) -.-> redis/RedisGroup(["Redis"]) redis/RedisGroup -.-> redis/setup_redis("Install Redis") redis/RedisGroup -.-> redis/launch_server("Start Redis Server") redis/RedisGroup -.-> redis/access_cli("Connect Using CLI") subgraph Lab Skills redis/setup_redis -.-> lab-552095{{"Redis Cluster Management"}} redis/launch_server -.-> lab-552095{{"Redis Cluster Management"}} redis/access_cli -.-> lab-552095{{"Redis Cluster Management"}} end

Initialize the Redis Cluster

In this step, you will initialize a Redis cluster using the redis-cli --cluster create command. This command simplifies the process of setting up a functional Redis cluster.

What is a Redis Cluster?

A Redis cluster is a distributed setup that automatically divides data across multiple Redis nodes. This allows you to scale your Redis deployment horizontally and manage larger datasets than a single Redis instance can handle.

Why use redis-cli --cluster create?

The redis-cli --cluster create command provides a straightforward way to create, manage, and interact with a Redis cluster. It automates the initial configuration and node discovery, making cluster creation easier.

Steps:

  1. Start Redis Instances:

    First, you need to configure and start multiple Redis instances. For this lab, we'll use six instances running on ports 7000 through 7005. Each instance needs to be configured with cluster-enabled yes.

    To do this, you can use the following commands to create configuration files and start the Redis instances. Note that these commands are for demonstration purposes. The setup script has already configured and started these instances for you.

    REDIS_CONF="/etc/redis/redis.conf"
    for port in 7000 7001 7002 7003 7004 7005; do
      CONF_FILE="/etc/redis/redis-${port}.conf"
      sudo cp "$REDIS_CONF" "$CONF_FILE"
      sudo sed -i "s/^port 6379/port ${port}/g" "$CONF_FILE"
      sudo sed -i "s/^#cluster-enabled yes/cluster-enabled yes/g" "$CONF_FILE"
      sudo sed -i "s/^#cluster-config-file nodes.conf/cluster-config-file nodes-${port}.conf/g" "$CONF_FILE"
      sudo sed -i "s/^#cluster-node-timeout 15000/cluster-node-timeout 15000/g" "$CONF_FILE"
      sudo sed -i "s/^#appendonly no/appendonly yes/g" "$CONF_FILE"
      sudo redis-server "$CONF_FILE" &
    done
    sleep 5
  2. Create the Cluster:

    Now, use the redis-cli --cluster create command to create the cluster. This command requires the IP addresses and ports of the initial nodes.

    Open your terminal and execute the following command:

    redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1
    • redis-cli --cluster create: Starts the cluster creation process.
    • 127.0.0.1:7000 127.0.0.1:7001 ... 127.0.0.1:7005: The addresses and ports of the Redis instances.
    • --cluster-replicas 1: Specifies that each master node should have one replica.
  3. Confirm the Cluster Creation:

    The redis-cli tool will ask you to confirm the cluster creation. Type yes and press Enter.

    >>> Creating cluster
    >>> Performing hash slots allocation on 6 nodes...
    >>> Master[0] -> Slots 0 - 5460
    >>> Master[1] -> Slots 5461 - 10922
    >>> Master[2] -> Slots 10923 - 16383
    >>> Adding replica node to Master[0]
    >>> Adding replica node to Master[1]
    >>> Adding replica node to Master[2]
    >>> M: 49a4928719291928192819281928192819281928 127.0.0.1:7000
       slots:[0-5460] (5461 slots) master
    >>> M: 9281928192819281928192819281928192819281 127.0.0.1:7001
       slots:[5461-10922] (5462 slots) master
    >>> M: 19281928192819281928192819281928192819281 127.0.0.1:7002
       slots:[10923-16383] (5461 slots) master
    >>> S: 81928192819281928192819281928192819281928 127.0.0.1:7003
       replicates 49a4928719291928192819281928192819281928
    >>> S: 28192819281928192819281928192819281928192 127.0.0.1:7004
       replicates 9281928192819281928192819281928192819281
    >>> S: 928192819281928192819281928192819281928192 127.0.0.1:7005
       replicates 19281928192819281928192819281928192819281
    >>> Can I set the above configuration? (type 'yes' to accept): yes
    >>> Slots 0-5460 assigned to node 49a4928719291928192819281928192819281928
    >>> Slots 5461-10922 assigned to node 9281928192819281928192819281928192819281
    >>> Slots 10923-16383 assigned to node 19281928192819281928192819281928192819281
    >>> Adding replica 81928192819281928192819281928192819281928 to 49a4928719291928192819281928192819281928
    >>> Adding replica 28192819281928192819281928192819281928192 to 9281928192819281928192819281928192819281
    >>> Adding replica 928192819281928192819281928192819281928192 to 19281928192819281928192819281928192819281
    >>> [OK] All nodes agree about the cluster configuration.
    >>> >>> Check cluster info
    >>> >>> Nodes
    >>> M: 49a4928719291928192819281928192819281928 127.0.0.1:7000
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    >>> M: 9281928192819281928192819281928192819281 127.0.0.1:7001
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    >>> M: 19281928192819281928192819281928192819281 127.0.0.1:7002
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    >>> S: 81928192819281928192819281928192819281928 127.0.0.1:7003
       replicates 49a4928719291928192819281928192819281928
    >>> S: 28192819281928192819281928192819281928192 127.0.0.1:7004
       replicates 9281928192819281928192819281928192819281
    >>> S: 928192819281928192819281928192819281928192 127.0.0.1:7005
       replicates 19281928192819281928192819281928192819281
    >>> [OK] All nodes agree about the cluster configuration.
    >>> All 16384 slots covered.

    This output shows the allocation of slots to each master node and the assignment of replicas.

  4. Connect to the Cluster:

    Connect to the Redis cluster using redis-cli. This allows you to execute commands against the cluster.

    redis-cli -h 127.0.0.1 -p 7000
  5. Exit redis-cli:

    Exit the redis-cli session to ensure the command is logged.

    exit

You have now successfully initialized a Redis cluster. In the next steps, we will explore how to add nodes, check cluster health, and reshard slots.

Add a New Node to the Cluster

In this step, you will learn how to add a new node to an existing Redis cluster using the CLUSTER MEET command. This command is essential for expanding your cluster's capacity and ensuring high availability.

Understanding CLUSTER MEET

The CLUSTER MEET command introduces a new Redis node to the cluster. When a node receives this command, it attempts to connect to the specified node and start the handshake process to become part of the cluster.

Prerequisites:

  • A Redis cluster initialized as described in the previous step.
  • A new Redis instance running and configured to be part of the cluster, but not yet connected. For this lab, we'll assume you have a new instance running on port 7006. This instance should be started with the cluster-enabled yes configuration option. The setup script has already configured and started this instance for you.

Steps:

  1. Connect to an Existing Node:

    Connect to one of the existing nodes in the cluster using redis-cli. It doesn't matter which node you connect to, as the CLUSTER MEET command will propagate the information throughout the cluster.

    redis-cli -h 127.0.0.1 -p 7000
  2. Use the CLUSTER MEET Command:

    Now that you are connected to a node in the cluster, use the CLUSTER MEET command to introduce the new node (port 7006).

    CLUSTER MEET 127.0.0.1 7006
    • CLUSTER MEET: The command to add a new node.
    • 127.0.0.1: The IP address of the new node.
    • 7006: The port number of the new node.

    You should see the following output:

    OK

    This indicates that the command was successfully sent.

  3. Exit redis-cli:

    Exit the redis-cli session to ensure the command is logged.

    exit

You have successfully added a new node to the Redis cluster. In the next step, we will check the cluster's health.

Check the Cluster Health

In this step, you will learn how to check the health and status of your Redis cluster using the CLUSTER INFO command. This command provides valuable insights into the overall state of the cluster.

Understanding CLUSTER INFO

The CLUSTER INFO command returns information about the Redis cluster, which can be used to monitor its health and diagnose problems.

Prerequisites:

  • A Redis cluster initialized and with a new node added as described in the previous steps.

Steps:

  1. Connect to a Cluster Node:

    Connect to any of the nodes in the cluster using redis-cli.

    redis-cli -h 127.0.0.1 -p 7000
  2. Execute the CLUSTER INFO Command:

    Use the CLUSTER INFO command to retrieve cluster information.

    CLUSTER INFO

    You should see output similar to the following:

    cluster_state:ok
    cluster_slots_assigned:16384
    cluster_slots_ok:16384
    cluster_slots_pfail:0
    cluster_slots_fail:0
    cluster_known_nodes:7
    cluster_size:3
    cluster_current_epoch:7
    cluster_my_epoch:7
    ...
  3. Interpreting the Output:

    Here's a breakdown of some key metrics:

    • cluster_state:ok: The cluster is in a healthy state.
    • cluster_slots_assigned:16384: The number of slots assigned to nodes.
    • cluster_slots_ok:16384: The number of slots functioning correctly.
    • cluster_slots_pfail:0: The number of slots in a "potentially failing" state.
    • cluster_slots_fail:0: The number of slots in a "failing" state.
    • cluster_known_nodes:7: The total number of nodes the current node knows about.
    • cluster_size:3: The number of master nodes in the cluster.
  4. Exit redis-cli:

    Exit the redis-cli session to ensure the command is logged.

    exit

You have successfully checked the health of your Redis cluster using the CLUSTER INFO command.

Reshard Slots to the New Node

In this step, you will learn how to re-shard slots in a Redis cluster using the redis-cli --cluster reshard command. This is important for distributing data evenly, especially after adding new nodes.

Understanding Redis Slots and Resharding

A Redis cluster divides the keyspace into 16384 slots. Each master node is responsible for a subset of these slots. When you add a new node, it doesn't initially own any slots. Resharding moves slots from existing nodes to the new node, balancing the data and workload.

Steps:

  1. Connect to the Cluster using redis-cli --cluster:

    To perform the resharding operation, you'll use the redis-cli --cluster reshard command. This command provides an interactive way to redistribute slots across the cluster.

    Open your terminal and execute the following command:

    redis-cli --cluster reshard 127.0.0.1:7000

    This command connects to the Redis cluster through the node at 127.0.0.1:7000 and starts the resharding process.

  2. Specify the Number of Slots to Move:

    The redis-cli tool will prompt you to enter the number of slots you want to move. For this example, let's move 101 slots to the new node.

    How many slots do you want to move? (default: all)

    Enter 101 and press Enter.

  3. Enter the Target Node ID:

    Next, the tool will ask you to enter the node ID of the target node, which is the new node you added in the previous step (port 7006). To find the node ID, you can use the CLUSTER NODES command as shown in the previous steps, or you can use the following command to get the node ID directly:

    redis-cli -h 127.0.0.1 -p 7006 cluster nodes | grep myself | awk '{print $1}'

    Copy the node ID from the output. The redis-cli tool will prompt you:

    What is the receiving node ID?

    Paste the node ID and press Enter.

  4. Specify the Source Nodes:

    The tool will ask you to specify the source nodes from which to take the slots. You can enter all to redistribute slots from all existing master nodes.

    Please enter all the source node IDs.
      Type 'all' to use all the nodes as source nodes for the hash slots.
      Type 'done' to stop entering IDs.

    Enter all and press Enter.

  5. Confirm the Resharding Plan:

    The redis-cli tool will display the resharding plan and ask you to confirm.

    Do you want to proceed with the reshard plan? (type 'yes' to accept):

    Type yes and press Enter to start the resharding process.

  6. Wait for Resharding to Complete:

    The redis-cli tool will now move the slots from the source nodes to the target node. This process may take some time, depending on the amount of data in the cluster. You will see progress messages as the slots are being moved.

  7. Exit redis-cli:

Once the resharding is complete, exit the redis-cli session.

```redis
exit
```

You have now successfully re-sharded slots in your Redis cluster using the redis-cli --cluster reshard command. This ensures that the data is more evenly distributed across the cluster, including the new node.

Summary

In this lab, you learned how to manage a Redis cluster. You started by initializing a cluster using redis-cli --cluster create, then added a new node with CLUSTER MEET. You checked the cluster's health using CLUSTER INFO and finally re-sharded slots with redis-cli --cluster reshard to balance the cluster. These are essential tasks for managing a Redis cluster and ensuring its scalability and high availability.