Read directly from a replica inside Redis cluster - redis

I want to read directly from a replica inside a Redis cluster but I am getting redirected even if the key actually exist inside the Redis node.
Checked READONLY command docs. However, that is seemed to be ignored in case of Redis Cluster.
How should I configure the Redis cluster so that I can directly read from a replica node for the reads issue against its hash slots? I am OK to read stale data in this case.

The READONLY command (as well as the READWRITE command that counteracts it) are applicable per connection in a clustered environment.
While READWRITE is the default behavior, in order to change that you'll need to call READONLY in the context of the connection that you're using.

Related

How to configure Redis clients when connecting to master-replica setup?

I have a Redis setup with 1 master and 2 replicas - so totally 3 nodes.
Given that writes can happen only via master node while reads can happen via all 3 nodes, how do I configure the clients?
Can I pass all nodes IP in the IP list and the client will take care of connecting to the right node by itself?
Or do the clients need to be configured separately for reads and writes?
It depends on the specific client you are using; some clients automatically split read-only / write commands based on the client-connection configuration, while others allow to specify the preferred replication target at the command or invocation level.
For example, ioredis automatically handles that through the scaleReads configuration option, while StackExchange.Redis allows to handle that through the CommandFlags enum at the command invocation level: you should really check the documentation of the specific Redis client you want to use.
Finally, redis-cli does not split read-only / write commands; it can connect (via the -c option) to a Redis Cluster and follow slot redirections automatically - but the connection will always happen against a master node, with no read/write splitting.

Using AWS Elasticache Config Endpoint returns 'READONLY You can't write against a read-only replica'

Disclaimer, I'm new to redis and elasticache.
Referencing an answer in this stackoverflow here.
I have a basic AWS ElastiCache Redis cluster setup:
3 shards, 9 nodes
encryption at rest
However, when I try to connect to the Configuration endpoint I get READONLY You can't write against a read-only replica'.
If I change my connection string to a node endpoint, I connect successfully.
What am I missing here? Why isn't the configuration endpoint navigating me to a non READONLY node?
make sure you are copying the primary end point, replica is only read, Since Redis 2.6, replicas support a read-only mode that is enabled by default. This behavior is controlled by the replica-read-only option in the redis.conf file, and can be enabled and disabled at runtime using CONFIG SET.

Redis Cluster default behaviour on read operations

In a redis cluster what is the default behaviour for the read operations that happen? Does a client read from the master? I know that a client writes/updates/deletes to the master but what about read operations? If the default behaviour is to read from the master node how can I configure it to read from the slave nodes instead?
It all depends on Redis client library. For Jedis/Lettuce, all operations(CRUD) will be send to corresponding master node, and slaves are only used for failover.
If you wan to implement READONLY slave, you need some customization on Redis client.
The default behavior of replica nodes in cluster-mode enabled clusters
is to redirect all client read/write requests to an authoritative
master node of the shard that belongs to the key's hash slot. The
replica node serves the read request only if that shard belongs to the
hash slot and a readonly command was initiated by the client. This
means that the replica node processes the request only if readonly is
issued by the client before the request. Otherwise, the request is
redirected to the primary node of the shard that the hash slot belongs
to.
https://aws.amazon.com/premiumsupport/knowledge-center/elasticache-redis-client-readonly/

How to disable a redis instance or make it readonly?

I have configured Redis in non-cluster mode, and for specific hours of the day want to disable it or make it readonly (any one would do). Found that in cluster mode, READWRITE command can be used to disable queries to slave node if earlier that was enabled using READONLY. Tried CLIENT PAUSE command but that didn't work, seems even that is meant for cluster mode.

Does setting "slave-read-only no" will make slave confirm every hash lookup with the master?

I want to configure slave to enable writes (slave-read-only no). The use case is to enable ephemeral cache.
However, this paragraph in the documentation made me concerned:
Normally slave nodes will redirect clients to the authoritative master for the hash slot involved in a given command, however clients can use slaves in order to scale reads using the READONLY command.
– http://redis.io/commands/readonly
Does setting slave-read-only no will make slave confirm every hash lookup with the master?
Please take note that slave-read-only config refers to replication and READONLY refers to the redis-cluster command.
If you are not using redis-cluster, you can safely ignore the READONLY command documentation. Refer to https://raw.githubusercontent.com/antirez/redis/2.8/redis.conf instead. Writes should not replicate nor require lookups to the master. My wireshark dumps on redis with slave-read-only no shows no indication of any communication with master as a consequence of writes to the slave itself.
If you are using redis-cluster on the other hand, and referring to the READWRITE behavior: Cluster nodes' communication with each other for hash slot updates and other cluster specific messages are optimized to use minimal bandwidth and the least processing time. Communicating hash slot updates most likely do not happen for every write on the slave.