Skip to content

AZAffinity & AZAffinityReplicasAndPrimary Implementation - #47

Open
gulbaki wants to merge 1 commit into
valkey-io:mainfrom
gulbaki:implement-az-affinity-strategy
Open

gulbaki wants to merge 1 commit into
valkey-io:mainfrom
gulbaki:implement-az-affinity-strategy

Conversation

@gulbaki

@gulbaki gulbaki commented Jul 14, 2025

Copy link
Copy Markdown

Fixes: #23
Implements support for AZAffinity and AZAffinityReplicasAndPrimary read strategies based on the Valkey blog post on AZ Affinity.
Developers can now use:
scaleReads: "AZAffinity" | "AZAffinityReplicasAndPrimary", clientAz: "zone-a" // etc.
During Redis initialization, a HELLO 3 call is made to retrieve the availabilityZone, which is then saved under each node instance.
A new selectNodeAz method was introduced. This method is called internally when scaleReads is set to AZAffinity or AZAffinityReplicasAndPrimary. Based on the strategy, it routes commands to the most suitable node:
AZAffinity logic:

  1. Prefer a replica in the same AZ
  2. If not available, pick a replica from a different AZ
  3. If none, fallback to master.

AZAffinityReplicasAndPrimary logic:

  1. Prefer a replica in the same AZ
  2. If not available, fallback to local master
  3. Then to remote replica
  4. Then to remote master

Tests
Tests were added to cover each of the above scenarios.
However, two tests sometimes pass and sometimes fail:
I couldn't figure out exactly why this happens. I couldn’t find a solution
AZAffinity → selects remote replica when no local replica exists
AZAffinityReplicasAndPrimary → selects local primary when no local replica exists

and I’m unsure if I’ve placed the test logic in the best place,
or if additional handling is needed in selectNodeAz or elsewhere

@mcollina

Copy link
Copy Markdown
Collaborator

Are you still interested in updating this PR? It now needs a rebase and several correctness fixes before it can be reconsidered:

  • Keep the connection in RESP2 (HELLO or HELLO 2), because this client uses redis-parser@3, which cannot parse the RESP3 map returned by HELLO 3.
  • Read the actual availability_zone field rather than az.
  • Ensure AZ metadata is available for all candidate nodes before metadata-dependent routing begins; currently lazy nodes can be selected before their zone is known.
  • Distribute reads among matching replicas instead of always selecting the first one.
  • Replace the synthetic HELLO response with realistic RESP2 coverage, make the routing tests deterministic, and resolve the reported flakes.
  • Resolve the current merge conflicts, failed CI jobs, and DCO failure.

Please let us know if you plan to continue the work.

@gulbaki

gulbaki commented Jul 27, 2026

Copy link
Copy Markdown
Author

@mcollina yes, I’m still interested. I’ll update the PR.

@gulbaki
gulbaki force-pushed the implement-az-affinity-strategy branch 2 times, most recently from 7500a0d to 8cab4a3 Compare July 28, 2026 13:38
Add AZAffinity read strategies and get node zone data with HELLO 2. Wait for all node metadata before routing reads and add tests for replica selection.

Signed-off-by: Baki Gul <bbkgull@gmail.com>
@gulbaki
gulbaki force-pushed the implement-az-affinity-strategy branch from 8cab4a3 to a62eecc Compare July 30, 2026 14:36
@gulbaki

gulbaki commented Jul 30, 2026

Copy link
Copy Markdown
Author

@mcollina Hi, I’ve updated and pushed the PR. The requested fixes are complete,
I’m not sure if I need to mention this, but I used Claude and Codex as coding assistants and reviewed all changes myself.

@mcollina mcollina left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

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.

Implement az-affinity-strategy

2 participants