Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

consistent

> DevOps
Open source

Consistent hashing with bounded loads in Golang

779 stars0 likes1 views
WebsiteGitHub

About

Consistent hashing with bounded loads in Golang

Consistent

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:

  • Consistent Hashing with Bounded Loads on Google Research Blog
  • Improving load balancing with a new consistent-hashing algorithm on Vimeo Engineering Blog
  • Consistent Hashing with Bounded Loads paper on arXiv

Table of Content

  • Overview
  • Notable Users
  • Install
  • Configuration
  • Usage
  • Benchmarks
  • Examples

Overview

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:

  • The member's name is hashed and inserted into the hash ring,
  • Average load is calculated by the algorithm defined in the paper,
  • Partitions are distributed among members by hashing partition IDs and none of them exceed the average load.

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:

  • The key(byte slice) is hashed,
  • The result of the hash is mod by the number of partitions,
  • The result of this modulo - MOD(hash result, partition count) - is the partition in which the key will be located,
  • Owner of the partition is already determined before calling 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.

Notable Users

buraksezer/consistent is used at production by the following projects:

  • olric-data/olric: Distributed, in-memory key/value store and cache.
  • aws/amazon-cloudwatch-agent-operator: The Amazon CloudWatch Agent Operator is software developed to manage the CloudWatch Agent on Kubernetes.
  • Azure/prometheus-collector: Azure Monitor managed service for Prometheus.
  • open-telemetry/opentelemetry-operator: Kubernetes Operator for OpenTelemetry Collector.
  • weibocom/motan-go: A cross-language remote procedure call(RPC) framework for rapid development of high performance distributed services.
  • vllm-project/aibrix: Cost-efficient and pluggable Infrastructure components for GenAI inference.
  • erda-project/erda: An enterprise-grade Cloud-Native application platform for Kubernetes.
  • giantswarm/starboard-exporter: Exposes Prometheus metrics from Starboard's VulnerabilityReport, ConfigAuditReport, and other custom resources (CRs).
  • megaease/easegress: A Cloud Native traffic orchestration system.
  • smartcontractkit/chainlink: Oracle platform bringing the capital markets onchain and the market leader powering the majority of decentralized finance.
  • rudderlabs/keydb: KeyDB is a distributed key store (not a key-value store) designed to be fast, scalable, and eventually consistent.
  • celo-org/celo-blockchain: Global payments infrastructure built for mobile.
  • koderover/zadig: Zadig is a cloud native, distributed, developer-oriented continuous delivery product.
  • mason-leap-lab/infinicache: InfiniCache: A cost-effective memory cache that is built atop ephemeral serverless functions.
  • opencord/voltha-lib-go: Voltha common library code.
  • kubeedge/edgemesh: Simplified network and services for edge applications.
  • authorizer-tech/access-controller An implementation of a distributed access-control server that is based on Google Zanzibar - "Google's Consistent, Global Authorization System.
  • Conflux-Chain/confura Implementation of an Ethereum Infura equivalent public RPC service on Conflux Network.
  • mapprotocol/atlas: Atlas chain is a truly fast, permissionless, secure and scalable public blockchain platform.

Install

With a correctly configured Go environment:

go get github.com/buraksezer/consistent

You will find some useful usage samples in examples folder.

Configuration

…

Any hash algorithm can be used as hasher which implements Hasher interface. Please take a look at the Sample section for an example.

Usage

LocateKey function finds a member in the cluster for your key:

go
// 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.

go
// 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.

Benchmarks

On an Apple M4 Pro:

…

Examples

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: 132433

Average load can be calculated by using the following formula:

load := (consistent.AverageLoad() * float64(keyCount)) / float64(config.PartitionCount)

Contributions

Please don't hesitate to fork the project and send a pull request or just e-mail me to ask questions and share ideas.

License

MIT License, – see LICENSE for more details.

Issues· 6 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Goawesomeawesome-goconsistent-hashingconsistent-hashing-library

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
CategoryDevOps
PricingOpen source

> Related tools

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理