Consistent hashing is a technique used in distributed systems to uniformly distribute data across nodes while minimizing data movement when nodes are added or removed. It is particularly useful in scenarios like load balancing, distributed caching, and sharding.
- Uniform Distribution: Ensures data is evenly distributed across available nodes.
- Scalability: Minimizes data redistribution when adding or removing nodes.
- Fault Tolerance: Easily handles node failures by redistributing only the affected keys.
- Flexibility: Suitable for various applications like caching, load balancing, and distributed systems.
- Hashing Keys: Each data item (key) is hashed to a position on a virtual ring (usually represented as a circular space).
- Hashing Nodes: Each node is also hashed to a position on the same virtual ring.
- Data Assignment: A key is assigned to the first node that appears in a clockwise direction on the ring.
- Adding/Removing Nodes: When nodes are added or removed, only the keys on the affected segments of the ring are redistributed, minimizing disruption.
- Reduced Data Movement: Only a fraction of keys are moved when nodes change.
- Efficient Scaling: Adding more nodes does not require rehashing all data.
- Improved Performance: Distributes load effectively across nodes.
To use this implementation, clone the repository:
# Clone the repository
git clone https://github.com/mohammadJBYaseen/consistent-hashing.git
# Navigate to the project directory
cd consistent-hashingimport consistenthashing.ConsistentHashing;
public class Main {
public static void main(String[] args) {
// Create a consistent hashing instance
ConsistentHashing ch = new ConsistentHashing();
// Add nodes
ch.addNode("Node1");
ch.addNode("Node2");
ch.addNode("Node3");
// Assign keys to nodes
String node = ch.getNodeForKey("my-data-key");
System.out.println("Key is assigned to " + node);
// Remove a node
ch.removeNode("Node2");
}
}Run the tests to ensure everything works as expected:
# Navigate to the test directory
cd src/test
# Compile and run the tests
javac -d ../bin *.java
java -cp ../bin org.junit.runner.JUnitCore TestConsistentHashing- Distributed Caching: Systems like Memcached and Redis clusters.
- Load Balancing: Distributing traffic across web servers.
- Sharding: Partitioning databases or file systems.
We welcome contributions! If you'd like to contribute, please:
- Fork the repository.
- Create a feature branch.
- Commit your changes.
- Submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
Feel free to reach out with any questions or feedback!