Skip to content

Как настроить Redis Cluster

Коротко:

Redis Cluster — nativ horizontal scaling для Redis. Minimum: 3 masters + 3 replicas = 6 nodes (distributed через 16384 slots). Automated failover, client-side sharding, transparent для app (smart clients). Не для small data (<5GB) — просто Redis master+replica достаточно. Alternative: managed Redis (AWS ElastiCache, Redis Cloud, Yandex Managed Redis).

Ниже: пошаговая инструкция, рабочие примеры, типичные ошибки, FAQ.

Пошаговая настройка

  1. Install Redis 7+ на 6 серверах (или 6 containers)
  2. Config: cluster-enabled yes, cluster-node-timeout 5000, appendonly yes
  3. Start all 6 instances на разных ports (7000-7005)
  4. Create cluster: redis-cli --cluster create ip1:7000 ip2:7001 ... --cluster-replicas 1
  5. Verify: redis-cli -p 7000 cluster infocluster_state:ok
  6. Client setup: use cluster-aware clients (ioredis, lettuce, go-redis) — handle MOVED redirects
  7. Monitoring: redis-exporter + Prometheus dashboard

Рабочие примеры

СценарийКонфиг
redis.conf (per node)port 7000 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes protected-mode no # для intra-cluster (использовать firewall + requirepass) requirepass strongpass
Create cluster CLIredis-cli --cluster create \ 10.0.0.1:7000 10.0.0.2:7001 10.0.0.3:7002 \ 10.0.0.4:7003 10.0.0.5:7004 10.0.0.6:7005 \ --cluster-replicas 1 -a strongpass
ioredis cluster client (Node.js)const Redis = require('ioredis'); const cluster = new Redis.Cluster([ { host: '10.0.0.1', port: 7000 }, { host: '10.0.0.2', port: 7001 }, // only need to provide a few — rest discovered ], { redisOptions: { password: 'strongpass' } }); await cluster.set('key', 'value');
Add new master + resharding# Add new node to cluster redis-cli --cluster add-node new:7006 existing:7000 # Reshard slots to new master redis-cli --cluster reshard existing:7000
Hash tags (keep keys в одном slot)# Без hash tags — random distribution SET user:1:name alice SET user:1:email a@e.com # С hash tag — оба на одном shard (enabling transactions) SET {user:1}:name alice SET {user:1}:email a@e.com

Типичные ошибки

  • Cluster-unaware client → MOVED errors при каждом запросе. Use ioredis-cluster, go-redis, lettuce
  • Multi-key transactions (MULTI) работают только для keys в одном hash slot (use {tags})
  • Minimum 3 masters — с 1-2 masters нет quorum для leader election
  • AOF + RDB backups обязательны — cluster не panacea для data durability
  • cross-DC latency — cluster assumes <5ms между nodes. Для multi-region — separate clusters + replication

Больше по теме

Часто задаваемые вопросы

Когда нужен Redis Cluster?

Data >5 GB или >100k ops/sec. Меньше — single master + replica + Sentinel достаточно.

Cluster vs Sentinel?

Sentinel — HA (1 master + replicas, automatic failover). Cluster — scaling (sharded across masters). Разные задачи.

Managed alternatives?

AWS ElastiCache (Cluster mode), Redis Cloud (Redis Labs), Yandex Managed Redis. Обычно $50-500/мес vs $0 self-host но 20+ hours/month maintenance.

Migrate from single Redis?

Write dual (old + new), read от single → migrate reads batch → cut write. Или use Redis-shake tool для bulk copy.