Why you're seeing this error
A Redis READONLY error means your application just sent a write command to a node that Redis now treats as a replica, not the primary. The exact text — READONLY You can't write against a read only replica — comes straight from Redis itself, and in 2026 it is almost always triggered by a failover: a replica got promoted to primary, the old primary stepped down, and your client's open connection hasn't caught up to that change yet.
I've debugged this exact error in client projects running Redis as a session store, a queue backend for BullMQ, and a cache in front of a payments API. It always looks alarming the first time — writes were working a second ago — but it is Redis doing exactly what it's designed to do: refusing a write that could make a replica's data diverge from the real primary.
What triggers a Redis READONLY error
Redis documents this error across every major client library, which tells you it's a protocol-level behavior, not a bug in one driver. The official Redis READONLY error reference lists the exact exception each client throws — ReadOnlyError in redis-py, ReplyError in ioredis, JedisDataException in Jedis — and all of them carry the same underlying message.
The three real-world causes I run into, in order of frequency:
- A failover just happened. Your managed Redis provider (Upstash, ElastiCache, a self-hosted Sentinel setup) promoted a replica to primary because the old primary failed a health check or went through maintenance. Your connection is still pointed at the demoted node.
- Your app was misconfigured from the start. Someone pasted a replica's hostname into
REDIS_URL instead of the primary's, and it worked fine until the first write-heavy code path ran.
- Cluster mode routing is stale. In Redis Cluster, a client can hold an outdated slot map and send a write to a node that no longer owns that slot as primary.
How to diagnose it in under five minutes
Before changing any code, confirm which node you're actually talking to. Don't guess.
redis-cli -h your-redis-host -p 6379 ROLE
A reply starting with slave confirms you're connected to a replica. Follow it with:
redis-cli -h your-redis-host -p 6379 INFO replication
Look at role:slave and master_host to find the current primary's address. If you're running Sentinel, ask Sentinel directly instead of guessing at a hostname:
redis-cli -p 26379 SENTINEL get-master-addr-by-name mymaster
This returns the IP and port of whichever node Sentinel currently believes is primary — which is the only address your write path should ever use.
The fix for ioredis (Node.js)
If you're on a single primary/replica pair without Sentinel, the fastest fix is catching the error at the connection level instead of wrapping every write in a try/catch. ioredis's reconnectOnError option takes a function that inspects the error and can tell the client to reconnect and resend the failed command automatically:
const Redis = require('ioredis');
const redis = new Redis({
host: process.env.REDIS_HOST,
port: 6379,
password: process.env.REDIS_PASSWORD,
tls: {},
reconnectOnError(err) {
if (err.message.includes('READONLY')) {
return 2; // reconnect AND automatically resend the failed command
}
return false;
},
});
Returning 2 from that callback is the part people miss — returning true only reconnects, it doesn't resend the write that failed. This pattern turns a failover into a brief, invisible reconnect instead of a 500 that reaches your users. For the full list of valid return values, check the ioredis CommonRedisOptions reference.
Add a thin application-level retry for critical writes
reconnectOnError resolves most cases, but if a retry lands a moment too early — while the failover is still actively completing — it can still fail once. For writes you genuinely can't lose (a payment status update, an idempotency key), wrap them in a small capped retry:
async function writeWithRetry(fn, attempts = 3) {
for (let i = 0; i < attempts; i++) {
try {
return await fn();
} catch (error) {
const isReadonly = error?.message?.includes('READONLY');
if (!isReadonly || i === attempts - 1) throw error;
await new Promise((r) => setTimeout(r, 100 * (i + 1)));
}
}
}
Keep this as a second layer, not your primary defense — most READONLY errors never reach it once reconnectOnError is configured correctly.
How do you fix READONLY errors with Redis Sentinel?
If you're running your own Sentinel-based high-availability setup rather than a managed service, the fix isn't a reconnect callback — it's letting a Sentinel-aware client discover the primary instead of hardcoding a hostname. Redis's own Sentinel documentation is explicit that clients need Sentinel support built in, and that the configuration should name the master set, not a fixed IP:
const redis = new Redis({
sentinels: [
{ host: 'sentinel-1', port: 26379 },
{ host: 'sentinel-2', port: 26379 },
{ host: 'sentinel-3', port: 26379 },
],
name: 'mymaster',
});
With this configuration, ioredis asks Sentinel for the current primary on every reconnect, instead of trusting a cached address. The Redis docs recommend at least three Sentinel instances across independent failure domains — don't run a single Sentinel process, because a lone Sentinel is itself a single point of failure for the exact failover detection you're relying on.
How do you fix READONLY errors in Redis Cluster?
In cluster mode, don't connect with a plain client at all. Use a cluster-aware client that tracks the current slot-to-primary mapping and routes writes accordingly:
const cluster = new Redis.Cluster([
{ host: '10.0.0.1', port: 6379 },
{ host: '10.0.0.2', port: 6379 },
{ host: '10.0.0.3', port: 6379 },
]);
await cluster.set('key', 'value');
If you still see READONLY errors after switching to a cluster client, the slot map is likely stale rather than missing entirely — that typically follows a resharding operation or a primary failing mid-script, and the fix is making sure your client version handles MOVED redirects promptly rather than silently sending the retry to the same stale node.
What NOT to do
It's tempting to make the error disappear with one command:
redis-cli CONFIG SET replica-read-only no
Don't do this to work around the error. Redis's own documentation on replication notes that writable replicas exist mostly for historical reasons, and writing directly to a replica lets its dataset silently diverge from the primary. You'll trade one visible error for a much harder-to-debug data consistency bug later, which is a worse trade in any production system — and especially bad if that Redis instance backs anything involving payment state, idempotency keys, or session data for a fintech integration.
Comparison: which fix matches your setup
| Your setup | Root cause | Fix |
|---|
| Single primary/replica, managed Redis | Stale connection after failover | reconnectOnError returning 2 |
| Self-hosted HA with Sentinel | Hardcoded hostname instead of discovery | Sentinel-aware client config |
| Redis Cluster | Stale slot map after resharding | Cluster-aware client, handle MOVED |
| Any setup | Replica hostname used by mistake | Point connection string at the primary |
A pre-deploy checklist
This is the same pattern I apply when building out queue and cache infrastructure for client apps: treat connection-level recovery as part of the base config, not an afterthought you bolt on after the first incident. The same discipline applies to database pooling — I covered a closely related failure mode in Postgres too many connections error: fix it the right way and in Prisma connection pool timeout: fix it in serverless apps, both of which come down to the same root idea — a client holding a stale view of which backend node it should be talking to.
If your production stack includes Redis behind a load balancer or orchestrator, it's also worth reviewing how your pods restart during a failover; a flapping Redis connection combined with an unhealthy pod can compound into the kind of crash loop I walked through in Kubernetes CrashLoopBackOff: how to fix it fast in 2026.
Frequently asked questions
What does "READONLY You can't write against a read only replica" mean?
It means a write command (SET, DEL, INCR, or similar) reached a Redis node that is currently a replica. Redis refuses the write to stop the replica's dataset from diverging from the real primary — it's correct, protective behavior, not a malfunction.
Why does my Redis client keep writing to the old primary after a failover?
Because the connection was established before the failover happened, and most Redis clients don't automatically re-discover the new primary just because a failover occurred elsewhere. The client's view of the topology stays stale until something — a dropped connection, a Sentinel lookup, a cluster slot refresh — forces it to reconnect.
Is a READONLY error a sign Redis is broken?
Usually not. A failover is often a normal, expected event triggered by planned maintenance or an automatic health check. The READONLY error is just the visible symptom of a client that hasn't caught up to the new topology yet. If it recurs constantly without a corresponding failover, that's worth investigating separately.
Does increasing maxRetriesPerRequest fix READONLY errors?
No — that setting controls how many times ioredis retries when the connection itself is down, which fixes a different error (MaxRetriesPerRequestError). READONLY errors need reconnectOnError or Sentinel/Cluster-aware discovery, because the connection is up, it's just pointed at the wrong node.
Should I set replica-read-only to no as a quick fix?
No. That removes Redis's protection against replica data diverging from the primary and can silently corrupt your dataset. Fix the client's node discovery instead — it's a few lines of configuration, not a structural change.
Key takeaways
- A Redis READONLY error means your client wrote to a node that is currently a replica, almost always right after a failover.
- Diagnose with
ROLE or INFO replication before changing any code — confirm which node you're actually connected to.
- For single primary/replica setups, configure ioredis's
reconnectOnError to detect READONLY and return 2 so it reconnects and resends the write.
- For high-availability setups, use a Sentinel-aware or cluster-aware client instead of a hardcoded hostname.
- Never set
replica-read-only no to silence the error — it trades a visible failure for a hidden data consistency bug.
- Add alerting on sustained READONLY errors so a real instability doesn't hide behind "it's just a failover."