>_
EngineeringNotes
← Back to System Design

Sharding & Partitioning

When a single database can no longer scale vertically or hold all your data, it's time to split it up. Discover the roadmap from a single node to high-throughput distributed systems.

Scaling the Unscalable: What exactly is a Database?

In reality, a database is just a process running on a server (like an EC2 instance).

  • Exposes a Port (e.g., 3306) for users to communicate via queries.
  • Uses the Local Disk of the virtual server to persist data durably.

"We represent it with a cylinder symbol, but it's fundamentally a process coupled with high-performance storage."

API Server
MySQL DB
PORT: 3306
CPU
RAM
Disk
100 WRITES/SEC

The Evolution: From 100 to 1000 Writes/sec

As your product goes viral, your single small server will start gasping for air. Here is how we evolve.

Vertical Scaling

The first instinct: Scale Up. Give the database more CPU, more RAM, and more Disk. The process stays the same, but the hardware bucket grows.

PRO: NO CODE CHANGESJust a simple configuration update or instance type change. Your application logic remains untouched.
CON: EXPONENTIAL COSTDoubling performance often triples or quadruples the hourly cost on cloud providers.
T2.MICRO
R5.4XLARGE
200 WRITES/SEC REACHED
Hardware Limitation: You eventually hit the ceiling. AWS/Physical hardware has a max limit (e.g., 128 vCPUs). You can't grow forever.

Replica Sets & HA

Relying on a single master (Read Replica model) creates a Single Point of Failure. If the master dies, your entire system goes read-only or crashes.

A Replica Set is a group of nodes that maintain the exact same data set. One node is elected as the Primary, while others are Secondaries.

READ PERFORMANCE

Secondaries can serve read requests, significantly boosting your system's read throughput.

PRIMARY NODEWrites + Reads
SECONDARY 1
SECONDARY 2
Failover Mechanism:If Primary dies, an election occurs. Secondary 1 becomes the new Primary automatically.

The Sharding Solution

When vertical scaling hits its limit, we Scale Out. We split the data across multiple identical servers.

Shard (The Physical)

A physical server instance added to the system infrastructure. We 'shard' the database.

Partition (The Logical)

The act of splitting the data itself into mutually exclusive subsets. We 'partition' the data.

Target: 1500 Writes/sec
Limit: 1000 Writes/sec
SHARD A
50% DATA
750 WPW
S P L I T
SHARD B
50% DATA
750 WPW

Combined Capacity: 2000 Writes/sec

How to Partition the Data?

Deciding which data goes where isn't random. It follows a Deterministic Logic based on how your system handles traffic and storage.

The goal is to avoid Hot Shards — situations where one server is overwhelmed while others sit idle. This is achieved by picking a partition key that distributes data uniformly.

Criteria 1Current Load
Criteria 2Use Case
Criteria 3Access Pattern

1. Horizontal

Splitting by Rows. Each shard holds full objects for a subset of IDs.

SELECT * FROM users WHERE id % 2 == 0;

2. Vertical

Splitting by Columns. Sensitive or rarely accessed attributes move to a separate shard.

Shard A: [id, email]
Shard B: [id, metadata]
Architectural Split
HORIZONTAL (ROW)
Vertical (Column)
100GB Dataset5 Partitions / 2 Shards
Shard Alpha
P120GB
P225GB
P315GB
Shard Beta
P418GB
P522GB

"If P5 becomes too hot, we can move it from Shard Beta to another shard."

The Partitioning Rule

A good partition key should have High Cardinality. For example, partitioning by UserID is better than partitioning by Status, which might only have a few values, causing massive data skew as your system grows.

When to Shard vs Partition?

These concepts are often used interchangeably, but their combinations define your architecture's capability.

Sharding \ Partitioning
NOYES
NO
Standard DB (Day 0)

Single process, single data set. Limited scale.

Logical Partitions

Splitting data within same server (e.g., Table partitioning).

YES
Read Replicas

Multiple physical servers, but NO data splitting. Full replication.

Full Sharding

Physical server splitting + Data partitioning. Massive scale.

Advantages

  • Higher Throughput: Halve the load per server and handle twice the traffic.

  • Increased Storage: Bypass physical disk limits (e.g., 100TB) by spreading data across nodes.

  • High Availability: If one shard goes down, only a portion of your users are affected.

Disadvantages

  • Operational Complexity: Managing replication lags, consistent hashing, and rebalancing is hard.

  • Expensive Cross-Shard Queries: Joining tables across shards is slow and often not natively possible.

  • Rebalancing Costs: Moving a "hot" partition to another shard is a heavy background task.

Ready to design for billions?

Sharding isn't just about splitting tables. It's about designing access patterns that ensure most queries hit exactly one shard.