{"id":13412520,"url":"https://github.com/mbrostami/consistenthash","last_synced_at":"2026-06-05T08:00:55.017Z","repository":{"id":144210228,"uuid":"257950901","full_name":"mbrostami/consistenthash","owner":"mbrostami","description":"A Go library that implements Consistent Hashing (+Block Partitioning)","archived":false,"fork":false,"pushed_at":"2026-06-04T22:18:03.000Z","size":43,"stargazers_count":32,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-06-05T00:09:21.700Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mbrostami.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-22T16:01:25.000Z","updated_at":"2026-06-04T22:18:07.000Z","dependencies_parsed_at":"2024-01-07T21:54:06.021Z","dependency_job_id":"aa9770b1-780b-4d53-bd7c-f6395e83d463","html_url":"https://github.com/mbrostami/consistenthash","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/mbrostami/consistenthash","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mbrostami%2Fconsistenthash","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mbrostami%2Fconsistenthash/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mbrostami%2Fconsistenthash/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mbrostami%2Fconsistenthash/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mbrostami","download_url":"https://codeload.github.com/mbrostami/consistenthash/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mbrostami%2Fconsistenthash/sbom","scorecard":{"id":631408,"data":{"date":"2025-08-11","repo":{"name":"github.com/mbrostami/consistenthash","commit":"eeffd0ceeecc2a82a1549139c4c5fc0111f27b09"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3,"checks":[{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Code-Review","score":0,"reason":"Found 0/25 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 6 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-21T07:59:17.198Z","repository_id":144210228,"created_at":"2025-08-21T07:59:17.198Z","updated_at":"2025-08-21T07:59:17.198Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33935514,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-05T02:00:06.157Z","response_time":120,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T20:01:25.635Z","updated_at":"2026-06-05T08:00:54.985Z","avatar_url":"https://github.com/mbrostami.png","language":"Go","funding_links":[],"categories":["分布式系统","Distributed Systems","Go","Relational Databases"],"sub_categories":["检索及分析资料库","Search and Analytic Databases","SQL 查询语句构建库","Advanced Console UIs"],"readme":"[![Go Report Card](https://goreportcard.com/badge/github.com/mbrostami/consistenthash)](https://goreportcard.com/report/github.com/mbrostami/consistenthash)\n\n# Consistent Hashing\n\nA Go library that implements Consistent Hashing with zero dependencies.\nThis package is based on [golang/groupcache](https://github.com/golang/groupcache) with a few additions.\n\n### Additions over groupcache:\n\n- `Remove` function added - removes a node (and all of its replicas) from the ring\n- int hashes replaced with `uint32`\n- Number of replicas is configurable per node via `AddReplicas` (useful when nodes have different capacity)\n- Concurrency-safe: all operations are guarded by a single `sync.RWMutex`\n- Stored keys are copied, so mutating the caller's buffer afterwards can't corrupt the ring\n\n\u003e Note: the `WithBlockPartitioning` option was removed. The earlier block-based\n\u003e lookup returned the wrong node once the ring spanned more than one block and\n\u003e was not concurrency-safe; lookups now use a sorted ring with binary search\n\u003e (`O(log N)`), which also benchmarked faster. No code change is needed unless\n\u003e you were passing `WithBlockPartitioning` (simply drop it).\n\n# Technical Details\n\nThe ring is a single slice of positions kept sorted by hash value, plus two maps:\n\n- **ring** `[]node` - every position on the ring (one per replica), sorted by hash. Each entry points back to the original key.\n- **hashMap** `map[uint32][]byte` - maps the hash of a key to the original key value.\n- **replicaMap** `map[uint32]uint` - records the replica count for a key only when it differs from the default (so adding 10k keys at the default replica count uses zero extra entries here).\n\n### Lookup\n\n`Get` hashes the lookup key and binary-searches the sorted ring for the first\nposition `\u003e= hash`, wrapping back to the first position when the key falls past\nthe end of the ring. Time complexity is **O(log N)** where N is the total number\nof positions (keys x replicas).\n\n### Adding\n\n`Add` / `AddReplicas` generate the replica positions for each key, append them to\nthe ring, then sort and de-duplicate. Re-adding the same key is idempotent. The\nsort makes the result independent of insertion order, so two rings built from the\nsame keys always agree.\n\n### Removing\n\n`Remove` recomputes the exact positions a key occupies and filters them out of the\nring in a single pass. It returns `false` if the key was not present.\n\n# Usage\n\n`go get github.com/mbrostami/consistenthash/v2`\n\n### Simple use case\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/mbrostami/consistenthash/v2\"\n)\n\nfunc main() {\n\n\tch := consistenthash.New(consistenthash.WithDefaultReplicas(10))\n\tch.Add([]byte(\"templateA\"), []byte(\"templateB\"))\n\n\tassignedTemplate := ch.Get([]byte(\"userA\")) // assigned template should always be the same for `userA`\n\tfmt.Printf(\"assigned: %s\", assignedTemplate)\n}\n```\n\n### Weighted load\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/mbrostami/consistenthash/v2\"\n)\n\nfunc main() {\n\tch := consistenthash.New(consistenthash.WithDefaultReplicas(10))\n\tch.Add([]byte(\"127.0.0.1:1001\"), []byte(\"127.0.0.1:1002\"))\n\tch.AddReplicas(40, []byte(\"127.0.0.1:1003\")) // 4x more weight\n\n\trKey := \"something like request url or user id\"\n\tnode := ch.Get([]byte(rKey)) // find upper closest node\n\tfmt.Println(node) // this will print out one of the nodes\n}\n\n```\n\n## More about consistent hashing\n\nYou can find some explanation in this blog post: https://liuzhenglaichn.gitbook.io/system-design/advanced/consistent-hashing\n\nThe code example to achieve a hash ring similar to the following picture:\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/mbrostami/consistenthash/v2\"\n)\n\n\nfunc main() {\n\tch := consistenthash.New(consistenthash.WithDefaultReplicas(3))\n\tch.Add([]byte(\"A\"), []byte(\"B\"), []byte(\"C\"))\n\n\tfmt.Println(ch.Get([]byte(\"Alica\")))\n\tfmt.Println(ch.Get([]byte(\"Bob\")))\n\tfmt.Println(ch.Get([]byte(\"Casey\")))\n\n}\n\n```\n\n![Image](https://1865312850-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Bkp-b8HYQgJF1rkOc%2F-M5FwA4YIBAqAjZvdVpU%2F-M5FyQai2CtC2j4GGr5K%2Fimage.png?alt=media\u0026token=77d5d346-f37f-4f28-8f64-66a1627d2deb)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmbrostami%2Fconsistenthash","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmbrostami%2Fconsistenthash","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmbrostami%2Fconsistenthash/lists"}