Learn consistent hashing, its problems, solutions, and relevance in system design interviews with clear examples and practical insights.
Ask about this video. Answers come from its transcript only — with the timestamp, so you can check them.
Generated from the transcript and can be wrong — check the timestamp.
Key Takeaways
- Consistent hashing minimizes data redistribution during scaling events.
- Hash rings and virtual nodes ensure even data distribution and fault tolerance.
- Traditional modulo hashing is inefficient for dynamic distributed systems.
- Consistent hashing is a critical concept for designing scalable distributed systems.
- Understanding consistent hashing is valuable for system design interviews involving distributed components.
What the video covers
- Consistent hashing solves the problem of data redistribution when scaling distributed databases.
- Traditional hashing with modulo causes massive data reshuffling when adding or removing servers.
- Consistent hashing uses a hash ring to evenly distribute data across servers with minimal redistribution.
- Databases are placed on the hash ring, and data is assigned by walking clockwise to the nearest database.
- Adding or removing a database only affects data in a small range on the ring, reducing overhead.
- Virtual nodes improve load balancing by assigning multiple points on the ring to each database.
- This approach prevents uneven data distribution when nodes are removed or added.
- Consistent hashing is widely used in systems like Redis, Cassandra, DynamoDB, and CDNs.
- In interviews, deep knowledge of consistent hashing is important when designing distributed caches, databases, or message queues.
- Hello Interview offers more free resources to prepare for software engineering system design interviews.
Full Transcript — Download SRT & Markdown
Speaker A
Let's talk about consistent hashing, including the problem it solves, how it works, and when it might come up in a system design interview. Now, consistent hashing is easiest learned via an example, so imagine we host a simple events website, something like Ticketmaster. It starts really small, so we only need a single server and a single database that stores all of our event information. Now, as the site becomes more popular, we have way too many events to store on that single database, so we add two additional databases. But there's a problem: for each given event, how do we know which database that event should be stored in? Is it stored in one, two, or three? We certainly don't want to have to query all three databases every time a client requests some information for a given event simply because we don't know which database it's actually stored in. One approach, as shown here, is to hash the event ID with some hash function. This can be an MD5, Murmur, whatever it may be, and this will give us some large number. We can then take that large number and mod it by the number of database servers that we have, in this case three. That will return a number between one and three, indicating which database that event should live on. And so, in our case, event 1234, when hashed, is 67211 mod 3 equals 2, and so it should be stored on database 2. This hash with modulo approach worked great to start, but the site continued to grow, and when we went to add a fourth database, we ran into a really big problem. See, by changing the number of database servers that we had, we've also now changed the modulo in the function. It's gone from mod 3 to mod 4. The issue with this is that this means almost all data now needs to be redistributed, not just data that should be in our new database. For this example, it makes that really clear. So if you remember before, event 1234 existed on database 2 because we had this function where we modded by three. Now, if we instead run this with the new setup, modding by four, now it should exist on database 3, and so that means event 1234 would need to be moved from database 2 to database 3. And 1234 isn't alone; it's not an isolated incident. Almost every single event in our database needs to be moved or redistributed. This redistribution is really bad. It creates a surge in database activity, and this can slow down or, in some cases, even crash our site. The problem didn't stop there either. Let's say that we needed to decommission database 3. Maybe it was getting old, so we would need to move the data from database 3 over to both one and two respectively. But since our modulo has changed again, we've not only redistributed all of the data from database 3, but we also redistributed all of the data once more. And so, looking back at our example, event 1234, which was on database 2, in theory shouldn't need to move, but now it's moved to database 1. Okay, so now that we understand the problem, let's go ahead and introduce the solution: consistent hashing. And so, consistent hashing consists of three steps. The first step is that we create what's called a hash ring that has a fixed number of points. And so, to keep it simple, we've illustrated here just 0 to 100. Now, in reality, this is 0 to 2^32, basically the full integer space, but the concept is entirely the same, so we'll stick with 0 to 100. Now, we then evenly distribute our databases across this hash ring. So, in our case, we have four databases. We could point them at 1, 25, 50, and 75 respectively. And now, in order to know which database a particular event should be stored on, we first hash the ID just like we did before. Imagine that event number two hashed is 16. We then find that point on our ring. 16 is here, and we walk clockwise until we hit a database. So, in this case, we hit database 2, and we're going to store event two on database number two. Now, this might seem obvious, but this ring isn't physical, of course. It's just the mathematical construct that's programmed into your code. But let's take a look at how this solved our problems from earlier. And so, if we want to go ahead and add our fifth database like we did before, look at what happens. Let's say we add that database, and we put it at 0 90 on the ring. We could put it anywhere, but we'll say 90. Now, the only events that need to be distributed are any events that hashed to the range 75 and 90. These are events that were previously on database 1, and they now need to be on database 5, but all other events stay exactly where they were. There's no longer this mass redistribution. Now, the exact same thing is true when we try to remove a database as well. And so, in this case, we can remove database 2, and only events that hash to a spot on the ring between 0 and 25, like event 2, which hashed to 16 before, need to be moved. They were previously on database 2, and now they need to be moved over to database 3. Okay, so we've solved most of our problems up until this point, but there's one last thing to take care of. And so, ideally, if we remove database 2, we wouldn't store all of the data from database 2 on database 3 as we're doing now. This means that database 3 has twice the amount of data as database 1 and database 4. So the question becomes, how do we make sure that the data is more evenly distributed when a database exits the ring? And the solution is something called virtual nodes. And so, instead of putting each database at just one point on the ring, we can put it at multiple points. And so, for example, if you take database 1, we don't only put it at position zero. We can also put it at position 20, 40, 60, and 80. For database 2, we don't only put it at 25. We can also put it at 5, 45, 65, and 85, and so on for each of the other databases. And so, what this means is that now, if database 2 is removed, instead of all the data, all the events that hashed to this point on the ring going to database 3, some of them are going to go to database 3, those between 0 and 10, but between 10 and 15, they're going to go to database 4, and between 15 and 20, they'll go to database 1, and 20 to 25 will end up falling on database 3. And so, as you can see, it ends up being a much more even redistribution. So now you know what consistent hashing is, but when does it come up in a system design interview? The reality is most of your favorite services use it behind the scenes to scale. Each of Redis, Cassandra, DynamoDB, most CDNs, and many more all use consistent hashing. Now, in an interview, you might make a nod to this when you introduce any of these technologies, but the only time you're really going to need to go deep into describing the algorithm is if you're designing a single scaled backend component, something like design and distributed cache, design and distributed database, design and distributed message queue, or so on. Lastly, if you really like this video and you want more free content to help you prepare for your software engineering interviews, head over to hellointerview.com. We have everything you need over there. Good luck with your interviews. Bye-bye.
Speaker A
something like Ticket Master it starts really small so we only need a single server and a single database that stores all of our event information now as the site becomes more popular we have way too many events to store on that single
Speaker A
database so we add two additional databases but there's a problem for each given event how do we know which database that event should be stored in is it stored in one 2 or three we certainly don't want to have to query
Speaker A
all three databases every time a client requests some information for a given event simply because we don't know which database it's actually stored in one approach as shown here is to Hash the event ID with some hash function this
Speaker A
can be an md5 murmur whatever it may be and this will give us some large number we can then take that large number and mod it by the number of database servers that we have in this case three that
Speaker A
will return a number between 1 and three indicating which database that event should live on and so in our case event 1 2 3 4 when hashed is 67211 mod 3 equal 2 and so it should be stored on database
Speaker A
2 this hash with modulo approach worked great to start but the site continued to grow and when we went to add a fourth database We R ran into a really big problem see by changing the number of database servers that we had we've also
Speaker A
now changed the modulo in the function it's gone from Mod 3 to mod 4 the issue with this is that this means almost all data now needs to be redistributed not just data that should be in our new
Speaker A
database for this example makes that really clear so if you remember before event 1 2 3 4 existed on database 2 because we had this function where we modded by three now if we instead run this by uh with the new setup modding by
Speaker A
four now it should exist on database 3 and so that means event 1 2 3 4 would need to be move from database 2 to database 3 and 1 12 3 4 isn't alone it's not an isolated incident almost every
Speaker A
single event in our database needs to be moved or redistributed the this redistribution is really bad it creates a surge in database activity and this can slow down or in some cases even crash our site the problem didn't stop
Speaker A
there either let's say that we needed to decommission database 3 maybe it was getting old so we would need to move the data from database 3 over to both one and two respectively but since our modulo has changed again we've not only
Speaker A
redistributed all of the data from database 3 but we also redistributed all of the data once more and so looking back at our example uh event 1 2 3 4 which was on database 2 in theory shouldn't need to move but now it's
Speaker A
moved to database 1 okay so now that we understand the problem let's go ahead and introduce the solution consistent hashing and so consistent hashing uh consists of three steps the first step is that we create what's called a hash ring that has a
Speaker A
fixed number of points and so to keep it simple we've Illustrated here just 0 to 100 now in reality this is 0 to 2 32 basically the full integer space uh but the concept is entirely the same so
Speaker A
we'll stick with 0 to 100 now we then evenly distrib our databases across this hash ring so in our case we have four databases we could Point them at 1 25 50 and 75 respectively and now in order to know
Speaker A
which database a particular event should be stored on we first hash the ID just like we did before imagine that event number two hashed is 16 we then find that point on our ring 16 is here and we
Speaker A
walk clockwise until we hit a database so in this case we hit database 2 and we're going to store event two on database number two now this might seem obvious but this ring isn't physical of course it's just the mathematical
Speaker A
construct that's programmed into your code um but let's take a look at how this solved our problems from earlier and so if we want to go ahead and add our fifth database like we did before look at what happens let's say we add
Speaker A
that database and we put it at 0 90 on the ring we could put it anywhere but we'll say 90 now the only events that need to be distributed are any events that hashed to the range 75 and 90 these
Speaker A
are events that were previously on database 1 and they now need to be on database 5 but all other events stay exactly where they were there's no longer this Mass redistribution now the exact same thing is true when we try to remove a database
Speaker A
as well and so in this case we can remove database 2 and only events that hash to uh a spot on the ring between 0 and 2 5 like event 2 which hashed to 16 before need to be moved they were
Speaker A
previously on database 2 and now they need to be moved over to database 3 Okay so we've solved for most of our problems up until this point but there's one last thing to take care of and so ideally if we remov database 2 we
Speaker A
wouldn't store all of the data from database 2 on database 3 as we're doing now this means that database 3 has 2x the amount of data as database 1 and database 4 so the question becomes how do we make sure that the data is more
Speaker A
evenly distributed when a database exits the ring and the solution is something called virtual nodes and so instead of putting each database at just one point on the ring we can put it at multiple points and so for example if you take
Speaker A
database 1 we don't only put it at position zero we can also put it at position 20 and 40 and 60 and 80 for database 2 we don't only put it at 25 we can also put it at 5 and 45 65 and 85
Speaker A
and so on for each of the other databases and so what this means is that now if database 2 is removed instead of all the data all the events that hashed to this point on the ring going to
Speaker A
database 3 some of them are going to go to database 3 those between 0 and 10 but between 10 and 15 they're going to go to database 4 and between 15 and 20 they'll go to database 1 and 20 to 25 will end
Speaker A
up falling on database 3 and so as you can see it ends up being a much more even redistribution so now you know what consistent hashing is but when does it come up in a system design interview the
Speaker A
reality is most of your favorite services use it behind the scenes to scale each of redus Cassandra datam modb uh most CDN and many more all use consistent hashing now in an interview you might make a nod to this when you
Speaker A
introduce any of these Technologies but the only time you're really going to need to go deep into describing the algorithm is if you're designing a single scaled backend component something like design and distributed cache design and distributed database uh
Speaker A
design and distributed message CU or so on uh lastly if you really like this video and you want more free content to help you prepare for your software engineering interviews head over to hello interview.com we have everything you need over there good luck with your
Speaker A
interviews bye-bye
Topics:consistent hashingsystem design interviewdistributed databasehash ringvirtual nodesdata distributionscalabilityredistribution problemdistributed cachesoftware engineering interview











