/Consistent-Hashing

This repository contains a Java implementation of ring consistent hashing, a technique for distributing data across a cluster of servers in a way that minimizes the amount of data that needs to be moved when a server is added or removed from the cluster.

Primary LanguageJava

Ring Consistent Hashing in Java

This repository contains a Java implementation of ring consistent hashing, a technique for distributing data across a cluster of servers in a way that minimizes the amount of data that needs to be moved when a server is added or removed from the cluster. The implementation includes two hashing functions, Murmur and FNV, which can be used to generate hash values for keys.

Features

  • Ring consistent hashing implementation in Java
  • Two hashing functions included: Murmur and FNV
  • Easy-to-use API for adding servers and getting server assignments for keys
  • Customizable number of virtual nodes per server for fine-grained control over distribution

Usage

To use the ring consistent hashing implementation, you can create a ConsistentHash object and add servers to it using the addServer method:

ConsistentHash  ring = new ConsistentHash<>(new Murmur(), 100);
hash.addNode("server1");
hash.addNode("server2");
hash.addNode("server3");

Once you have added servers to the hash, you can use the getServer method to determine which server a given key should be assigned to:

String key = "mykey";
String server = ring.chooseNode(key);

The getServer method will return the name of the server that the key should be assigned to, based on the hash value generated by the selected hashing function.

Hashing Functions

The implementation includes two hashing functions, Murmur and FNV. You can choose which hashing function to use when you create a ConsistentHash object:

ConsistentHash ring = new ConsistentHash(new FNV, 100);

Both hashing functions implement the HashFunction interface, which defines a single method, hash, that takes a key as input and returns a hash value as a long. You can also implement your own custom hashing function by implementing the HashFunction interface and passing an instance of your implementation to the ConsistentHash constructor.

License

This implementation is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! If you find a bug or have a feature request, please open an issue or submit a pull request.