A toy consistent hashing project in rust.
This is a standard consistent hashing implementation, where you'll be spinning up a new allocator and storage nodes across
a hash space. If you add / remove a new storage node, only K/n
migrations needs to happen on an average where K
being the
number of keys and n
being the number of nodes.
Why? because I was bored af and other rust projects were kinda standard.
We store the keys in anti clockwise direction. If a key is placed on 4
on the hashspace, we'll be storing it in nearest
smallest hash allocated (in the image, it's on slot 1
in node A
)
This storage strategy currently does not have virtual nodes, so replication and hot-nodes would be an issue in v0.1.0
$ cargo build
$ RUST_LOG=chrrrrrr=debug ./target/debug/chrrrrrr --mode a
Kindly note: Default port for server is :3030
$ RUST_LOG=chrrrrrr=debug ./target/debug/chrrrrrr --mode n --port 3031
$ RUST_LOG=chrrrrrr=debug ./target/debug/chrrrrrr --mode n --port <your_port>
curl --location --request POST 'http://localhost:3030/insert' \
--header 'Content-Type: application/json' \
--data-raw '{
"key": "{your_key}",
"value": "{your_value}"
}'
curl --location --request GET 'http://localhost:3030/get/{your_key}'
}'
Note: you can add new storage nodes any time you want, allocator will take care of migrating the keys to new nodes.
- Build a demo script
- Background thread to see if nodes are still responsive and dequeue accordingly.
- Implementing virtual nodes.
I saw conhash-rs being a package for consistent hashing, probably much more convenient than spinning up new servers by self.