Consistent Hashing

//  - consistent_main.go - 
package main

import (
	"fmt"

	"github.com/buraksezer/consistent"
	"github.com/cespare/xxhash"
)

// In your distributed system, you probably have a custom data type
// for your cluster members. Just add a String function to implement
// consistent.Member interface.
type myMember string

func (m myMember) String() string {
	return string(m)
}

// consistent package doesn't provide a default hashing function.
// You should provide a proper one to distribute keys/members uniformly.
type hasher struct{}

func (h hasher) Sum64(data []byte) uint64 {
	// you should use a proper hash function for uniformity.
	return xxhash.Sum64(data)
}

func main() {
	// Create a new consistent instance
	cfg := consistent.Config{
		PartitionCount:    7,
		ReplicationFactor: 20,
		Load:              1.25,
		Hasher:            hasher{},
	}
	c := consistent.New(nil, cfg)

	// Add some members to the consistent hash table.
	// Add function calculates average load and distributes partitions over members
	node1 := myMember("node1.olricmq.com")
	c.Add(node1)

	node2 := myMember("node2.olricmq.com")
	c.Add(node2)

	key := []byte("my-key1")
	// calculates partition id for the given key
	// partID := hash(key) % partitionCount
	// the partitions is already distributed among members by Add function.
	owner := c.LocateKey(key)
	fmt.Println(owner.String())
	// Prints node2.olricmq.com
}

#github.com/mbrostami/consistenthash

//  - consistenthash_main.go - 
package main

import (
	"fmt"

	chash "github.com/mbrostami/consistenthash"
)

func main() {
	// Create ConsistentHash with 2 replicas
	ch := chash.NewConsistentHash(2, nil)
	ch.Add("127.0.0.1:1001") // node 1
	ch.Add("127.0.0.1:1002") // node 2
	// ch.AddReplicas("127.0.0.1:1003", 4) // node3 has more capacity so possibility to get assigned request is higher than other nodes

	rKey := "something like request url"
	node := ch.Get(rKey) // find upper closest node
	fmt.Println(node)    // this will print out one of the nodes
}

#doublejump

This is a revamped Google’s jump consistent hash. It overcomes the shortcoming of the original implementation - not being able to remove nodes. Here is the main idea behind doublejump.

//  - doublejump_main.go - 
package main

import (
	"fmt"

	"github.com/edwingeng/doublejump"
)

func main() {
	h := doublejump.NewHash()
	for i := 0; i < 10; i++ {
		h.Add(fmt.Sprintf("node%d", i))
	}

	fmt.Println(h.Len())
	fmt.Println(h.LooseLen())

	fmt.Println(h.Get(1000))
	// fmt.Println(h.Get("foobar"))
	fmt.Println(h.Get(2000))
	fmt.Println(h.Get(3000))
	fmt.Println(h.All())
}