Way of evenly distributing load of Caches such as CDN
We need to map request ID to different servers
The request ID generally encapsulates user ID and hence less likely to change for a user
We can cache stuff for particular user in a particular server
But if we cache then we need to make sure even if we increase servers, the request should hit the same server again, hence consistent hashing is needed irrespective of increase in number of servers
It does not work well for databases
Uses
Distributed Caches
Load Balancers
Implement consistent hashing
We can choose M and hash function h()
we can create a ring of [0,1,...,M-1] points which are circular
we map h(server_id) % M and mark points on the circle
For each request_id, we calculate h(request_id) % M and mark point on circle
we go clock-wise and try to find nearest h(server_id) % M
The request is served by the nearest server clockwise
This should make the load distributed equally in 1/N manner
Any change like adding/deleting the server should not cause much difference for the requests being served by each server
But practically it does not happen and we may end up with skewed distributions