>_
EngineeringNotes
Back to DBMS Topics
Lecture 18

Partitioning & Sharding in DBMS

When datasets grow to petabyte scale and request rates skyrocket, traditional single-server systems and simple replication clusters break down. Database Partitioning & Sharding are critical database optimization techniques that divide giant tables into manageable slices distributed across multiple servers.

1

The Scaling Story: From Garage Startup to Instagram Scale

To understand why we need partitioning and sharding, let us follow the scaling journey of a fictional social media platform, InstaShare.

Phase 1: The Humble Garage Startup (Single Instance)

InstaShare launches with 1,000 active users. All data fits comfortably on a single relational database instance costing $10/month. Reads and writes are direct, consistent, and blazing fast (~10ms).

Phase 2: Going Viral & High Availability (Clustering)

InstaShare gains millions of users. The single database server chokes under read requests. The engineers implement Database Clustering (Master-Slave replication). Writes go to the Master, and reads are load-balanced across multiple Replicas (Slaves). This increases read throughput and guarantees high availability if a node fails.

Phase 3: The Petabyte Wall (Where Replication Fails)

InstaShare reaches 500 million active users generating 50 Terabytes of database logs, posts, and user profiles. Here, the engineering team hits a hard technical wall:

Astronomical Storage Costs

Because clustering replicates the entire dataset to every node, if we have 5 replicas to support read traffic, each node must purchase 50 TB of high-performance storage. Replicating 50 TB five times requires paying for 250 TB of expensive storage. This is NOT cost-effective!

Write Latency & Sync Lag

When millions of users upload images and post comments concurrently, the Master node must handle every single write operation. It must then propagate these writes to the replicas. The synchronization network overhead slows down write response times, creates massive replica lag, and causes major read-write consistency anomalies.

Phase 4: The Ultimate Solution (Partitioning & Sharding)

To scale beyond the petabyte limit, InstaShare divides its data:
1. Vertical Partitioning: Slices the User table columns so heavy blobs (profile pictures, bios) reside on slower, cheaper storage nodes, while login credentials reside on fast, optimized nodes.
2. Horizontal Partitioning & Sharding: Slices the User rows by geographical region (e.g., users in Asia on Server A, users in the US on Server B). Now, each server only handles a fraction of the total storage and traffic, offering infinite horizontal scaling!

Key Scaling Takeaway

Clustering provides high availability and scales read traffic by duplicating data, but it is limited by the storage capacity of a single server and incurs high sync overhead. Partitioning/Sharding optimizes scale by dividing and conquering, enabling databases to support billions of concurrent actions efficiently.

2

Understanding Database Partitioning

A big problem can be solved easily when it is chopped into several smaller sub-problems. That is what the partitioning technique does. It divides a big database containing data metrics and indexes into smaller and handy slices of data called partitions.

Once the database is partitioned, standard SQL queries can directly run against the partitioned tables without any alteration. The Data Definition Language (DDL) can easily work on these smaller slices, instead of handling the giant database altogether.

Partitioning is the technique used to divide stored database objects into separate physical storage spaces or servers. If applied to a database that is already scaled out (i.e. equipped with multiple servers), we can partition our database among those servers and handle big data easily.

3

Vertical vs. Horizontal Partitioning

The Original Dataset: Users Table
UserIDUsernameEmailPasswordHashBioProfilePictureURLCountry
101alice_99alice@mail.comf7a93c...Software Dev/pics/a99.jpgUSA
102dev_rahulrahul@mail.com92e44d...Writer & Artist/pics/rah.jpgIndia
103yuki_tokyoyuki@mail.coma02d41...Travel Blogger/pics/yuki.jpgJapan
104bob_smithbob@mail.comd028ff...Photographer/pics/bob.jpgUSA

Vertical Partitioning (Column-wise)

Slices the database relation vertically / column-wise. Columns that are accessed frequently are separated from larger columns that are rarely fetched.

Partition A (Auth & Identity Database) - Server 1
UserIDUsernameEmailPasswordHash
101alice_99alice@mail.comf7a93c...
102dev_rahulrahul@mail.com92e44d...
Partition B (Metadata & Blobs Database) - Server 2
UserIDBioProfilePictureURLCountry
101Software Dev/pics/a99.jpgUSA
102Writer & Artist/pics/rah.jpgIndia

⚠️ Drawback: Slicing vertically means we need to perform JOIN queries or access different physical servers/locations to reconstruct a complete tuple (all user info).

Horizontal Partitioning (Row-wise)

Slices the database relation horizontally / row-wise. Entire rows containing complete tuples are separated based on a specific logic (e.g. range, hash, list).

Partition 1 (Country: USA) - Americas Node
UserIDUsernameEmailCountry
101alice_99alice@mail.comUSA
104bob_smithbob@mail.comUSA
Partition 2 (Country: Asia) - Asia-Pacific Node
UserIDUsernameEmailCountry
102dev_rahulrahul@mail.comIndia
103yuki_tokyoyuki@mail.comJapan

💡 Advantage: Independent chunks of complete data tuples are stored on separate servers, making it easy to fetch whole rows instantly.

4

When is Partitioning Applied?

Unmanageable Datasets

The dataset becomes so massive that performing basic maintenance, queries, indexes, or updates on a single table becomes extremely tedious and memory-intensive.

High Request Latency

The rate of client requests is so large that accessing a single database server takes too much time, degrading overall system response times.

5

Key Advantages of Partitioning

Parallelism

Queries can scan multiple partitions in parallel, significantly boosting search speeds.

Availability

If one partition fails or undergoes maintenance, other partitions remain accessible to users.

Performance

Restricting scans to relevant partitions (Partition Pruning) prevents checking irrelevant data.

Manageability

Operations like rebuilding indexes, database backups, and loading logs are faster on small partitions.

Reduce Cost

Avoids costly vertical scaling (buying bigger hardware nodes) by scaling out onto multiple systems.

6

Distributed Database System

A Distributed Database is a single logical database that is spread across multiple locations (servers) and logically interconnected by a network.

The Distributed Database Recipe

A modern distributed database system is the end product of applying three essential database optimization mechanisms:

1. ClusteringHigh Availability via Full Replication
2. PartitioningSlicing tables into smaller logical chunks
3. ShardingDistributing slices across multiple servers
7

Sharding: Scaling Globally

Sharding is the actual technique used to implement Horizontal Partitioning.

The Core Idea & Routing Layer

Instead of having all data sit on one single database instance, we split the rows up and introduce a Routing Layer. The Routing Layer acts as a traffic director: it receives user requests, determines where the requested record sits based on a Shard Key, and forwards the query to the correct physical database node.

Pros of Sharding

  • Scale-Out (Infinite Scalability): Datasets are spread across multiple instances, enabling databases to scale linearly.
  • Fault Isolation (Availability): If Shard US fails, Shard EU and Shard Asia remain fully functional. Only a fraction of users are impacted.

Cons of Sharding

  • ⚙️ Operational Complexity: Implementing the routing layer, managing data mappings, and monitoring multiple shards increases database overhead.
  • 🔄 Re-Sharding & Data Imbalances: Non-uniform data growth may make certain shards hotspots, forcing costly data re-distribution.
  • 📊 Scatter-Gather Problem: Analytical queries spanning multiple user records require querying all shards in parallel and assembling results, which is highly inefficient.