Redis Cluster - Part 3

Using Redis-CLI with a Redis Cluster

When you use redis-cli to connect to a shard of a Redis Cluster, you are connected to that shard only, and cannot access data from other shards. If you try to access keys from the wrong shard, you will get a MOVED error.

Let's say we have a key "a" on one of the redis cluster nodes

redis-cli -p 7002
127.0.0.1:7002> keys *
1) "a"

And we connect to some other redis cluster nodes, and try to access that key

redis-cli -p 7000
127.0.0.1:7000> keys *
(empty array)
127.0.0.1:7000>
127.0.0.1:7000> get a
(error) MOVED 15495 127.0.0.1:7002

There is a trick you can use with redis-cli so you don’t have to open connections to all the shards, but instead you let it do the connect and reconnect work for you. It’s the redis-cli cluster support mode, triggered by the -c switch:

redis-cli -c -p 7000
127.0.0.1:7000> keys *
(empty array)
127.0.0.1:7000> get a
-> Redirected to slot [15495] located at 127.0.0.1:7002
"b"
127.0.0.1:7002>

Hash Slot Map Diagram

Examples of clients that support Redis cluster:

  • Java: Jedis, Lettuce

Some references:

Redis cluster specification | Redis

Redis Cluster: Architecture, Replication, Sharding and Failover

Creating a Redis Cluster

If you liked this blog, you can follow me on twitter, and learn something new with me.