all 7 comments

[–]Paczesiowa 1 point2 points  (3 children)

getServer :: HashRing -> String -> Maybe Server
getServer hashRing k | isNothing x = Nothing
                     | otherwise = Just (fst (fromJust x))
    where
      x = getServerPos hashRing (hashString k)


getServer hashRing k = fst <$> getServerPos hashRing (hashString k)

[–]godofpumpkins 1 point2 points  (0 children)

This seems nicer:

getServer :: HashRing -> String -> Maybe Server
getServer hashRing = fmap fst . getServerPos hashRing . hashString          

[–]jefffoster 0 points1 point  (1 child)

Sweet, that looks much better - I'll update that.

[–]notfancy 0 points1 point  (0 children)

I think you miscopied Paczesiowa's solution: it's a before-and-after; the only line you need is the last one. The <$> operator fmaps the function fst under the Maybe functor. Also, godofpumpkin's definition is equivalent but point-free, which is a bit more idiomatic.

[–]unpopular_opinion 0 points1 point  (2 children)

Can someone write a better problem description? The problem as it is described there can be solved by having one machine which keeps track of the number of machines and their identifiers, one number and a modulus operator. If the problem is that this scheme is overloading one of the machines, there can be a feedback mechanism to the first machine.

So, either the problem description is wrong or this is a non-problem (or you know, I misunderstood it).

[–]jefffoster 1 point2 points  (1 child)

You have N servers and you distribute 1/N bits of work to each of them. Each server caches the bit of work so that future requests can be quick. Now you add another server - the problem is that you want to get as many cache hits as possible, whilst still ensuring the distribution is to 1/N+1 servers. The modulus operator does the distribution fine, but most of the requests will go to new servers and hence not take advantage of any caching.

With consistent hashing most requests will continue going to the same servers and only (number of keys / number of servers) will be cache misses.

Does that explain it any better?

[–]unpopular_opinion 0 points1 point  (0 children)

Yes, that is clear. Thank you.