import org.springframework.cache.annotation.Cacheable;
@Cacheable(“pi")publicdoublecalculatePi(double n){
double pi = 0;
for (int i = 1; i < n; i++) {
pi += Math.pow(-1, i + 1) / (2 * i - 1);
}
return4 * pi;
}
defset(key, value, servers):
n = len(servers)
index = hash(key) % n
servers[index].set(key, value)
# base case # adding one node (3+1) # removing one node (3-1)>>> n = 3 >>> n = 4 >>> n = 2>>> hash('key1') % n >>> hash('key1') % n >>> hash('key1') % n
120>>> hash('key2') % n >>> hash('key2') % n >>> hash('key2') % n
011
Consistent Hashing
Consistent hashing is based on mapping each object to a point on the edge of a circle (or equivalently, mapping each object to a real angle). The system maps each available machine (or other storage bucket) to many pseudo-randomly distributed points on the edge of the same circle.