System design paper · 1997

Consistent hashing

Karger, Lehman, Leighton, Panigrahy, Levine and Lewin · STOC 1997

How to spread data over many servers so that adding or losing one moves only a small slice of it, not almost all of it.

Every cache and every page is turned into a number from 0 to 1, and the numbers run round the circle like a clock face. Each page goes to the first cache clockwise from it, so here page P is stored on cache B.

You’ll learn to

  • Explain why the usual way to pick a server for each piece of data moves almost all of it when a server is added, and how placing servers and data on a circle of numbers fixes it.
  • Work out how much data moves when a cache joins or leaves, and why giving each cache many places on the circle evens out the work.
  • Describe how a separate tree of caches for each page spreads one very popular page over many machines.
  • Name the systems that use consistent hashing today, and the newer methods that improve on it.

The idea in short

In 1997 a popular web page could swamp its server for hours. Caches, machines that keep copies of pages, can share the load, but only if every browser agrees which cache holds which page. The usual way to decide sends almost every page somewhere new whenever a cache joins or leaves.

Consistent hashing uses a fixed recipe to turn the name of every cache and every page into a number, and places those numbers round a circle, like the minutes on a clock face. A page belongs to the first cache after it, going clockwise. A new cache takes over only the pages just before it, and nothing else moves. Give each cache many spots on the circle and the work is shared out evenly.

The problem

In the mid-1990s the web had a new kind of failure: the . A site would suddenly become popular, and its server and the network around it would be buried under requests. The 1997 paper this guide covers lists recent examples: NASA’s JPL site after the Shoemaker-Levy 9 comet struck Jupiter, an IBM site during the Deep Blue–Kasparov chess match, and political sites on election night. Users were sometimes locked out for hours or days. A follow-up paper by the same group put a number on it: a site mentioned on the evening news as the “cool site of the day” could see 10,000 times its normal traffic the next day.

Buying a bigger server doesn’t fix this. Nobody knows which site will be hot next, and no one can afford to be ready for 10,000 times their normal load every day.

was the obvious tool. A cache sits between browsers and servers, keeps copies of pages it has fetched, and answers repeat requests itself. The trouble was making many caches work as a team. The authors walk through what had been tried:

  • One shared cache for many users. The more users share it, the more requests it answers, and the more likely it is to be swamped itself.
  • Caches that ask each other. Before going to the server, a cache asks all the others at once whether they have the page. With many caches, these questions become a flood of their own.
  • A tree of caches, as in the Harvest project, a research web cache of the time. The caches are arranged in levels, like a family tree, with the server at the top. A request the nearest cache can’t answer goes up a level, and so on towards the server. But every page uses the same tree, so the cache at the top sees at least one request for every distinct page anyone asks for. That cache becomes the hot spot.
  • Copying busy pages. When a server is swamped, it copies the page to more machines. This needed every machine to know about every cache and to coordinate with all the others, which isn’t possible on the real web, where no one has the full picture.

There was a deeper problem underneath all of these. On the web, machines come and go, and news of that spreads slowly. One browser may know about 20 caches; another may have heard of only 18, or of 19 different ones. The paper calls each browser’s list of caches its view. Any scheme that needs everyone to agree on the list of caches breaks the moment views differ.

So the question was: how do you spread pages over a changing set of caches, with no central coordinator, so that browsers with different views still mostly agree on where each page lives, and no single machine gets swamped?

The design choices

The paper’s answer has two parts, and they are often confused:

  1. Random trees: a separate tree of caches for every page, so no machine sits near the top of the tree for many pages at once. This spreads the load of a hot page.
  2. : a way to map pages to caches that barely changes when caches join, leave or are missing from someone’s view. This is what keeps the trees, and everything else, stable.

Today consistent hashing is famous and random trees are mostly forgotten. This guide covers both, but spends most of its time on the hashing, because that is the part almost every system that spreads data or requests over many machines now uses: databases, caches and , which share incoming requests among servers.

