Skip to content

Instantly share code, notes, and snippets.

@eapache
Last active December 1, 2015 20:41
Show Gist options
  • Save eapache/cd5464bf2218ad4b56af to your computer and use it in GitHub Desktop.
Save eapache/cd5464bf2218ad4b56af to your computer and use it in GitHub Desktop.
Theoretical global-knowledge algorithm for smart kafka partition rebalancing
brokers = [1,2,...]
# ensure exactly 3 replicas per partition
for p in partitions
if len(p) > 3; p = p[0...2]
if len(p) < 3; p += (brokers - p).sample(3 - len(p))
weights = {}
for p in partitions
for b in p
weights[b]++
while max(weight) - min(weight) > 3 # any proof 3 is always possible?
let p = (find partition whose list contains the max-weight broker but not the min-weight broker)
p.replace(max-weight-broker, min-weight-broker)
weights = {}
for p in partitions
weights[p.leader]++
while max(weight) - min(weight) > 1 # any proof 1 is always possible?
let p = (find partition led by max-weight broker also replicated on min-weight broker)
p = [min-weight, max-weight, the-other-one]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment