The Easiest
Consistent Hashing
charsyam@naver.com
Consistent Hashing?
WHY?
There was data
In the beginning.
1
2
3 4
5
6
7
89
How do you distribute
data pairly into 3 servers?
Considering the future.
1
2
3
4
5
6
7
8
9
Sequence
1 2 3
4 5 6
7 8 9
Modular
If you add one server, or
remove one server
What happened?
1
2
3
4
5
6
Add one server for Sequence
7
8
9
Add one server for Modular
1 2 3
4 5 6
7 8 9
How do you redistribute
these data?
Redistribute by Modular
1 2 3 4
5 6 7 8
9
Most of data is
redistributed!
Redistribution is
Burden.
What is a good way
to reduce redistribution?
Consistent Hashing
Can Do!!!
Consistent Hashing
redistribute
only N/K data
N = data size K = servers
Key Concept is
Hash
What is main
concept of hash
If you use same hash
function?
The result is always the same.
hash(“abc”) = 1
hash(“abc1”) = 2
hash(“abc”) = 1
You have 3 servers.
10.0.1.1
10.0.1.2
10.0.1.3
There is a hash function.
y = hash(x)
You hash 3 servers
hash(“10.0.1.1”) = 100
hash(“10.0.1.2”) = 400
hash(“10.0.1.3”) = 700
Just Giving server address as key
Just define a rule.
We will store a key in
hash(key) is higher and
the nearest one.
hash(key) Hash value
hash(“10.0.1.1”) 100
hash(“10.0.1.2”) 400
hash(“10.0.1.3”) 700
There is key “redis”
hash(“redis”) = 200
Where we store it?
hash(key) Hash value
hash(“10.0.1.1”) 100
hash(“redis”) 200 in hash(“10.0.1.2”)
hash(“10.0.1.2”) 400
hash(“10.0.1.3”) 700
There is key “charsyam”
hash(“charsyam”) = 450
Where we store it?
hash(key) Hash value
hash(“10.0.1.1”) 100
hash(“redis”) 200 in hash(“10.0.1.2”)
hash(“10.0.1.2”) 400
hash(“charsyam”) 450 in hash(“10.0.1.3”)
hash(“10.0.1.3”) 700
There is key “udemy”
hash(“udemy”) = 50
Where we store it?
hash(key) Hash value
hash(“udemy”) 50 in hash(“10.0.1.1”)
hash(“10.0.1.1”) 100
hash(“redis”) 200 in hash(“10.0.1.2”)
hash(“10.0.1.2”) 400
hash(“charsyam”) 450 in hash(“10.0.1.3”)
hash(“10.0.1.3”) 700
There is key “web”
hash(“web”) = 1000
Where we store it?
There is no server has
higher hash value 1000.
Where we can store it?
Think it is Circle
hash(key) Hash value
hash(“udemy”) 50 in hash(“10.0.1.1”)
hash(“10.0.1.1”) 100
hash(“redis”) 200 in hash(“10.0.1.2”)
hash(“10.0.1.2”) 400
hash(“charsyam”) 450 in hash(“10.0.1.3”)
hash(“10.0.1.3”) 700
hash(“web”) 1000 in hash(“10.0.1.1”)
Key “web” is stored in
First Server.
If we add new server
It is “10.0.1.4”.
And hash(“10.0.1.4”) = 500
hash(key) Hash value
hash(“udemy”) 50 in hash(“10.0.1.1”)
hash(“10.0.1.1”) 100
hash(“redis”) 200 in hash(“10.0.1.2”)
hash(“10.0.1.2”) 400
DELETED hash(“charsyam”) 450
hash(“10.0.1.4”) 500
hash(“10.0.1.3”) 700
hash(“web”) 1000 in hash(“10.0.1.1”)
After adding “10.0.1.4”
Key “charsyam” is
missing
But other keys are
never changed.
You can still find key
“udemy” in “10.0.1.1”
There is key “charsyam”
hash(“charsyam”) = 450
Where we store it?
hash(key) Hash value
hash(“udemy”) 50 in hash(“10.0.1.1”)
hash(“10.0.1.1”) 100
hash(“redis”) 200 in hash(“10.0.1.2”)
hash(“10.0.1.2”) 400
hash(“charsyam”) 450 in hash(“10.0.1.4”)
hash(“10.0.1.4”) 500
hash(“10.0.1.3”) 700
hash(“web”) 1000 in hash(“10.0.1.1”)
A
Add A Server
A
B
Add B Server
A
BC
Add C Server
A
BC
Add Key 1
1
A
BC
Add Key 2
1
2
A
BC
Add Key 3
1
2
3
A
BC
Add Key 4
1
2
3
4
A
BC
Add Key 5
1
2
3
4
5
A
C
Fail B Server
2
3
4
5
A
C
Add Key 1
2
3
4
5
1
In Next Lecture
● We will discuss belows topics
○ How to use Consistent Hashing in Real World.

The easiest consistent hashing