A few decisions shape the rest:

  • Local control only. Every browser and cache decides on its own, from what it knows. No machine coordinates the others, and no machine needs a full list of caches. The paper only assumes each machine knows some share of them, say a tenth.
  • Randomness instead of planning. Where pages and caches end up is decided by a , a fixed recipe that turns a name into a random-looking number, so no one has to keep a table of who stores what. The paper proves that bad outcomes are very unlikely, not impossible.
  • Pages don’t change. The analysis treats every page as fixed. Pages that change over time are left as an open question.

The paper also sets out exactly what “barely changes” should mean, as four properties:

Property What it asks for Why it matters
Balance Each cache gets about the same share of pages No cache is overloaded
Monotonicity When a cache is added, pages move only to the new cache, never between old ones Adding a cache doesn’t shuffle everything else
Spread Across all the views, one page lands in only a few caches A page isn’t copied into every cache
Load Across all the views, one cache is given only a few different pages No cache is asked to hold far more than its share

Balance and monotonicity are about one view changing over time. Spread and load are about many views existing at once. The next sections take them in that order.

Why the usual method breaks

First, the tool both methods use. A hash function turns any text, such as a page’s URL, into a number. The same text always gives the same number, and different texts give numbers that look random, so they spread out evenly.

Each item to be stored has a name, called its . For a web cache, the key is the page’s URL. Here a server means any machine that stores items, a cache included. The standard way to spread keys over N servers, numbered 0 to N − 1, is to hash each key, divide the number by N and keep the remainder: server = hash(key) mod N. This is called hash mod N. It balances well and needs no table. The paper’s complaint is what happens when N changes.

To keep the numbers small, take 20 keys whose hashes happen to be 0 to 19, and 4 servers numbered 0 to 3. Key 6 goes to server 6 mod 4 = 2. Now add a fifth server. Key 6 goes to 6 mod 5 = 1. Most keys move like this, because changing N changes the answer for almost every hash value.

Before: 4 servers

key 6 → 6 mod 4 = 2; key 13 → 13 mod 4 = 1

Each key goes to the server numbered by its remainder.

After: 5 servers

key 6 → 6 mod 5 = 1; key 13 → 13 mod 5 = 3

The same keys get different remainders, so they go to different servers.

Count who stayed

only keys 0, 1, 2 and 3 → 4 of 20 stay, 16 move (80%)

A key stays only if its remainder mod 4 equals its remainder mod 5. Among 0 to 19, that happens only for keys smaller than 4.

In general, going from n servers to n + 1, only about 1 in n + 1 keys stays put. From 10 servers to 11, 91% of keys move. For a cache that is a disaster: almost every request now goes to a cache that doesn’t have the page yet, so the caches have to fetch nearly everything again from the servers behind them, all at once. The paper’s verdict: after a change, everything cached is useless.

Adding a fifth server to 20 keys. With hash mod N, 16 keys move. With consistent hashing, 4 move: the share the new server should take.

The diagram names the servers by letter, A to E. The second row uses consistent hashing, which the next section explains. It is the ideal. Five caches should each hold about a fifth of the keys, so the new one has to take about 4 of the 20 from somewhere. Moving those 4 is unavoidable. Moving any more is waste.

The paper proves that consistent hashing gets close to this ideal. Compare the list of caches before a change with the list after it. The share of pages that stay on the same cache is about the share of caches the two lists have in common. Going from 10 caches to 11, the lists share 10 of 11 caches, so about 10 in 11 pages stay and 1 in 11 moves: 9%, instead of 91%.

Placing caches and pages on a circle

Start with a hash function, and divide each result by the largest number it can give, so every result is a number between 0 and 1. A cache’s name might hash to 0.30, and a page’s URL to 0.47.

Now picture every number from 0 to 1 written along a line, and bend the line round until its two ends meet, like the edge of a clock face. 0 sits at the top, 0.25 at three o’clock, 0.5 at six and 0.75 at nine, and just after 0.99 you are back at 0. That loop of numbers is the circle. It isn’t a machine or something stored in memory. It is only a way of picturing which number comes next.

Every cache and every page now has a spot, or point, on the circle: the number its name hashes to. The rule has three steps:

  1. Hash each cache’s name to find its spot.
  2. Hash each page’s URL to find its spot.
  3. From the page’s spot, go clockwise, towards bigger numbers, until you reach a cache. That cache holds the page.

Place four caches

A = 0.05, B = 0.30, C = 0.55, D = 0.80

Their names hash to these four spots.

Find the cache for a page

page at 0.47 → next cache clockwise is C at 0.55

B at 0.30 is behind the page, so it doesn’t count. The first cache ahead of it is C.

Wrap round past the top

page at 0.90 → past 1, back to 0 → A at 0.05

No cache sits between 0.90 and 1, so the walk carries on past the top and reaches A.

The last step is the only reason it is a circle rather than a straight line: without the wrap, a page past the last cache would have nowhere to go.

The diagram at the top of this page shows the same rule: page P sits between A and B, so it goes to B. A browser needs only the hash function and its list of caches to work this out. No table, no coordinator, no messages.

One detail worth knowing: the 1997 paper actually assigns each page to the cache whose point is closest, in either direction. The 1999 follow-up, which built the real system, uses the next cache clockwise, and that is the version every system since has copied. Both keep the properties that matter; clockwise is just easier to explain and implement.

Adding a cache

Now add a fifth cache, E. It hashes to a point between A and B.

When E joins, only the pages between A and E move, and all of them come from B. Every other page stays where it was.

Before, a page between A and B went clockwise to B. Now some of those pages reach E first. Those pages, the ones between A and E, move from B to E. Nothing else changes: a page anywhere else on the circle reaches the same cache as before.

This is monotonicity in action. Pages move only to the new cache, never between old ones. Removing a cache is the same in reverse: its pages slide clockwise to the next cache, and no other page moves.

With one point per cache, though, “a fifth of the keys move” is only true on average. Where E lands is random. It may take a tiny slice from B or a huge one. That points at the real weakness of the plain circle.

Many points per cache

With one point per cache, the gaps between caches are random, and random gaps are very uneven. A cache owns the arc, the stretch of circle, that ends at its point: every page on that stretch goes to it. So a cache that happens to follow a big gap gets a big share of the pages.

How uneven? Place 10 caches at random on a circle, and the biggest gap is on average about 29% of the circle. So the busiest cache can expect about 29% of all pages instead of its fair 10%. Nearly three times its share, just from bad luck.

The paper’s fix is to give every cache many points on the circle. Hash “A-1”, “A-2”, “A-3” and so on, and treat each one as cache A. A page still goes to the first point clockwise, and that point says which cache to use. Each cache now owns many small arcs instead of one big one. Some are bigger than average and some smaller, and they largely cancel out, so the totals come out far more even.

With one point each, A owns 45% of the circle, nearly twice its fair 25%. In this example, with four points each, A’s four arcs add up to 27%. Systems that place points at random use hundreds per cache.

The paper shows that a modest number of points per cache is enough, and that it needs to grow only slowly as caches are added. Machines in a system are often called , so these extra points are what later systems call : one real machine standing in several places on the circle. Later systems also call the circle the ring.

How many points are enough? A useful rule of thumb: if each cache has k points, the typical difference between a cache’s share of pages and the average share shrinks like 1 over the square root of k. A hundred times more points makes it ten times smaller.

typical difference from the average ≈ 1÷√k
k = points per cache. With k = 100, loads typically differ from the average by about 10%. With k = 1,000, by about 3%.

The 1999 paper measured this on real data. It took a week of logs from a web server at MIT, 26,804 distinct URLs, and spread them over caches with many points each:

Caches Average URLs per cache Typical difference from the average As a share of the average
3 8,934 246 2.7%
5 5,360 173 3.2%
8 3,350 112 3.4%
10 2,680 68 2.6%

Every cache stayed within a few per cent of its fair share. Many points also help in a second way. When a cache leaves, its pages don’t all fall on one unlucky neighbour; its many small arcs are picked up by many different caches.

Clients that disagree

Everything so far assumes one view: everyone sees the same caches. The web doesn’t work like that, and this is where consistent hashing earns its name: browsers that see different caches still give mostly the same answers.

Suppose one browser knows about caches A, B, C and D, and another has never heard of B. Both use the same circle. For a page P whose next cache clockwise is B, the first browser asks B. The second browser skips the B it doesn’t know about and asks C, the next cache it does know.

Two clients with different views send page P to different caches, B and C. But the disagreement is small: P ends up in 2 caches, not 4.

So P ends up cached in two places. That is the price of disagreement, and the paper shows it stays small:

  • Spread. Across all the views, any one page lands in only a small number of caches. A browser that doesn’t know a cache skips past it to the next cache it does know, so a page only ever spreads to the caches just after its point.
  • Load. Across all the views, any one cache is handed only a few distinct pages beyond its fair share.

The paper proves limits on both. If every browser knows at least, say, a tenth of the caches, a page is copied into a few dozen caches at most, however many browsers there are and however much their views differ. With mod N hashing, browsers with different N simply disagree about almost every page.

The 1999 paper tested this too. It placed 1,500 pages on 80 caches under many views, with 5 of the caches missing at random from each view. Counting every cache that ended up holding each page, across all the views, there were 1,877 copies: only 25% more than the 1,500 there would be if every view had agreed.

This also answers what to do about failures. The paper’s advice is simple: if you decide a cache is down, take it out of your view. Your pages slide to the next cache clockwise, and other browsers that haven’t noticed yet still mostly agree with you.

Finding the cache quickly

A browser has to do this lookup for every request, so it has to be fast. With all the points kept in a list sorted by their position on the circle, finding “the next point clockwise” is a : check the middle of the list, throw away the half that can’t hold the answer, and repeat.

Keep every point in sorted order

100 caches × 1,000 points = 100,000 points

Each point records which cache it belongs to.

Hash the URL and search for the next point

halving 100,000 down to 1 takes about 17 checks

Wrap around to the smallest point if the URL hashes past the largest.

Return that point’s cache

about 20 millionths of a second on a 1998 PC

That’s about 0.1% of the 20 thousandths of a second it took to send a small page over an office network of the day.

The 1999 team timed exactly this setup, 100 caches with 1,000 points each, on an ordinary PC of the time. The whole step, including hashing the URL, took 20 millionths of a second on average.

The 1997 paper goes one step further. Cut the circle into many equal slices and keep a short sorted list for each. The hash tells you the slice directly, and each slice holds only a few points, so a lookup takes about the same tiny time however many caches there are.

Random trees for hot pages

Consistent hashing decides which cache owns a page. On its own it doesn’t solve the original problem: if a page is very hot, the one cache that owns it gets swamped instead of the server. The paper’s second idea, random trees, handles that.

Picture a tree like the Harvest one, with the page’s home server, the web server it comes from, at the top, called the root, and caches everywhere else. The caches at the bottom are the leaves. In the diagram each cache has two below it. Every page gets its own tree, with each position filled by a cache chosen by hashing the page’s URL together with the position’s number. So for page P the first cache below the home server might be F, while for page Q the same spot holds a completely different cache.

Each page gets its own tree of caches. A request starts at a random leaf and climbs only as far as it has to, and caches copy the page once it’s asked for often.

A request for P works like this:

The browser picks a random leaf

here, leaf A

It works out P’s tree from the hash and its own list of caches, picks a leaf at random, so that requests are shared across all the leaves, and sends the request there.

Each cache answers or passes it up

A → F → home server

A cache with a copy of P answers. Otherwise it passes the request to the cache above it.

A cache copies P once it’s been asked enough

after q requests

Each cache counts requests for P. When the count reaches a set number, q, it keeps its own copy.

Why this spreads the load: when P is cold, requests climb all the way to the server, and that’s fine because there are few of them. As P heats up, caches near the root see enough requests to keep a copy, and then caches below them, so the copies spread down the tree towards the browsers. A very hot page ends up copied in many caches, and its requests are spread across all the leaves. A cold page costs almost nothing, because a cache only stores pages it has been asked for q times.

Because every page has a different tree, no cache sits near the root for many pages. In the Harvest design, one root saw every page. Here each cache sits just below the top for only a small share of pages.

The costs are easy to state. A request travels at most from leaf to root and back, so the delay is at most twice the height of the tree, its number of levels. The paper proves no scheme that avoids swamping can do better. Giving each cache more caches below it makes the tree shorter, but puts more load on each cache. As for q, the analysis found that raising it seemed only to hurt, and suggested q = 2.

The two ideas fit together. The trees use consistent hashing to pick the cache for each position, so a browser with a slightly different view builds a slightly different tree, and the trees still mostly agree.

What production taught them

The 1997 paper is mostly proofs. Two years later, a group at MIT led by David Karger, one of the 1997 authors, built the idea into a working web cache, the Cache Resolver, and reported what it took.

Letting the browser choose

The key design change was to have the browser decide which cache to ask. In the usual setup, a browser always talks to its local cache, which then asks other caches or the server. A page could be missing from every cache on the way, and each one added a delay. In the Cache Resolver, the browser hashes the URL and goes straight to the cache that owns it, so a page is looked for in at most one cache.

Browsers of the day couldn’t be changed, so the team used two things browsers already supported:

  • A browser setup script. Browsers could load a small script that picks which cache to use for each URL. The team’s script hashed each URL to one of 1,000 made-up cache names and returned five of them in order, so the browser could try the next one if a cache failed, plus a final “go direct to the server”.
  • , the internet’s system for turning names into machine addresses. The team’s DNS servers turned each made-up name into a real cache’s address, using consistent hashing over the caches that were alive. When a cache died or joined, only the DNS servers needed to know, and only the made-up names whose points sat next to it on the circle were handed to a different cache.

Splitting the work this way meant browsers never needed a list of caches at all. The authors noted that browsers could later hold the 1,000-name map themselves.

The test

They tested on 7 machines: 3 caches, a web server behind a slow link, a DNS server, a machine sending requests for 1,500 files totalling 34 MB, and one machine used by the setups they compared against. Cache sizes ran from 9 to 36 MB. Compared with the common setup, where each browser uses one local cache, the Cache Resolver found more pages already in its caches, and the gap was largest when the caches were small. That makes sense: when every browser has its own local cache, a popular page ends up copied in every one of them. With consistent hashing, each page is stored once across the group, so the same disk space holds more distinct pages.

Setups where caches ask each other did about as well overall. But whenever the first cache didn’t have a page, they paid for an extra round of questions before going to the server.

Hot pages and regions

The paper also sketches how the real system would handle the problems from 1997:

  • Hot pages. A very hot name can be given a list of cache addresses instead of one, and DNS hands them out in turn, which spreads the requests. The plan was to spread a hot site’s names across every cache in a region first, then remove addresses one at a time as the heat dies down.
  • Nearby caches. A two-step DNS lookup first picks the browser’s region, then a cache inside it.
  • No single machine whose failure breaks everything. Caches, DNS servers and browsers all work from local information.

This is very close to what came next. Two of the 1997 authors, Tom Leighton and his student Danny Lewin, co-founded Akamai in 1998 to run this kind of system for paying websites. It launched its commercial service in April 1999 and went public that October. Consistent hashing was one of its founding ideas.

What held up and what changed

The core idea held up completely. Almost every system that spreads data over a changing set of machines uses consistent hashing or a close relative. What changed is the circle itself: later work found other ways to get the same property, each better at one thing.

