Sharding
Sharding
com
Sharding
● What is sharding?
● Consistent Hashing
● Horizontal Partitioning vs Sharding
● Example (Code with Postgres)
● Pros & Cons
● Summary
URL shortener table with 1
What is Sharding? million pages
URL_TABLE
id Url urlid*
1 https://www.canva. 5FTOJ
com/design/DADr
SELECT URL SCuKg4I/5sKekxV
FROM URL_TABLE dctoGGq7Ri9O5G
Q/edit
WHERE URLID = “5FTOJ“
2 https://en.wikipedi CeG0z
a.org/wiki/Shard_(
database_architec
ture)#Database_ar
chitecture
.. …. ….
.. …. ….
1M https://www.quora. J9COp
com/How-does-ba
se64-encoding-wo
rk
What is Sharding? 200k
S1
200k
SELECT URL S2
FROM URL_TABLE
Which database server is
WHERE URLID 5FTOJ in?
= “5FTOJ“ Split 1 million rows
Server 3!
200k table into 5 database
S3 instances.. Same
schema
200k
S4
200k
S5
Consistent Hashing
postgres:5432
Hash(“Input3”)
Hash(“Input1”)
Hash(“Input2”)
postgres:5434 5434
5432
5433 postgres:5433
Consistent Hashing
postgres:5432
Hash(“Input2”)
Hash(“Input3”)
postgres:5434 5433
5434
postgres:5433
Consistent Hashing
postgres:5432
num(“Input2”) % 3
postgres:5434 1 postgres:5433
+5432
=5433
Horizontal Partitioning vs Sharding
● What is sharding?
● Consistent Hashing
● Horizontal Partitioning vs Sharding
● Example (Code with Postgres)
● Pros & Cons