0% found this document useful (0 votes)
23 views

Sharding

Sharding is splitting a database across multiple database servers to improve scalability and performance. Consistent hashing is a technique used to determine which database server a record belongs to. Sharding splits a database table across multiple servers, while horizontal partitioning splits a table into multiple tables within the same database server. The document provides an example of sharding a URL shortener database across 3 Postgres servers and discusses pros like scalability and cons like complexity.

Uploaded by

pvb55998
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Sharding

Sharding is splitting a database across multiple database servers to improve scalability and performance. Consistent hashing is a technique used to determine which database server a record belongs to. Sharding splits a database table across multiple servers, while horizontal partitioning splits a table into multiple tables within the same database server. The document provides an example of sharding a URL shortener database across 3 Postgres servers and discusses pros like scalability and cons like complexity.

Uploaded by

pvb55998
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

husseinnasser.

com

Sharding

Sharding & Consistent Hashing


Agenda

● 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

● HP splits big table into multiple tables in the same


database
● Sharding splits big table into multiple tables across
multiple database servers
● HP table name changes (or schema)
● Sharding everything is the same but server changes
Example Code with Postgres (Url shortener)

● Spin up 3 postgres instances with identical schema


○ 5432, 5433, 5434
● Write to the sharded databases.
● Reads from the sharded databases.
Pros of Sharding
● Scalability
○ Data
○ Memory
● Security (users can access certain shards)
● Optimal and Smaller index size
Cons of Sharding
● Complex client (aware of the shard)
● Transactions across shards problem
● Rollbacks
● Schema changes are hard
● Joins
● Has to be something you know in the query
Summary

● What is sharding?
● Consistent Hashing
● Horizontal Partitioning vs Sharding
● Example (Code with Postgres)
● Pros & Cons

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy