Sharding and Partitioning of Data: Scale Your Product Like a Pro ๐
Have you heard about sharding and partitioning of data when scaling your product?
When a product is scaled, millions of users may use it simultaneously. At that time, we need to maintain data consistency and ensure the product is much faster than beforeโbecause performance is directly affected.
Partitioning and sharding are two powerful methods that help fetch data faster from databases and improve performance under heavy load.
๐งฉ What is Partitioning?
Partitioning is the process of dividing a large dataset into smaller, manageable pieces. This division helps in improving query performance, scalability, and data organization.
There are several types of partitioning:
- Horizontal Partitioning
- Vertical Partitioning
- Range Partitioning
- Hash Partitioning
๐น Horizontal Partitioning
Horizontal partitioning divides the rows of a table into multiple smaller tables, typically across databases or servers. This is the most commonly used type and is the basis for sharding.
๐ Example: Horizontal Partitioning
Suppose you have a Customers
table:
CustomerID | Name | Country |
---|---|---|
1 | Alice | USA |
2 | Bob | USA |
3 | Charlie | Canada |
4 | David | UK |
5 | Eva | Canada |
Partition by Country:
USA Table
CustomerID | Name | Country |
---|---|---|
1 | Alice | USA |
2 | Bob | USA |
Canada Table
CustomerID | Name | Country |
---|---|---|
3 | Charlie | Canada |
5 | Eva | Canada |
UK Table
CustomerID | Name | Country |
---|---|---|
4 | David | UK |
๐ธ Vertical Partitioning
In vertical partitioning, we divide a table by columns instead of rows. Itโs helpful when frequently accessed and infrequently accessed data can be separated.
๐ Example: Vertical Partitioning
Suppose you have a Users
table:
UserID | Name | PasswordHash | ProfilePicture | Bio | |
---|---|---|---|---|---|
1 | Alice | alice@x.com | ******** | img1.jpg | Loves cats |
2 | Bob | bob@x.com | ******** | img2.jpg | Guitar player |
Authentication Table
UserID | PasswordHash | |
---|---|---|
1 | alice@x.com | ******** |
2 | bob@x.com | ******** |
Profile Table
UserID | Name | ProfilePicture | Bio |
---|---|---|---|
1 | Alice | img1.jpg | Loves cats |
2 | Bob | img2.jpg | Guitar player |
๐งฑ What is Sharding?
Sharding is a type of horizontal partitioning where data is split across multiple machines or databases (called shards). Each shard holds a subset of the data. Sharding is commonly used in distributed systems to:
- Reduce latency
- Improve fault tolerance
- Scale out horizontally
- Bring data closer to the user (geographical sharding)
โ๏ธ How Sharding Works
๐งฑ What is Sharding in Databases? (With Formula Explained)
When your application grows and millions of users start hitting your system, your database can become a performance bottleneck. Thatโs where sharding comes into play.
๐ What is Sharding?
Sharding is a technique used to split a large database into smaller, faster, and more manageable pieces, known as shards.
Each shard is a separate database that contains a subset of the overall data.
Sharding is a form of horizontal partitioning, and it helps:
- ๐ง Improve performance
- ๐ Scale out the system
- ๐ Reduce latency
- ๐ฅ Avoid a single point of failure
๐งฎ How Does Sharding Work?
To decide which shard a particular piece of data goes to, systems use a formula based on a shard key.
๐ The Sharding Formula:
text
shard = shard_key % n (n defines how many shards your system will use)
Top comments (0)