0
0
RedisHow-ToIntermediate · 4 min read

How to Scale Redis Horizontally: Simple Guide

To scale Redis horizontally, use Redis Cluster which shards data across multiple nodes automatically. This spreads the load and increases capacity by distributing keys among nodes.
📐

Syntax

Redis horizontal scaling mainly uses Redis Cluster. The key commands involve starting multiple Redis nodes and connecting them into a cluster.

  • redis-server --cluster-enabled yes: Starts a Redis node with clustering enabled.
  • redis-cli --cluster create [node1:port] [node2:port] ... --cluster-replicas 1: Creates a cluster from nodes with replicas.
  • redis-cli -c: Connects to the cluster-aware client.

Each node holds a subset of the keyspace, called hash slots (0-16383).

bash
redis-server --cluster-enabled yes --port 7000
redis-server --cluster-enabled yes --port 7001
redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 --cluster-replicas 0
redis-cli -c -p 7000
💻

Example

This example shows how to create a simple Redis cluster with two nodes on ports 7000 and 7001, then set and get keys distributed across the cluster.

bash
redis-server --cluster-enabled yes --port 7000 --daemonize yes
redis-server --cluster-enabled yes --port 7001 --daemonize yes
redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 --cluster-replicas 0
redis-cli -c -p 7000 SET user:1 "Alice"
redis-cli -c -p 7001 SET user:2 "Bob"
redis-cli -c -p 7000 GET user:1
redis-cli -c -p 7001 GET user:2
Output
OK OK OK "Alice" "Bob"
⚠️

Common Pitfalls

Common mistakes when scaling Redis horizontally include:

  • Not enabling clustering on all nodes, causing errors.
  • Using a non-cluster-aware client, which can't route commands correctly.
  • Ignoring data rebalancing when adding or removing nodes.
  • Setting --cluster-replicas incorrectly, risking data loss.

Always use redis-cli -c or a cluster-aware client library to interact with the cluster.

bash
Wrong:
redis-cli SET key1 value1

Right:
redis-cli -c SET key1 value1
📊

Quick Reference

CommandDescription
redis-server --cluster-enabled yes --port Start Redis node with clustering
redis-cli --cluster create ... --cluster-replicas Create cluster with replicas
redis-cli -c -p Connect with cluster-aware client
CLUSTER INFOShow cluster status
CLUSTER NODESList cluster nodes and slots

Key Takeaways

Use Redis Cluster to shard data across multiple nodes for horizontal scaling.
Enable clustering on all Redis nodes and connect them properly.
Always use a cluster-aware client to interact with the cluster.
Plan replicas carefully to ensure data safety and availability.
Monitor cluster status and rebalance slots when scaling nodes.