Skip to content

fix: Always send RedisCluster raw commands to the master - #3018

Closed
tillkruss wants to merge 1 commit into
phpredis:developfrom
tillkruss:fix/rawcommand-readonly
Closed

tillkruss wants to merge 1 commit into
phpredis:developfrom
tillkruss:fix/rawcommand-readonly

Conversation

@tillkruss

Copy link
Copy Markdown
Member

Fixes #3017.

RedisCluster::rawCommand() never set c->readonly, so it inherited the value left by the previous command. After a read with FAILOVER_DISTRIBUTE or FAILOVER_DISTRIBUTE_SLAVES, a raw write could be sent to a replica and be rejected with READONLY.

This sets c->readonly = 0 in rawCommand(), as cluster_flush_cmd() and directed INFO already do. Raw commands routed by key now also always go to the slot's master. Before, they were only distributed to replicas when the previous command happened to be a read.

The new testDirectedRawCommandUsesMaster fails without the change and passes with it (Redis 8.10.3, 3 primaries with 1 replica each, PHP 8.5.12). The other RedisCluster tests have the same results with and without the change.

`RedisCluster::rawCommand()` never set `c->readonly`, so it inherited the
value left by the previous command. After a read with `FAILOVER_DISTRIBUTE`
or `FAILOVER_DISTRIBUTE_SLAVES`, a raw write could be sent to a replica and
be rejected with `READONLY`.

Fixes phpredis#3017
@michael-grunder

Copy link
Copy Markdown
Member

I think this si the best solution until we built some kind of string -> command lookup table.

@tillkruss tillkruss closed this Nov 24, 2026
michael-grunder added a commit that referenced this pull request Nov 24, 2026
* Add `@redis-readonly` PHPDoc tags to denote read-only commands.
* Create a  helper to generate the lookup table of read-only commands with `gperf`
* Check if  commands are read-only in `rawCommand` invocation.
* Fix four more locations where we were allowing `c->readonly` to be
  inherited

Fixes: #3017

See: #3018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RedisCluster: directed rawCommand() may be routed to a replica under FAILOVER_DISTRIBUTE

2 participants