SimuVedaSystem Design Lab Open in the lab

Design interviews / Intermediate / 6 min / Updated

Design a distributed cache: consistent hashing and hot keys

Add a server: hash mod N moves 80% of keys, a consistent hash ring about 1 in N. Virtual nodes even the load; replicas spread one hot key.

Start the simulation → Change the load, break a part, watch what happens. Free, no sign-up.

In short

How do you design a distributed cache?

A distributed cache spreads keys over servers; consistent hashing places servers and keys on a ring so changes move few keys.

Why does it matter?

Caches grow and shrink; with mod N one new server empties most of the cache and floods the database.

What is the trade-off?

Rings need virtual nodes to balance load; a single hot key still needs replicas or a local cache in front.

How do you explain it in an interview?

"Consistent hashing with 100-200 virtual nodes per server, replicas for hot keys, client-side near cache for the hottest reads."

Check yourself

You go from 4 to 5 cache servers with hash(key) mod N. About how many keys move?

Answer: About 80%. Almost every key maps to a new server, so the cache goes cold and the database takes the load.

With consistent hashing, which keys move when a server is added?

Answer: Only the keys on the arc the new server takes from its neighbour, about 1/N. Each key goes to the next server clockwise on the ring, so only keys between the new point and its predecessor change owner.

What problem do virtual nodes solve?

Answer: Uneven arcs: with one point per server some own far more of the ring. Many points per server average out the arc sizes, so each server holds close to 1/N of the keys.

See it happen in the simulation →

distributed cacheconsistent hashinghash ringvirtual nodesmemcachedredis clusterhot keysystem design interview

More in design interviews