Consistent hashing with bounded loads in Golang
This library provides a consistent hashing function which simultaneously achieves both uniformity and consistency.
For detailed information about the concept, you should take a look at the following resources:
In this package's context, the keys are distributed among partitions and partitions are distributed among members as well.
When you create a new consistent instance or call Add/Remove:
Average load cannot be exceeded. So if all members are loaded at the maximum while trying to add a new member, it panics.
When you want to locate a key by calling LocateKey:
MOD(hash result, partition count) - is the partition in which the key will be located,LocateKey. So it returns the partition owner immediately.No memory is allocated by consistent except hashing when you want to locate a key.
Note that the number of partitions cannot be changed after creation.
buraksezer/consistent is used at production by the following projects:
VulnerabilityReport, ConfigAuditReport, and other custom resources (CRs).With a correctly configured Go environment:
go get github.com/buraksezer/consistentYou will find some useful usage samples in examples folder.
…Any hash algorithm can be used as hasher which implements Hasher interface. Please take a look at the Sample section for an example.
LocateKey function finds a member in the cluster for your key:
// With a properly configured and initialized consistent instance
key := []byte("my-key")
member := c.LocateKey(key)It returns a thread-safe copy of the member you added before.
The second most frequently used function is GetClosestN.
// With a properly configured and initialized consistent instance
key := []byte("my-key")
members, err := c.GetClosestN(key, 2)This may be useful to find backup nodes to store your key.
On an Apple M4 Pro:
…The most basic use of the consistent package should be like this. For a detailed list of functions, visit pkg.go.dev. More sample code can be found under _examples.
…Another useful example is _examples/relocation_percentage.go. It creates a consistent object with 8 members and distributes partitions among them. Then adds 9th member,
here is the result with a proper configuration and hash function:
…Moved partition count is highly dependent on your configuration and quailty of hash function. You should modify the configuration to find an optimum set of configurations for your system.
_examples/load_distribution.go is also useful to understand load distribution. It creates a consistent object with 8 members and locates 1M key. It also calculates average
load which cannot be exceeded by any member. Here is the result:
Maximum key count for a member should be around this: 147602
member: node2.olric, key count: 100362
member: node5.olric, key count: 99448
member: node0.olric, key count: 147735
member: node3.olric, key count: 103455
member: node6.olric, key count: 147069
member: node1.olric, key count: 121566
member: node4.olric, key count: 147932
member: node7.olric, key count: 132433Average load can be calculated by using the following formula:
load := (consistent.AverageLoad() * float64(keyCount)) / float64(config.PartitionCount)Please don't hesitate to fork the project and send a pull request or just e-mail me to ask questions and share ideas.
MIT License, – see LICENSE for more details.
No open issues yet, or sync has not completed.