READONLY error
A command that changes data was sent to a read-only replica.
Redis returns this error when you send a command that changes data to a replica. A replica keeps a copy of a primary's data, and by default it's read-only. Only the primary accepts writes.
READONLY You can't write against a read only replica.
A Lua function, or a script that starts with a #!lua line, gets the same error on a replica before it starts, unless it has the no-writes flag. A script without that line gets the error when it calls a command that changes data, with the script's details added to the end.
In client libraries
| Client library | Exception or error type | Message |
|---|---|---|
| redis-py | ReadOnlyError |
You can't write against a read only replica. |
| Jedis | JedisDataException |
READONLY You can't write against a read only replica. |
| node-redis | SimpleErrorTest with err.message.startsWith('READONLY') |
READONLY You can't write against a read only replica. |
| Lettuce The async API wraps the exception in an ExecutionException (get()) or a CompletionException (join()). |
RedisReadOnlyException |
READONLY You can't write against a read only replica. |
| go-redis | redis.ErrorTest with redis.IsReadOnlyError(err) |
READONLY You can't write against a read only replica. |
| ioredis | ReplyErrorTest with err.message.startsWith('READONLY') |
READONLY You can't write against a read only replica. |
| StackExchange.Redis NRedisStack uses StackExchange.Redis, so it raises the same exceptions. The sync and async APIs raise the same exceptions. |
RedisConnectionException |
No connection (requires writable - not eligible for replica) is active/available to service this operation: SET, …StackExchange.Redis refuses a write to a replica itself, so the server's READONLY error never arrives. |
| Predis | Predis\Response\ServerExceptionTest with $e->getErrorType() === 'READONLY' |
READONLY You can't write against a read only replica. |
| redis-rb | Redis::ReadOnlyError |
READONLY You can't write against a read only replica. (redis://<host>:<port>) |
| redis-rs The sync and async APIs raise the same errors. |
ErrorKind::Server(ServerErrorKind::ReadOnly) |
ReadOnly: You can't write against a read only replica. |
Example
> SET mykey "value"
(error) READONLY You can't write against a read only replica.
> GET mykey
"value"
How to fix it
Check which node you're connected to with ROLE. A replica replies with slave as the first item.
- You meant to connect to the primary. Check the host and port in your app's configuration.
- The primary changed. After a failover, a replica becomes the new primary, and the old primary can come back as a replica. An app that connects to a fixed address then ends up on a replica. Use a client that finds the current primary for you, through Redis Sentinel.
Don't make the replica writable to work around this error. The replica-read-only no setting exists for historical reasons, and writes to a replica can make its data differ from the primary's. See Read-only replica.