All Projects → gobwas → hashring

gobwas / hashring

Licence: MIT License
Consistent hashing hashring implementation.

Programming Languages

go
31211 projects - #10 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to hashring

hashring
Consistent-hashing: Hash ring implementation in Go
Stars: ✭ 30 (+7.14%)
Mutual labels:  consistent-hashing, hashring
consistent-hashing
an implementation of Consistent Hashing in pure Ruby using an AVL tree
Stars: ✭ 40 (+42.86%)
Mutual labels:  consistent-hashing
dynamic-sharding
用动态分片解决pushgateway高可用 单点 HA问题
Stars: ✭ 27 (-3.57%)
Mutual labels:  consistent-hashing
distributed-dev-learning
汇总、整理常用的分布式开发技术,给出demo,方便学习。包括数据分片、共识算法、一致性hash、分布式事务、非侵入的分布式链路追踪实现原理等内容。
Stars: ✭ 39 (+39.29%)
Mutual labels:  consistent-hashing
rust-hash-ring
A consistent hashing library in Rust
Stars: ✭ 30 (+7.14%)
Mutual labels:  consistent-hashing
Doramon
个人工具汇总:一致性哈希工具,Bitmap工具,布隆过滤器参数生成器,Yaml和properties互转工具,一键式生成整个前后端工具,单机高性能幂等工具,zookeeper客户端工具,分布式全局id生成器,时间转换工具,Http封装工具
Stars: ✭ 53 (+89.29%)
Mutual labels:  consistent-hashing
lua-resty-jump-consistent-hash
consistent hash for openresty
Stars: ✭ 24 (-14.29%)
Mutual labels:  consistent-hashing

hashring

GoDoc CI

Consistent hashing hashring implementation.

Overview

This is an implementation of the consistent hashing hashring data structure.

In general, consistent hashing is all about mapping objects from a very big set of values (e.g., request id) to objects from a quite small set (e.g., server address). The word "consistent" means that it can produce consistent mapping on different machines or processes without additional state exchange and communication.

For more theory about the subject please see this great document.

For more info about the package please read the docs.

The Why?

This is an approach for load distribution across servers which I found convinient to be used in a few projects I had worked on in the past.

I think it is good to implement it from scratch to synthesize all my experience working with it and share it with the community in hope it will help to build something good.

The key points of the package:

  1. Efficient concurrent read operations
  2. Correct handling of write operations (order of insertions on different processes doesn't matter; hash collisions are handled carefully).

Installation

go get github.com/gobwas/hashring

Usage

package main

import (
	"strings"
	"io"

	"github.com/gobwas/hashring"
)

func main() {
	var ring hashring.Ring
	_ = ring.Insert(StringItem("server01"))
	_ = ring.Insert(StringItem("server02"))
	_ = ring.Insert(StringItem("server03"))
	_ = ring.Insert(StringItem("server04"))

	ring.Get(StringItem("user01")) // server04
	ring.Get(StringItem("user02")) // server04
	ring.Get(StringItem("user03")) // server02
	ring.Get(StringItem("user04")) // server01
}

type StringItem string

func (s StringItem) WriteTo(w io.Writer) (int64, error) {
	n, err := io.WriteString(w, string(s))
	return int64(n), err
}

Contributing

If you find some bug or want to improve this package in any way feel free to file an issue to discuss your findings or ideas.

Debugging

Note that this package comes with built in debug tracing (which only takes place if you pass hashring_trace build tag, thanks to gtrace zero-cost tracing).

This means that you can make hashring to print out each meaningful step it does to understand better what's happening under the hood.

It also consumes hashring_debug build tag, which allows you to hook into hash calculation process and override the value it returns. For example, this was useful to test hash collisions.

Magic factor

Magic factor is a number of "virtual" nodes each item gets on a ring. The higher this number, the more equal distribution of objects this ring produces and the more time is needed to update the ring.

The default value is picked through testing different sets of servers and objects, however, for some datasets it may be enough to have smaller (or higher) value of magic factor. There is a branch called magic which contains code used to generate statistics on distribution of objects depending on magic factor value.

Here is the sample result of distribution of 10M objects across 10 servers with different magic factor values: Magic factor plot

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].