Member-only story
A Simple Implementation Of Consistent Hashing
Let’s say that you have M tasks, and you want to distribute to N servers. Here is the interface of the class we are going to build:
We have a class called ConsistentMap and a get operation where we want to get an assigned server that we want to execute the task.
We will add another function allows us to add a server to the system.
Our goal is to distribute the tasks to the servers equally. To do this: we use a hash function and hash all the servers and tasks into the same space. And then assign the task to the next server available on the space. This implement assumes the hashCode
function of both Server
and Task
are evenly distributed in integer range.

We can use a TreeMap
to store the server and to look up for the server (O(logN) complexity)