Skip to content

Minimal Golang library for implemeting weighted round robin load balancing.

License

Notifications You must be signed in to change notification settings

mr-karan/balance

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

logo

balance

A minimal Golang library for implementing weighted round-robin load balancing for a given set of items.

Installation

go get github.com/mr-karan/balance

Usage

package main

import (
    "fmt"
    "github.com/mr-karan/balance"
    
    // Create a new load balancer.
    b := balance.NewBalance()

    // Add items to the load balancer with their corresponding weights.
    b.Add("a", 5)
    b.Add("b", 3)
    b.Add("c", 2)    
    
    // Get the next item from the load balancer.
    fmt.Println(b.Get())

    // For 10 requests, the output sequence will be: [a b c a a b a c b a]
)

Algorithm

The algorithm is based on the Smooth Weighted Round Robin used by NGINX.

Algorithm is as follows: on each peer selection we increase current_weight of each eligible peer by its weight, select peer with greatest current_weight and reduce its current_weight by total number of weight points distributed among peers.

Examples

Round Robin

For implementing an equal weighted round-robin load balancer for a set of servers, you can use the following config:

b.Add("server1", 1)
b.Add("server2", 1)
b.Add("server3", 1)

Since the weights of all 3 servers are equal, the load balancer will distribute the load equally among all 3 servers.

Weighted Round Robin

For implementing a weighted round-robin load balancer for a set of servers, you can use the following config:

b.Add("server1", 5)
b.Add("server2", 3)
b.Add("server3", 2)

The load balancer will distribute the load in the ratio of 5:3:2 among the 3 servers.

Benchmark

go test -v -failfast -bench=. -benchmem -run=^$
goos: linux
goarch: amd64
pkg: github.com/mr-karan/balance
cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
BenchmarkBalance
BenchmarkBalance/items-10
BenchmarkBalance/items-10-8             18249529                63.82 ns/op            0 B/op          0 allocs/op
BenchmarkBalance/items-100
BenchmarkBalance/items-100-8             9840943               119.5 ns/op             0 B/op          0 allocs/op
BenchmarkBalance/items-1000
BenchmarkBalance/items-1000-8            1608460               767.1 ns/op             0 B/op          0 allocs/op
BenchmarkBalance/items-10000
BenchmarkBalance/items-10000-8            123394              9621 ns/op               0 B/op          0 allocs/op
BenchmarkBalance/items-100000
BenchmarkBalance/items-100000-8            10000            102295 ns/op               0 B/op          0 allocs/op
PASS
ok      github.com/mr-karan/balance     7.927s

About

Minimal Golang library for implemeting weighted round robin load balancing.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published