By using AWS re:Post, you agree to the AWS re:Post Terms of Use

How do I resolve Redis client read requests that are redirected to the primary node of a shard instead of a replica node in ElastiCache for Redis?

3 minute read
0

I want to resolve Redis client read requests that are redirected to the primary node of a shard instead of a replica node in Amazon ElastiCache for Redis.

Short description

Any node in an ElastiCache for Redis cluster can receive queries from Redis clients. When a client sends a read and write query to a replica node, the node checks that the request is a single key or multi-key operation. The single key or multi-key operation must belong to the same hash slot of the shard. By default, replica nodes with cluster mode turned on redirect all read and write requests to the primary node. Read and write requests are redirected to an authoritative primary node of the shard that belongs to the key's hash slot. If the shard belongs to the hash slot and a readonly command is initiated first, then the replica node only completes the read request. The replica node processes the read request only if readonly is issued by the client first. If the readonly request isn't sent first, then the replica node redirects the request to a primary node.

Resolution

To make sure that your Redis client read requests are read by the replica node, complete the following steps:

  1. Log in to your cluster, and then set a key. Example:

    172.31.21.72:6379> set key8 "This is testing for readonly"
    -> Redirected to slot [13004] located at 172.31.21.72:6379
    OK
    172.31.21.72:6379>
  2. Connect to a replica node. Make sure that you issue a readonly command. Example: Note: The following example shows a readonly command that isn't sent first and the request is redirected to a primary node.

    172.31.30.175:6379> info replication
     # Replication
     role:slave
     master_host:172.31.21.72
     master_port:6379
     master_link_status:up
     172.31.30.175:637> CLUSTER KEYSLOT key8
     (integer) 13004 
     172.31.30.175:637> get key8
     (error) MOVED 13004 172.31.21.72:6379
     172.31.30.175:637> get key8
     -> Redirected to slot [13004] located at 172.31.21.72:6379
     "This is testing for readonly"
     172.31.21.72:6379>
    

    Note: The MOVED error shown in the preceding example occurs when the Redis cluster client can't handle redirection requests to the primary node. For more information about the MOVED error, see MOVED redirection on the Redis website.

    Example:
    Note: The following example shows a readonly command that's sent first. This allows the replica node to process the request and not redirect the request to a primary node.

    172.31.30.175:6379> readonly
    OK
    172.31.30.175:6379> get key8
    "This is testing for readonly"
    172.31.30.175:6379>

Note: Make sure that you issue the readonly command when the client connects to a node for the first time. The command is active only until the client reads keys from the same node. If the client connects to another replica node in the same or different shard, then make sure that a readonly command is issued.

AWS OFFICIAL
AWS OFFICIALUpdated 6 months ago