Learning center

Redis READONLY errors after a failover: which node is the client on?

After a failover, the READONLY error means the client sent a write to a node that is now a replica. Ask that node for its ROLE, then compare the address the client is connected to with the address the primary endpoint resolves to now. If they differ, the client is holding an old connection, caching an old address or configured with a node endpoint, and the cache itself has already recovered.

The symptom#

Writes fail with READONLY You can't write against a read only replica. while reads from the same client succeed. Redis rejects every write command on a replica because replica-read-only is on by default. Redis source, server.c · Read-only replica

The errors begin at a failover: a primary was replaced or a replica promoted, and the old primary came back as a replica. The new primary accepts writes from clients that found it. Clients that did not are still talking to the node that was demoted.

This guide is one of a set on runtime dependency failures.

Three ways a client stays on the old primary#

  • Open connections survive. A DNS change affects only new lookups. A pool that opened its connections to the old primary keeps using them after that node becomes a replica, and nothing closes them unless the client reacts to the error or the node drops the connection.
  • The client cached the old address. The runtime, the client library or a local resolver keeps the address it resolved before the failover and reconnects to it. Client caches old address
  • The configuration names a node instead of the primary. On ElastiCache with cluster mode disabled, the primary endpoint is a DNS name that always resolves to the primary node, and it follows a promotion. Node endpoints resolve to specific nodes. An application configured with the endpoint of node 001 keeps writing to node 001 whatever its role. ElastiCache endpoints

ElastiCache propagates the promoted replica's DNS name to the primary endpoint during a Multi-AZ failover, so applications that write to the primary endpoint need no change; the reader endpoint is updated to point at replicas. Multi-AZ failover

Ask the nodes and the resolver#

ROLE returns master or slave as its first element; a replica also returns its primary's address and its replication state. INFO replication adds role, master_host and master_link_status. Both are read-only. ROLE · INFO replication fields

redis-cli -h NODE_ADDRESS -p 6379 ROLE
redis-cli -h NODE_ADDRESS -p 6379 INFO replication

List the replication group's nodes, their current roles and the primary endpoint. CurrentRole is reported per member for cluster mode disabled. describe-replication-groups

aws elasticache describe-replication-groups \
  --profile PROFILE --region REGION --replication-group-id REPLICATION_GROUP_ID \
  --query 'ReplicationGroups[0].NodeGroups[].{primary:PrimaryEndpoint.Address,members:NodeGroupMembers[].{node:CacheClusterId,role:CurrentRole,address:ReadEndpoint.Address}}' \
  --output json --no-cli-pager

aws elasticache describe-events \
  --profile PROFILE --region REGION \
  --source-type replication-group --source-identifier REPLICATION_GROUP_ID \
  --duration 360 --no-cli-pager

describe-events returns the group's events for the given number of minutes, including the failover and its time. describe-events

Then compare, from inside the client's container or host, what the endpoint resolves to and where the open connections go:

getent hosts PRIMARY_ENDPOINT
dig +short PRIMARY_ENDPOINT
ss -tn state established '( dport = :6379 )'

getent resolves through the same system resolver the application uses, while dig asks the DNS server directly, so a difference between them points at a local cache. Neither shows a cache inside the application runtime; the remote addresses in ss show where the process is actually connected. getent(1) · ss(8)

From the server side, CLIENT LIST on the demoted node shows the addr of every client still connected to it. It is O(N) in connections, so run it once. CLIENT LIST

Decision table#

Configured hostEndpoint resolves toOpen connections go toPoints to
Node endpointthat nodethat node, now a replicaConfiguration names a node
Primary endpointnew primaryold primaryPool kept connections across the failover
Primary endpointold primary, in getent onlyold primaryLocal or runtime DNS cache
Primary endpointnew primarynew primaryErrors came from before reconnect; check timestamps

An illustrative failover with two outcomes#

This example is illustrative. The payments service writes idempotency keys to an ElastiCache replication group payments-cache in account 111122223333. At 03:14 UTC a failover promotes payments-cache-002 (10.0.8.42) and payments-cache-001 (10.0.8.17) returns as a replica. From 03:15, some payments tasks log READONLY errors on every write.

In the first outcome, the failing tasks' configuration sets REDIS_HOST=payments-cache-001.cache.example.internal, standing in for the node endpoint someone copied from the console during setup. ROLE on that host returns slave with master 10.0.8.42. Every new connection from those tasks reaches the replica. The fix is the primary endpoint in configuration, and the cache needs nothing.

In the second outcome, every task uses the primary endpoint, and both getent and dig return 10.0.8.42 inside a failing task. ss in the same task shows eight established connections to 10.0.8.17, opened at 01:02. The client library keeps healthy-looking connections in its pool and does not close them on READONLY. Tasks started after 03:14 write normally. Recycling the failing tasks clears the errors. The lasting fix is a client that drops a connection when it returns READONLY and opens a new one through the primary endpoint.

Limits#

The CurrentRole field and the primary endpoint apply to cluster mode disabled. With cluster mode enabled, clients connect through the configuration endpoint and discover each shard's primary themselves. ElastiCache endpoints

A single-node cluster has no primary endpoint and no replica to promote. When its node is replaced, the new primary starts empty unless restored from a snapshot. That shows up as missing data, with no READONLY error. Multi-AZ failover

Redis replicates asynchronously by default, so writes the old primary accepted but had not yet replicated can be missing on the new primary, and that loss raises no READONLY error. Replication If the incident also involves missing keys or connection refusals, those are separate questions. Max clients or timeouts · Connection refused or timeout

Questions#

Why do only some instances get READONLY after a Redis failover?

Instances that opened connections or resolved the address after the promotion reach the new primary. Instances still holding connections opened before it, or caching the old address, keep writing to the demoted node. Compare the remote address of each instance's open connections.

Does the ElastiCache primary endpoint change during failover?

The name stays the same; ElastiCache points it at the promoted node. Applications that use it get the new primary on their next lookup, but connections already open stay where they were.

What does ROLE return on a replica?

The string slave for backward compatibility, followed by the primary's address and port, the replication state and the replication offset received so far.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading