Free sample lesson ยท Advanced System Design and Architecture

Sharding Basics

What happens when a single database can no longer hold your growing data?

Lesson 1 of the Wondering course Advanced System Design and Architecture

When One Node Runs Out

A single database node has hard physical ceilings: CPU, RAM, and I/O bandwidth.

At a certain scale, upgrading hardware stops paying off. Vertical scaling hits diminishing returns and astronomical costs.

Sharding breaks the large database into smaller, more manageable parts called shards.

Two ways to grow
  • Vertical scaling
  • Sharding

The dataset stays one logical whole. The storage and load spread across many machines.

What a Shard Actually Is

A shard is a slice of one logical dataset living on its own physical database instance.

Applications still query what looks like a single dataset. A routing layer decides which shard holds the rows.

One logical dataset, many instances
  • Logical dataset: Appears as a single database to the application.
  • Shard A: Own physical instance with its own CPU, RAM, and I/O.
  • Shard B: Own physical instance with its own CPU, RAM, and I/O.
  • Shard C: Own physical instance with its own CPU, RAM, and I/O.

This is the transition from a monolithic database architecture to a distributed one.

Each shard owns its own CPU, RAM, and I/O budget.

Why Horizontal Scaling Wins

Horizontal scaling means adding machines instead of buying a bigger one.

Sharding is what makes that possible for stateful data. Load spreads across the fleet, so the architecture can support millions of concurrent users.

One overloaded node beside a sharded fleet handling the same traffic

Notice that the request volume is identical on both sides; only the number of machines absorbing it changes.

Capacity becomes something you add, not something you purchase in one expensive jump.

The Sharding Key

Every sharded system needs a rule that maps a row to a shard. That rule is the sharding key.

A well-defined sharding key is critical to preventing hot spots in production clusters.

How a row finds its shard
  • Request arrives: The application issues a read or write for a row.
  • Read key: The routing layer extracts the sharding key from the request.
  • Map to shard: The key determines which physical database instance owns that row.
  • Execute locally: The query runs on a single shard using only that node's CPU, RAM, and I/O.

It is the single design decision that determines whether data lands evenly across the fleet or piles onto one unlucky instance.

Hot Spots in Production

A hot spot is a shard receiving far more traffic than its peers.

When that happens, the cluster is only as fast as its busiest node. The other instances sit idle while one saturates its CPU and I/O bandwidth.

You have paid for horizontal scaling and gotten a single-node bottleneck back.

Can more shards make things worse?

Even distribution across the fleet is the whole point.

Cross-Shard Joins

Inside one node, a join is cheap. Across shards, it is a distributed operation.

The query layer must fetch partial results from multiple instances and combine them, paying network latency on every hop.

Joins before and after sharding
  • Single node join
  • Cross-shard join

This is one of the real trade-offs in complexity you accept when moving from a monolithic database to a distributed one.

Distributed Transactions

A write touching two shards is no longer a local commit.

It becomes a distributed transaction, coordinated across independent instances that can fail separately.

Senior design work is largely about avoiding this. Choose a sharding key that keeps related rows together so most transactions stay on one shard.

One shard or many?
  • Write arrives
  • Rows on one shard?: Does the sharding key keep the related rows together?
  • Local commit: The transaction stays on a single instance.
  • Distributed transaction: Coordination across independent instances that can fail separately.
  • Revisit sharding key: Regroup related rows to avoid repeated coordination cost.
  • Write complete
  • Pays latency
  • If frequent
  • Re-evaluate

When you cannot, you pay in coordination and latency.

The Trade-Off You Accept

Sharding buys throughput and capacity. It charges you in operational complexity.

You gain the ability to handle petabytes of data and millions of concurrent users. You lose cheap joins, simple transactions, and easy schema changes.

The sharding ledger
  • You gain
  • Petabyte-scale data: Capacity grows by adding instances to the fleet.
  • Millions of concurrent users: Load spreads instead of saturating one node.
  • Low latency at scale: Queries hit one shard's CPU and I/O, not everyone's.
  • You pay
  • Cross-shard joins: Partial results combined over the network.
  • Distributed transactions: Coordination across instances that fail separately.
  • Hot spot risk: A poor key concentrates traffic on one instance.

Name both columns when you defend a sharded design.

In an interview, naming both sides of that ledger matters more than describing the split itself.

Designing the Foundation

These basics are the foundation for globally distributed, high-availability systems.

The target is petabytes of data served with low latency for end users.

What rests on what
  • Low latency at scale: Petabytes served to end users without slowdown.
  • Global high availability: Distributed architecture across many instances.
  • Even distribution: No hot spots concentrating load on one node.
  • Sharding key: The rule mapping every row to a physical instance.

Each level rests on the one below.

Start every design from the sharding key. Even distribution first, then reason about the joins and transactions you have made expensive.

Check yourself

Sharding supports horizontal scaling by adding database machines instead of buying one increasingly larger machine.

  • True
  • False

Answer: True

This is correct. Sharding distributes one logical dataset across multiple physical instances, allowing capacity and throughput to grow by adding machines. Buying a larger machine is vertical scaling.

In a sharded database, the _____ directs queries, each _____ stores a dataset slice, and the _____ maps rows to shard locations.

Answer: In a sharded database, the routing layer directs queries, each shard stores a dataset slice, and the sharding key maps rows to shard locations.

The routing layer chooses where to send a query. A shard is one physical slice of the logical dataset. The sharding key determines where each row belongs.

In a sharded production cluster, one shard is saturated while the other shards are mostly idle. What is the most likely problem?

  • A hot spot caused by uneven data or traffic distribution
  • A cross-shard join that evenly uses every shard
  • Vertical scaling that added too many database machines
  • A local commit confined to the saturated shard

Answer: A hot spot caused by uneven data or traffic distribution

A hot spot occurs when one shard receives much more traffic than its peers. The busiest shard limits overall performance, even though other machines have unused capacity. An uneven sharding key can cause this pattern.

Keep going

This is lesson 1 of 11 in Advanced System Design and Architecture. Next up:

  1. Consistent Hashing
  2. Replication Strategies
  3. Service Mesh
  4. Resilience Patterns
  5. Message Queues

Start this course free

Wondering turns any topic into short lessons you can read, watch, or listen to, with practice questions and an AI tutor.