database/redis

Redis Explained

Redis Cluster is a distributed implementation of Redis designed to provide horizontal scalability, high availability, and fault tolerance. It allows Redis to automatically partition data across multiple nodes, meaning that a Redis Cluster can handle much larger datasets and provide high availability and resilience

How Redis Cluster Works:

  1. Cluster Nodes:
    • Redis Cluster consists of multiple master nodes and replica nodes.
    • Each node in the cluster knows about the other nodes in the cluster, and they communicate to ensure that data is properly partitioned and replicated.
  2. Hash Slots:
    • Redis Cluster uses 16384 hash slots to partition the data.
    • Each key in Redis is assigned to one of these slots using a hash function. Each node in the cluster is responsible for a range of hash slots.
    • For example, a key like user:1000 will be mapped to a specific slot based on its name, and this slot will be served by one of the Redis nodes.
  3. Key Distribution:
    • When a Redis Cluster is created, keys are distributed across the available master nodes based on the hash slot of the key.
    • Each master node is responsible for a certain range of these 16384 slots.
    • If there are N master nodes, the slots are divided evenly among them, e.g., if there are 6 nodes, each will handle about 2730 slots.
  4. Replication and Failover:
    • For each master node, there can be one or more replica nodes (also known as slave nodes) to provide redundancy.
    • If a master node becomes unavailable (e.g., due to failure), one of its replicas is promoted to master, ensuring no data loss and maintaining availability.
  5. Cluster Topology and Communication:
    • Redis Cluster nodes exchange information about the state of each node and their slots. They use a Gossip Protocol to inform each other about the state of the cluster (e.g., who is master, who is replica, and who is unreachable).
    • Redis Cluster handles communication and coordination among nodes without any external tool (such as Redis Sentinel).

Difference Between Master and Replica Nodes in Redis

FeatureMaster NodeReplica Node (Slave)
RolePrimary node that handles both read and write operations.Secondary node that replicates data from the master.
Data StorageStores original data and accepts write operations.Maintains a copy of the master’s data (read-only by default).
Write OperationsSupports write operations (SET, INCR, DEL, etc.).Does not support direct writes (except in rare cases like when manually promoted to master).
Read OperationsHandles read operations by default.Can handle read operations (if configured), useful for read scaling.
FailoverIf a master node fails, a replica is promoted to master (in Sentinel or Cluster mode).Can be promoted to master in case of failure of the master.
ReplicationSends data updates to replica nodes.Receives data updates from the master.
ScalabilityLimited by the resources of a single node.Multiple replicas can be added to distribute read load.
PersistenceUses RDB or AOF for persistence (depending on configuration).Follows the persistence settings of the master.
Use CasePrimary database node that handles all operations.Backup node for high availability and read scaling.

Key Points:

  • Master nodes handle both reads and writes and replicate data to their replica nodes.
  • Replica nodes are mainly used for high availability and load balancing (by distributing read operations).
  • In Redis Cluster, replicas can take over as master if the original master fails.
  • In Redis Sentinel, replicas are monitored and promoted to master automatically when a master fails.

Hash Slots in Redis Cluster and How Data is Stored

What Are Hash Slots?

Redis Cluster uses hash slots as a way to distribute data across multiple nodes. Instead of assigning individual keys to specific nodes manually, Redis Cluster divides the entire keyspace into 16,384 hash slots, and each node in the cluster is responsible for a subset of these slots.

Info

A single hash slot can hold multiple keys.
Since there are only 16,384 slots, but Redis can store millions of keys, multiple keys naturally fall into the same slot.

How Data is Stored Using Hash Slots

  1. Consistent Hashing (CRC16 Algorithm):
  • When a key is stored in Redis Cluster, it is assigned to a specific hash slot using the CRC16 hashing algorithm.
  • The formula used is:
hash_slot = CRC16(key) % 16384
  • The resulting hash slot number (from 0 to 16,383) determines which node will store the key.
  1. Node Assignment:
    • Each Redis master node in the cluster is assigned a range of hash slots.
    • Example:
      • Node 1: Hash slots 0 - 5,461
      • Node 2: Hash slots 5,462 - 10,922
      • Node 3: Hash slots 10,923 - 16,383
  2. Key Distribution:
  • When a client sends a command like:
SET user:1000 "Alice"
  • The CRC16(“user:1000”) % 16384 calculation determines its hash slot.
  • Redis stores "Alice" in the node responsible for that slot.

Example of Key Storage in a Cluster

KeyCRC16 Hash ValueHash Slot (CRC16 % 16384)Node Assigned
user:145674567Node 1 (0-5461)
order:5001234512345Node 3 (10923-16383)
session:abc78907890Node 2 (5462-10922)

Hash Slot Example with 3 Nodes

If we have 3 master nodes, the slots are distributed as:

Node 1 → [ 0 - 5461 ]
Node 2 → [ 5462 - 10922 ]
Node 3 → [ 10923 - 16383 ]

If a new node is added, Redis will rebalance the slots among all nodes.

Benefits of Hash Slots

Automatic Data Distribution: Ensures keys are evenly distributed across nodes.
Scalability: Adding or removing nodes redistributes only affected hash slots.
Failover Support: If a master node fails, a replica can take over its slots.

Handling Multi-Key Operations

Redis Cluster does not support multi-key operations across multiple hash slots.
Example:

MSET key1 "value1" key2 "value2"
  • If key1 and key2 are in different slots, Redis will return a CROSSSLOT error.
  • Solution: Use hash tags, like {user}:name and {user}:age, to ensure both keys are placed in the same slot.