0

I wonder read key in Redis Cluster mode.

my redis cluster info:

127.0.0.1:7001> cluster nodes
2f255a65c7238134a5b2b84671f1d1af01778178 127.0.0.1:7004@17004 slave 4c1fe92725284ae39f9b538674d9f13ae6d75d66 0 1519807517000 5 connected
cf434afde5f61917f46af8982f6f1e72bedabc9e 127.0.0.1:7001@17001 myself,master - 0 1519807516000 2 connected 5461-10922
4c1fe92725284ae39f9b538674d9f13ae6d75d66 127.0.0.1:7002@17002 master - 0 1519807517765 3 connected 10923-16383
281be7a01b553b0600789ec08e30c38949bfdd59 127.0.0.1:7005@17005 slave e2ca464b949bddc0e5414345ad6f238449be8e94 0 1519807516756 6 connected
e2ca464b949bddc0e5414345ad6f238449be8e94 127.0.0.1:7000@17000 master - 0 1519807517563 1 connected 0-5460
d86241a9eb69e8055de05dd95d54b348a836cbda 127.0.0.1:7003@17003 slave cf434afde5f61917f46af8982f6f1e72bedabc9e 0 1519807517000 4 connected

and I wondering this!

bash
❯ redis-cli -c -p 7003
127.0.0.1:7003> keys *
1) "key1"
127.0.0.1:7003> get key1
-> Redirected to slot [9189] located at 127.0.0.1:7001
"val1"
127.0.0.1:7001>

Slave have same key with master and
Master have same key.


but Why.. Why get key command act to redirect to master???
why?
I don't understand Redis Cluster ...

Prabhakar
  • 1,138
  • 2
  • 14
  • 30
b_dobee
  • 19
  • 1
  • 9

1 Answers1

4

By default, slave redirects the client to its master, since data on slave might be stale, i.e. the write to master might NOT be synced to slave.

However, sometimes we don't care about the staleness, and want to scale read operations by reading (possible stale) data from slave.

In order to achieve that, you can send the READONLY command to slave. Then any ready-only operations on that connection will be served by the slave, and won't be redirected to its master.

If you want to turn off the READONLY mode, you can send the READWRITE command to tell the slave to redirect read requests to master.

NOTE:

no matter whether the slave is in READONLY or READWRITE mode, you CANNOT write to slave, i.e. write operations are always redirected to master.

UPDATE: slave-serve-stale-data and slave-read-only configurations have nothing to do with the READONLY and READWRITE commands.

slave-serve-stale-data controls whether the slave should redirect the request to master or just returns an error, when it loses connection with master, or the replication is still in progress.

slave-read-only controls whether you can write to slave. However, these writes won't be propagated to master and other slaves, and will be removed after resync with the master.

for_stack
  • 21,012
  • 4
  • 35
  • 48
  • but I config set Redis Cluster like this, `slave-serve-stale-data yes` and `slave-read-only yes` so, Redis Cluster have same key in master and slave but when slave data is stale, then slave redirects the client to its master? am I understand...? – b_dobee Mar 02 '18 at 02:15
  • These two configs have nothing to do with the `READONLY` and `READWRITE` commands. – for_stack Mar 02 '18 at 02:20
  • Thank u for your details, So slave don not redirects the client to its master except data stale? only slave redirects the client to its master since data might be stale? how do slave know own data is stale? – b_dobee Mar 02 '18 at 04:10
  • NO. Whether it redirects the request or not, only depends on whether you've already sent `READONLY` command on the corresponding connection. Slave doesn't know whether the data is stale. – for_stack Mar 02 '18 at 04:40