Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/munawwar/hrw-hash
๐คน HRW / Rendezvous hashing in JS
https://github.com/munawwar/hrw-hash
consistent-hashing hash hashing-algorithm hrw-hashing nodejs rendezvous-hashing
Last synced: about 1 month ago
JSON representation
๐คน HRW / Rendezvous hashing in JS
- Host: GitHub
- URL: https://github.com/munawwar/hrw-hash
- Owner: Munawwar
- License: mit
- Created: 2022-02-09T19:31:32.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2024-07-14T06:31:02.000Z (6 months ago)
- Last Synced: 2024-11-28T18:12:27.161Z (about 2 months ago)
- Topics: consistent-hashing, hash, hashing-algorithm, hrw-hashing, nodejs, rendezvous-hashing
- Language: JavaScript
- Homepage:
- Size: 200 KB
- Stars: 7
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HRW (Highest Random Weight) hashing / Rendezvous hashing
https://en.wikipedia.org/wiki/Rendezvous_hashing.
```
npm install hrw-hash
``````js
import { hrwHash } from 'hrw-hash'
// or const { hrwHash } = require('hrw-hash')
const servers = ['image-server-1.example.com', 'image-server-2.example.com']
const domainToUse = hrwHash('example.png', servers)[0] // always 'image-server-2.example.com'
```Advantage of this hashing over other sticky load balancing algorithms is that:
1. it does not use a shared lookup table / ring
2. it is independent of the order of destinations (hrwHash('john', ['a', 'b']) is same as hrwHash('john', ['b', 'a']))
3. it distributes keys evenly (probabilistically) when a destination is added or removedDisadvantage of this hashing method:
1. Replacing a destination with another one will re-route keys unnecessarily as the algo is order insensitiveThe implementation is small (0.5 kb minified) and with no 3rd party dependencies. It uses bigint, unescape and encodeURIComponent, so it supports modern browsers, node.js >= 12.5.0 and cloudflare workers.
It uses `mulberry32(fnv1a32('key'))` as hash function internally for the random hash. In my tests, this gives better distribution than fnv1a-32 alone and still keeps the implementation fast and small.
```js
// for advanced use cases you can import the hash function
import { hashFunc } from 'hrw-hash'
hashFunc('string') // returns positive integer < 2^32
```# Dev Test
Test the types against https://arethetypeswrong.github.io
# Breaking Change V2
- Removed minified dist files (dist/*.min.js). If you want minified build for browser, then use `import { hrwHash } from 'https://esm.sh/hrw-hash'`
- Removed UMD build
- Changed minimum node.js version requirement from >= v12.0.0 to >= 12.5.0