https://www.youtube.com/watch?v=RKX0PpFn_Bg
You have a database on one server. A few hundred users, everything's fast. Then your app takes off. The server's maxed out. So you add more servers β but now which server stores which data?
The obvious answer: hash(key) % num_servers . Clean. Simple. Until you add a fourth server and nearly every key maps to a different machine. Catastrophe.
The Hash Ring
Instead of a line, imagine a circle. Each server gets hashed to a position on this ring. Each key also gets hashed to a position. To find which server owns a key, walk clockwise until you hit a server.
When you add a new server, it lands somewhere on the ring, and only the keys between it and its predecessor need to move . Everything else stays put. Instead of reshuffling 90% of your data, you move maybe 10%.
But with just 3 servers, the ring divides unevenly. The fix? Virtual nodes β place each server at 150 positions scattered around the ring.β¦