The Simple Magic of Consistent Hashing
Join the DZone community and get the full member experience.Join For Free
the simplicity of consistent hashing is pretty mind-blowing. here you have a number of nodes in a cluster of databases, or in a cluster of web caches. how do you figure out where the data for a particular key goes in that cluster?
you apply a hash function to the key. that's it? yeah, that's the whole deal of consistent hashing. it's in the name, isn't it?
the same key will always return the same hash code (hopefully), so once you've figured out how you spread out a range of keys across the nodes available, you can always find the right node by looking at the hash code for a key.
it's pretty ingenious, if you ask me. it was cooked up in the lab chambers at akamai, back in the late nineties. you should go and read the original paper right after we're done here .
consistent hashing solves the problem people desperately tried to apply sharding to pretty nicely and elegantly. i'm not going to bore you with the details on how exactly consistent hashing works. mike perham does a pretty good job at that already, and there are many more blog posts explaining implementations and theory behind it . also, that little upcoming book of mine has a full-length explanation too. here's a graphic showing the basic idea of consistent hashing, courtesy of basho.
instead i want to look at the practical implications of consistent hashing in distributed databases and cache farms.
easier to avoid hotspots
when you put data on nodes based on a random result, which is what the hash function calculates, a value that's a lot more random than the key it's based on, it's easier to avoid hotspots. why?
assume a key based on an increasing value, or a simple range of keys, based on the hour of the day, like 2011-12-11-13. you add new hours and therefore new data as time passes, and keys are stored based on the range they fall in. for example, the keys 2011-12-11-18 until 2011-12-11-23 are stored on the same node, with the rest of the keys stored on other nodes, just because the ranges or the partitioning scheme happen to be set up this way.
for a consumer-facing site, the evening hours are usually the busiest time of the day. they create more data, more writes, and possibly more reads too. for the hours between 18:00 and 23:00, all the load goes to the single node that carries all the relevant data.
but when you determine the location in the cluster based solely on the hash of the key, chances are much higher that two keys lexicographically close to each other end up on different nodes. thus, the load is shared more evenly. the disadvantage is that you lose the order of keys.
there are partitioning schemes that can work around this, even with a range-based key location. hbase (and google's bigtable, for that matter) stores ranges of data in separate tablets. as tablets grow beyond their maximum size, they're split up and the remaining parts re-distributed. the advantage of this is that the original range is kept, even as you scale up.
consistent hashing enables partitioning
when you have a consistent hash, everything looks like a partition. the idea is simple. consistent hashing forms a keyspace, which is also called continuum, as presented in the illustration. as a node joins the cluster, it picks a random number, and that number determines the data it's going to be responsible for. everything between this number and one that's next in the ring and that has been picked by a different node previously, is now belong to this node. the resulting partition could be of any size theoretically. it could be a tiny slice, or a large one.
first implementations of consistent hashing still had the problem that a node picking a random range of keys resulted in one node potentially carrying a larger keyspace than others, therefore still creating hotspots.
but the improvement was as simple as it was ingenious. a hash function has a maximum result set, a sha-1 function has a bit space of 2^160. you do the math. instead of picking a random key, a node could choose from a fixed set of partitions, like equally size pizza slices. but instead of picking the one with the most cheese on, everyone gets an equally large slice. the number of partitions is picked up front, and practically never changes over the lifetime of the cluster.
for good measure, here's a picture of a sliced pizza.
partitioning makes scaling up and down more predictable
with a fixed number of partitions of the same size, adding new nodes becomes even less of a burden than with just consistent hashing. with the former, it was still unpredictable how much data had to be moved around to transfer ownership of all the data in the range of the new node. one thing's for sure, it already involved a lot less work than previous methods of sharding data.
with partitioning, a node simply claims partitions, and either explicitly or implicitly asks the current owners to hand off the data to them. as a partition can only contain so many keys, and randomness ensures a somewhat even spread of data, there's a lot less unpredictability about the data that needs to be transferred.
if that partitions just so happens to carry the largest object by far in you whole cluster, that's something even consistent hashing can't solve. it only cares for keys.
going back to hbase, it cares for keys and the size of the tablet the data is stored in, as it breaks up tablets once they reach a threshold. breaking up and reassigning a tablet requires coordination, which is not an easy thing to do in a distributed system.
consistent hashing and partitioning enable replication
consistent hashing made one thing a lot easier: replicating data across several nodes. the primary means for replication is to ensure data survives single or multiple machine failures. the more replicas you have, the more likely is your data to survive one or more hardware crashes. with three replicas, you can afford to lose two nodes and still serve the data.
with a fixed set of partitions, a new node can just pick the ones it's responsible for, and another stack of partitions it's going to be a replica for. when you really think about it, both processes are actually the same. the beauty of consistent hashing is that there doesn't need to be master for any piece of data. every node is simply a replica of a number of partitions.
but replication has another purpose besides ensuring data availability.
replication reduces hotspots (even more!!!)
having more than one replica of a single piece of data means you can spread out the request load even more. with three replicas of that data, residing on three different nodes, you can now load-balance between them. neat!
with that, consistent hashing enables a pretty linear increase in capacity as you add more nodes to a cluster.
consistent hashing enables scalability and availability
consistent hashing allows you to scale up and down easier, and makes ensuring availability easier. easier ways to replicate data allows for better availability and fault-tolerance. easier ways to reshuffle data when nodes come and go means simpler ways to scale up and down.
it's an ingenious invention, one that has had a great impact. look at the likes of memcached , amazon's dynamo , cassandra , or riak . they all adopted consistent hashing in one way or the other to ensure scalability and availability.
want to know more about distributed databases in general and riak in particular? you'll like the riak handbook , a hands-on guide full of practical examples and advice on how to use riak to ensure scalability and availability for your data.
in the next installment we're looking at the consequences and implications of losing key ordering in a riak cluster.
Opinions expressed by DZone contributors are their own.