Thanks to visit codestin.com
Credit goes to github.com

Skip to content

add random load balancing strategy which allows for use of the primary#4027

Merged
petyaslavova merged 5 commits into
redis:masterfrom
Pack-Yak1:master
Apr 9, 2026
Merged

add random load balancing strategy which allows for use of the primary#4027
petyaslavova merged 5 commits into
redis:masterfrom
Pack-Yak1:master

Conversation

@Pack-Yak1

@Pack-Yak1 Pack-Yak1 commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

Description of change

This MR adds a new LoadBalancingStrategy value: RANDOM. It is identical to the existing RANDOM_REPLICA except it allows for the use of the primary as well.

Our team would like to make use of the random load balancer but don't want to waste the primary, which is the motivation for this PR

Pull Request check-list

Please make sure to review and check all of these items:

  • Do tests and lints pass with this change?
  • Do the CI tests pass with this change (enable it first in your forked repo and wait for the github action build to finish)?
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Is there an example added to the examples folder (if applicable)?

NOTE: these things are not required to open a PR and can be done
afterwards / while the PR is open.


Note

Medium Risk
Changes cluster node selection for read commands by introducing a new random strategy that can route reads to primaries as well as replicas; incorrect selection boundaries could shift traffic patterns or expose edge cases in slot node lists.

Overview
Adds a new LoadBalancingStrategy.RANDOM option that randomly selects from all nodes serving a slot (including the primary), alongside the existing replica-only random behavior.

Refactors the load balancer’s random selection into _get_random_server_index(list_size, replicas_only) and updates sync/async cluster tests to cover the new strategy with deterministic random.randint mocking and adjusted expectations.

Reviewed by Cursor Bugbot for commit 45e96dd. Bugbot is set up for automated code reviews on this repo. Configure here.

@jit-ci

jit-ci Bot commented Apr 8, 2026

Copy link
Copy Markdown

Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset.

In case there are security findings, they will be communicated to you as a comment inside the PR.

Hope you’ll enjoy using Jit.

Questions? Comments? Want to learn more? Get in touch with us.

@jit-ci

jit-ci Bot commented Apr 8, 2026

Copy link
Copy Markdown

🛡️ Jit Security Scan Results

CRITICAL HIGH MEDIUM

✅ No security findings were detected in this PR


Security scan by Jit

@petyaslavova petyaslavova 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.

Hi @Pack-Yak1, thank you for your contributions! Can you please add some tests covering the changes?

Comment thread redis/cluster.py Outdated
Comment thread redis/cluster.py
Comment thread redis/cluster.py Outdated
@Pack-Yak1

Copy link
Copy Markdown
Contributor Author

Added test cases and responded to comments above

@petyaslavova petyaslavova 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.

@petyaslavova petyaslavova added the feature New feature label Apr 9, 2026
@petyaslavova petyaslavova merged commit 407ceba into redis:master Apr 9, 2026
68 checks passed
petyaslavova added a commit that referenced this pull request Apr 16, 2026
#4027)

* add random load balancing strategy which allows for use of the primary

* address style comments

* add test cases for new strategy

* apply lint fixes

---------

Co-authored-by: petyaslavova <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants