Back to blog

A cache node dies: why does half the cache vanish?

Thắng Đoàn
Thắng Đoàn

FotoFlow has 50,000 users and six Memcached nodes in front of the database, picked the obvious way: server = hash(key) % 6. Last night one node died and 80 percent of keys remapped at once. Every remapped key missed, the stampede of reloads flattened the database, and the site was down for an hour. The placement scheme itself was the bug.

Bend the hash space into a ring

Hash values run from 0 to 2^32. Join the ends and you get a ring. Now hash each server's name too and place it on the ring at that point: not evenly spaced, just wherever the hash lands. Nothing here depends on how many servers there are. That is the whole point: stop tying placement to N.

To place a key, hash it onto the ring as well. A key belongs to the first server clockwise from it on the ring. Clients do this with a sorted list of server positions and a binary search: fast, and identical on every machine.

Adding or removing moves one arc

A seventh node joins at its hash position, between two existing servers. It takes over only the keys in the arc just counter-clockwise of it: the ones that used to walk past that spot to the next server. Adding a server moves only the keys in the arc just counter-clockwise of it: about 1/N of them, the minimum possible. Every other key stays exactly where it was. When a server dies, its keys simply continue clockwise to the next server, and nobody else is affected. Compare that with % N, where losing one server reshuffles almost everything.

This is how Amazon's Dynamo, Cassandra, and many caches and CDNs place data. Rendezvous and jump hashing are close cousins that get the same result without a ring.

Virtual nodes even it out

Random positions mean uneven arcs: with a few servers, luck alone can leave one owning twice the ring of another, and twice the traffic. The fix is virtual nodes: give each server several positions by hashing A#1, A#2, and so on, so it owns many small arcs instead of one big one. Virtual nodes give each server many small arcs, evening out shares and spreading the load when a server leaves. Real systems use 16 to a few hundred per server, and stronger servers get more.

One copy per key is not enough: a dead server would lose its data. So each key is stored on the first three distinct servers clockwise: its owner plus the next two. Store each key on the first N distinct servers clockwise, and a dead server loses nothing. Dynamo calls this the preference list.

The cache cluster, redone

Back to FotoFlow's six cache nodes, now on a ring. The seventh node is added and only about 20 percent of keys move. The cache stays mostly warm, the database barely notices, and a node dying at 3 a.m. is a non-event. This is where consistent hashing started: spreading web caches across machines that come and go.

What this does not solve

Placement is solved, but the rows still need ids, and FotoFlow's ids come from a database counter that only one shard owns. Generating unique ids without one central counter is the next post.

Your cache placement today: what percentage of keys move when you add one node?
Could any of your shards or caches switch to a ring without a data migration?

A ring turns a reshuffle into a sliver.

Next in the series: Five servers, one counter: how do you hand out unique IDs?

Sources

Wikipedia: Consistent hashing

Amazon Dynamo (All Things Distributed)

Designing Data-Intensive Applications

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.