liblb: github.com/lafikl/liblb/consistent Index | Examples | Files

package consistent

import "github.com/lafikl/liblb/consistent"

Consistent uses consistent hashing algorithm to assign work to hosts. Its best for the cases when you need affinty, and your hosts come and go. When removing a host it gaurantees that only 1/n of items gets reshuffled where n is the number of servers.

One of the issues with Consistent Hashing is load imbalance when you have hot keys that goes to a single server, it's mitigated by using virtual nodes, which basically means when adding a host we add n - 20 in our case - replicas of that host.

Beware that Consistent Hashing doesn't provide, an upper bound for the load of a host.

If you need such gaurantees see package liblb/bounded.

https://en.wikipedia.org/wiki/Consistent_hashing

Code:

hosts := []string{"127.0.0.1:8009", "127.0.0.1:8008", "127.0.0.1:8007"}

lb := consistent.New(hosts...)
for i := 0; i < 10; i++ {
    host, err := lb.Balance(fmt.Sprintf("hello world %d", i))
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Send request #%d to host %s\n", i, host)
}

Index

Examples

Package Files

consistent.go

type Consistent Uses

type Consistent struct {
    // contains filtered or unexported fields
}

func New Uses

func New(hosts ...string) *Consistent

func (*Consistent) Add Uses

func (c *Consistent) Add(host string)

func (*Consistent) Balance Uses

func (h *Consistent) Balance(key string) (host string, err error)

func (*Consistent) Remove Uses

func (c *Consistent) Remove(host string)

Package consistent imports 2 packages (graph). Updated 2017-07-19. Refresh now. Tools for package owners.