Rendezvous hashing. Independently in 1996, David Thaler and Chinya Ravishankar at the University of Michigan proposed what is now called . For each page, compute a score for every cache, from a hash of the page and the cache together, and pick the cache with the highest score. When a cache leaves, only its own pages move, each to the cache with its next-highest score. The 1997 paper mentions this approach and sets it aside for large systems, because every lookup has to score every cache. For a few dozen caches that cost is tiny, and Microsoft’s web cache of the time worked this way.

Chord. In 2001, Karger and others at MIT and Berkeley used the circle for a system with no central servers, where every machine is equal. Each key belongs to the first machine clockwise. The new part was finding that machine without knowing every other one: each machine keeps a short list of others: one a little way ahead, one twice as far, one twice as far again, up to halfway round the ring. Each hop at least halves the distance left to go, so a lookup among a million machines takes about 20 hops.

Dynamo and Cassandra. Amazon’s Dynamo (2007) put data on a ring with virtual nodes and stored each key on the first few machines clockwise, so a copy survives if one machine fails. It is the most famous use of consistent hashing in a database; see our Dynamo guide for how its way of splitting data changed in production. Cassandra, an open-source database, copied the ring and its virtual nodes. Since version 4.0 it gives each machine 16 points by default, and places them to balance the load rather than at random, which is why it needs so few.

Jump consistent hash. In 2014, John Lamping and Eric Veach at Google published a consistent hash that needs no memory at all: a few lines of code turn a key and the number of servers into a server number. It is faster than the ring and almost perfectly balanced. Their comparison: with 1,000 points per server, loads on a ring still differ by about 3.2%, while jump hash’s differ by almost nothing. The catch is that servers must be numbered 0, 1, 2 and so on, and you can only add or remove the last one. That suits a database split into numbered parts, but not a group of caches where any machine can fail.

Maglev. Google’s Maglev (2016) spreads network traffic over many servers. It builds a large fixed-size table in which the servers take turns claiming their favourite empty slots, so every server gets almost the same number. A key is hashed to a slot, and the slot names the server. It moves a few more keys than the ring when a server changes, but in exchange the balance is very even and every lookup is a single table read, which matters when every piece of traffic needs one.

Bounded loads. In 2016, Vahab Mirrokni, Mikkel Thorup and Morteza Zadimoghaddam added a cap: no server may hold more than a set margin, say 25%, above the average. A key whose server is full keeps walking clockwise to the first one with room. Each change then moves only a bounded number of keys, and no server is ever overloaded. Google used it in its message service, Cloud Pub/Sub, and when Andrew Rodland at Vimeo added it to the HAProxy load balancer, Vimeo’s cache bandwidth fell by a factor of almost 8.

Method Lookup Memory Balance Best for
Ring with virtual nodes (1997) Binary search over the points One entry per point Good, better with more points Caches and stores where any machine can come or go
Rendezvous hashing (1996) Score every cache One entry per cache Good Small groups; no points to tune
Jump hash (2014) A few steps of arithmetic None Near perfect Numbered parts that grow or shrink at the end
Maglev (2016) One table read Fixed-size table Near perfect Spreading network traffic over servers
Bounded loads (2016) Binary search, plus a short walk Ring plus a load count per server Capped at a set margin Spreading traffic over caches

What dropped out of use is the other half of the paper. Random trees aren’t used much under that name. The job they did, spreading a hot page across many caches, is now done by , companies that run caches close to users around the world, with their own layers of caches and their own handling of hot content. But the principle of giving each page its own random arrangement of machines, so no machine is special for everything, is still everywhere.

References

About the paper

Consistent Hashing and Random Trees: Distributed Caching Protocols for Relieving Hot Spots on the World Wide Web by Karger, Lehman, Leighton, Panigrahy, Levine and Lewin, STOC 1997.

Read the original paper

More System design papers

All papers