Skip to content

Add CommandFlags.PreferAnyServer so reads can use the primary and replicas - #3390

Open
ShmueLior wants to merge 2 commits into
seredis:mainfrom
ShmueLior:prefer-any-server-2974
Open

ShmueLior wants to merge 2 commits into
seredis:mainfrom
ShmueLior:prefer-any-server-2974

Conversation

@ShmueLior

Copy link
Copy Markdown
Contributor

Fixes #2974

With only one or two replicas, PreferReplica leaves the primary idle for reads. This adds a flag that lets a read go to any node, primary or replica, so reads are spread across all of them. It still picks one server per call, like the other flags.

Name is a placeholder for now (PreferAnyServer). Happy to rename it if you prefer something else.

What changed:

  • New CommandFlags.PreferAnyServer = 16, using the slot that was marked as reserved for more prefer/demand options.
  • Standalone: picks the next available server in the existing round robin, primary or replica.
  • Cluster: round robin over the slot owner and its replicas, so it never leaves the shard that owns the key.
  • Commands that must run on a primary still go to the primary, same as PreferReplica today. A transaction that has any primary-only work does the same, using the rule from Send a transaction to the primary when it has primary-only work and the caller prefers a replica #3381.
  • If you combine it with DemandMaster, PreferReplica or DemandReplica, that one wins.
  • MOVED/ASK resend works the same as PreferMaster.

Tests:

  • New PreferAnyServerUnitTests on the in-process server (standalone and cluster, keyed and keyless, combined flags, primary-only commands, no replicas).
  • GetServerTestsBase.PreferAnyServerReadsFromPrimaryAndReplica against real Redis (standalone and cluster), with NoRedirect so a wrong node would fail.
  • Transaction tests: primary-only work with PreferAnyServer runs on the primary, and a read-only transaction spreads over both nodes.
  • FormatTests rows for the new value (not added for .NET Framework, since I can't run that locally).

Checklist

  • I fully and freely contribute this code in accordance with the project license (and am legally able to do so)
  • I take responsibility for this contribution's quality and correctness, including any portions produced with AI assistance (see CONTRIBUTING.md).

…eredis#3381

A transaction with primary-only work must still run on the primary when the
caller uses PreferAnyServer, the same way seredis#3381 already does for PreferReplica.
A read-only transaction with PreferAnyServer should spread over both nodes.
@mgravell

mgravell commented Dec 2, 2026

Copy link
Copy Markdown
Member

thanks for the input; I don't think I have time to think about side-effects on this before 3.5.0, but: will review shortly after

This branch has not been deployed

No deployments
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.

Read CommandFlags option replica+primary

2 participants