Redis "READONLY You can't write against a read only replica" in CI
The connection landed on a replica, and replicas reject writes by default (replica-read-only yes). Redis returns "READONLY You can't write against a read only replica." for SET and other write commands. In CI this usually means the test connected to a replica node or a Sentinel-managed pool pointed at one.
What this error means
Reads work but any write fails with "READONLY You can't write against a read only replica." during tests that use a replicated Redis or a Sentinel setup.
(error) READONLY You can't write against a read only replica.
# python:
redis.exceptions.ReadOnlyError: You can't write against a read only replica.Common causes
The client connected to a replica, not the primary
A replicated or Sentinel topology routed the connection to a read-only replica, so writes are rejected while reads succeed.
A single node was misconfigured as a replica
The service was started with replicaof/slaveof, leaving it read-only even though tests expect a writable primary.
How to fix it
Write to the primary
- For a simple test, run a standalone Redis with no replica role.
- For Sentinel, resolve the master via the client and send writes there.
- Verify with a test write against the resolved primary.
# standalone, writable primary for tests
services:
redis:
image: redis:7
ports: ['6379:6379'] # no replicaof, writes allowedRoute writes through the Sentinel master
Ask Sentinel for the current master and open a write connection to it, rather than a fixed replica address.
from redis.sentinel import Sentinel
s = Sentinel([("localhost", 26379)])
master = s.master_for("mymaster")
master.set("k", "v")How to prevent it
- Use a standalone writable Redis for tests unless replication is under test.
- Resolve the master through Sentinel before writing.
- Do not start the test node with replicaof/slaveof.