Why Partition Data: Horizontal, Vertical and Sharding
Overview
A single database server has limits on storage, memory and how many requests it can handle. When your data or traffic grows past those limits, you split the data into smaller parts and put them on different machines.
This lesson explains the two ways to split data and what sharding means.
Detailed Notes
What is data partitioning
Data partitioning means dividing one large dataset into smaller pieces called partitions, so no single machine has to hold or serve everything.
- Vertical partitioning splits by columns. For example, a users table keeps
id, name, emailin one table and the largebio, photocolumns in another. - Horizontal partitioning splits by rows. Users 1 to 1 million go in one partition, the next million in another. Each partition has the same columns.
- Sharding is horizontal partitioning where each partition (a shard) lives on a different machine.
The problem it solves
Scaling up (a bigger server) eventually stops working: the biggest machine is limited and very expensive. Earlier you saw that scaling out, adding more machines, is the long-term answer. A database is harder to scale out than a web server because the data itself has to be divided. Partitioning does that: each machine stores and serves only a fraction of the data, so storage, reads and writes all grow by adding machines.
How it works
- Choose a shard key, a column used to decide where a row goes (for example
user_id). - A rule maps each key to a shard (the strategies come in the next lesson).
- The application, or a router in front of the databases, looks at the key and sends the query to the right shard.
If every query includes the shard key, each query touches only one shard. That is the goal.
user_id 42 -> rule -> Shard 2
user_id 7 -> rule -> Shard 1
Vertical partitioning helps when some columns are big or rarely used, but one machine still holds each table. Horizontal partitioning is what lets you grow without a ceiling.
Sharding has a price. Queries that span shards are harder, joins across shards are expensive, and your application becomes more complex. So shard only when you really need to. Try indexes, caching and a bigger machine first.
A simple example
A library with a million books and one librarian is overwhelmed. Split the books by author's last name: A-H, I-P and Q-Z in three buildings, each with its own librarian. If you know the author, you go to exactly one building.
System design example
A chat app has 500 million users and billions of messages. One database cannot store them or handle the traffic. The team shards by user_id across 20 database servers. To read one user's messages, the app computes which shard holds that user and queries only that machine. To grow, they add more servers. The same idea appears in nearly every large system design interview: "I would shard the data by X".
Summary
- Partitioning splits a large dataset into smaller pieces.
- Vertical splits by columns; horizontal splits by rows.
- Sharding is horizontal partitioning across machines.
- A shard key decides which shard owns a row.
- Sharding scales without a ceiling but adds complexity, so use it last.
Architecture Diagram
A router uses the shard key to pick one shard.
- 1A request arrives with a user id.
- 2The router applies the sharding rule to the key.
- 3The request goes to exactly one shard.
- 4That shard answers from its slice of the data.
Common Interview Questions
Finished this lesson?
0 of 91 lessons done · 0% of the course

