If several workers share a pool through Redis, a sorted set can implement a simple policy: choose the eligible proxy with the fewest recorded allocations. Store a proxy identifier as the member and its allocation count as the score.

For a pool serving permitted public requests, the free proxy list can supply endpoint candidates to evaluate under that pool’s eligibility rules. Initialize eligible members with an agreed starting count. Then make selection and increment one server-side operation:

local chosen = redis.call('ZRANGE', KEYS[1], 0, 0)[1]
if not chosen then
    return nil
end

redis.call('ZINCRBY', KEYS[1], 1, chosen)
return chosen

Pass the sorted-set key as the script’s single key argument through your Redis client’s EVAL interface. An empty pool produces a null result that the application must handle without making a request.

Why combine the two commands?

Separate reads and increments let two workers observe the same minimum before either updates it. Redis executes a Lua script atomically, so another worker cannot insert a selection between this script’s read and increment. Keep the script short because its execution blocks other Redis work.

ZRANGE returns the lowest score first; equal scores are resolved by member order, not randomly. ZINCRBY raises the chosen member’s score by one for the next allocation.

Define what the score means

This counts allocations, including an allocation whose network request later fails. It does not measure latency, successful requests or work currently in progress. Repeatedly selecting the sole eligible member is still possible; the script does not reserve it exclusively.

Maintain eligibility using the pool’s health and policy rules. If you remove and later restore a member, decide whether its historical count should be retained. Starting a restored proxy at zero can concentrate allocations on it while it catches up.

A least-concurrent policy needs a different lifecycle, including completion and abandoned-job handling. Do not interpret this cumulative counter as that lifecycle.

Winndannde jowitiiɗe heen