READONLY: Write rejected on a read only replica
The connection points at a replica, and Redis refuses writes there: only the primary accepts commands that modify data.
- Error prefix
READONLY
它的意義
READONLY means the write landed on a replica. A Redis replica serves reads (by default, possibly stale data), but every write must go to the primary so it can be replicated in order. With replica-read-only at its default of yes, any write command on a replica gets this error instead of executing.
It usually means the client is pointed at the wrong host: a connection string still naming yesterday's primary, a DNS record that moved after a failover, or a proxy that hands out replicas to writers. It also bites after a manual promotion when the old primary was never demoted and clients keep writing to it.
常見原因
- The connection string or DNS name points at a replica instead of the primary.
- A failover or promotion finished and clients still hold the old primary's address, which is now a replica.
- A proxy or service discovery layer includes replicas in the pool it hands to writers.
Find the cause
redis-cli -h node-2 ROLE # The role (slave; Redis keeps this word in ROLE on every version) plus the master's host and port redis-cli -h node-2 INFO replication # role, master_host, master_link_status # From the suspected primary, who follows it? redis-cli -h node-1 INFO replication | grep -E 'role:|slave[0-9]|replica[0-9]'
如何修正
-
Check what the node you are connected to believes it is:
redis-cli -h node-2 INFO replication | grep 'role:' # role:slave means writes fail here
-
Ask any replica where the primary is now:
redis-cli -h node-2 INFO replication | grep master_host redis-cli -h node-2 ROLE
-
Point writers at the primary. After a promotion, demote the old primary so it follows the new one:
redis-cli -h old-primary REPLICAOF new-primary 6379
Other ways to handle it
For failover, let the nodes agree on roles themselves: Redis Sentinel or Redis Cluster reassign them and clients follow. If you promote by hand, demote the old primary the moment writes move, or you will be chasing READONLY across half the fleet.
安全地重現問題
redis-server --port 7302 --replicaof 127.0.0.1 6399 --save '' & redis-cli -p 7302 SET foo bar # (error) READONLY You can't write against a read only replica. redis-cli -p 7302 GET foo # (nil) - reads still work; stale data is served by default
問題
Can I make a replica writable?
replica-read-only no exists, but such writes are not replicated and vanish on the next synchronization. It is meant for special setups, not as a workaround.
Why did the same write work yesterday and fail today?
The node changed role. Failover tools swap primaries and replicas; your client kept the old address, which is now a replica.
相關錯誤
MASTERDOWNStale data is not allowed to be servedSQLITE_READONLYSQLite:嘗試寫入唯讀資料庫25006PostgreSQL:唯讀 SQL 交易
More cluster and replication errors
MOVEDThe key lives on another cluster nodeASKThe key is migrating to another nodeCROSSSLOTMulti-key command spans two slotsCLUSTERDOWNThe cluster cannot serve this slotTRYAGAINSlot is rehashing, retry the commandNOREPLICASToo few healthy replicas to write
程式碼和訊息文字來自 Redis documentation and source code。瀏覽完整的 Redis error list 或 搜尋每個資料庫錯誤.
Work on Redis in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for Redis and the other databases you use.
本頁文字與資料: CC BY 4.0,請註明出處 SQLly (sqlly.app)。 程式碼與 SQL 片段: MIT. 授權詳情 引用的錯誤訊息文字歸其資料庫廠商所有。