Skip to content

Rotate slaves #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Rotate slaves #50

wants to merge 1 commit into from

Conversation

TimWeils
Copy link

While using this connector in my code I noticed that the operations towards slaves are not balanced between all slave instances returned by Sentinel. Connector just tries to connect to slaves in the order returned by Sentinel and uses the first one that succeeds. I checked how other Redis Clients solve this problem and for example client for Python rotates the slaves in round-robin manner (as you can see here). I implemented similar rotation for this connector so that all slave instances are utilized equally.

@stepulak
Copy link

This is really helpful, thank you.

Could you please @pintsized or @piotrp look at this pull request?

Copy link
Member

@pintsized pintsized left a comment

Choose a reason for hiding this comment

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

LGTM. Could you please add a test case to cover the change? You can find examples in sentinel.t.

@piotrp
Copy link
Contributor

piotrp commented May 5, 2025

You shouldn't keep rotation state in a global variable, this should be kept in connector instance. And, preferably, be a opt-in behavior (or at least opt-out?).

A few technical remarks:

  • you call #slaves a lot of times, this should be kept in a variable
  • a new array should be created by tab_new

A generalized rotation function could look like this:

local function rotate_slaves_2(slaves, rotate_by)
    local slave_count = #slaves
    local rotated_slaves = tbl_new(#slaves, 0)

    for i, slave in ipairs(slaves) do
        local new_index = (i - rotate_by - 1) % slave_count + 1
        rotated_slaves[new_index] = slave
    end

    return rotated_slaves
end

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.

4 participants