keepcalculator

06 Numbers and Powers

Database Sharding: A Plain-English Primer

Card No. 06·62103 · Updated · Standard formula, shown below

Database sharding is a horizontal scaling technique that splits a large database into smaller, faster, more manageable pieces called shards, each holding a subset of the data. You distribute these shards across multiple servers, so no single machine bears the full load. This primer explains how sharding works, when to use it, and the trade-offs you must accept.

Why sharding exists

Every database has limits. A single server has finite CPU, memory, and disk I/O. As your data grows and your query volume increases, you eventually hit a ceiling. You can scale vertically by buying a bigger server, but that gets expensive and still has a hard limit. Sharding scales horizontally: instead of one giant database, you run many smaller databases that together hold all your data. Each shard has the same schema but different rows, and together they answer queries that a single machine could not handle.

How sharding works

The core idea is partitioning data by a shard key — a column or set of columns that determines which shard a row belongs to. For example, a user database might shard on user_id. A common approach is hash-based sharding: you compute a hash of the shard key and map the result to a shard number. If you have 4 shards, shard = hash(user_id) % 4. This distributes rows roughly evenly, but it makes range queries (e.g., “all users with IDs between 1000 and 2000”) difficult because the IDs are scattered across shards.

Range-based sharding assigns contiguous ranges of the shard key to each shard. For example, users with IDs 1–1000 go to shard A, 1001–2000 to shard B, and so on. This supports efficient range queries, but it can lead to uneven load if one range is much more active than others (a “hot spot”).

Directory-based sharding uses a lookup table that maps each shard key to a shard. This gives you the most flexibility — you can move data between shards without changing the key — but the lookup table becomes a single point of failure and a performance bottleneck.

Choosing a shard key

The shard key is the most important decision you will make. A good key has three properties:

  • High cardinality – many distinct values, so data spreads evenly.
  • Even distribution – no single value (or small set of values) dominates traffic.
  • Query alignment – your most common queries should filter by the shard key, so the database can route the query to only one shard.

For example, in a multi-tenant SaaS app, sharding by tenant_id works well if tenants are roughly equal in size. In a messaging app, sharding by user_id lets you keep all of a user’s messages on one shard, making it easy to fetch their chat history. Avoid shard keys that are monotonically increasing (like an auto-increment ID) with range-based sharding, because new writes will all go to the last shard, creating a hot spot.

Common sharding problems

Sharding adds complexity that you must manage. Here are the most frequent issues:

  • Cross-shard queries – If a query needs data from multiple shards, you must query each shard and merge results in the application. This is slower and more complex than a single query.
  • Distributed transactions – Transactions that span shards are hard to implement and often require two-phase commit, which is slow and fragile. Many systems avoid them by designing the shard key so that related data lives on the same shard.
  • Resharding – When you need to add more shards (or rebalance data), you must move data between servers. This is a complex, error-prone operation that often requires downtime or careful orchestration.
  • Hot spots – Even with a good key, some shards can become overloaded. For example, a celebrity user might generate far more traffic than others, overloading the shard that holds their data.

Sharding vs. other scaling options

Sharding is not the only way to scale. Before you shard, consider these alternatives:

  • Vertical scaling – Upgrade to a bigger server. Simple, but has a hard limit and becomes expensive.
  • Replication – Copy the entire database to multiple servers. Reads can be distributed, but writes still go to a single primary. This helps with read-heavy workloads but not with write-heavy ones.
  • Caching – Use Redis or Memcached to serve frequent reads without hitting the database. This can dramatically reduce load, but it does not reduce storage requirements.
  • Partitioning within a single database – Some databases (e.g., PostgreSQL, MySQL) support table partitioning, which splits a table into smaller physical segments on the same server. This can improve performance and manageability without the complexity of distributed sharding.

Sharding is a last resort after you have exhausted these options, because it introduces permanent operational overhead.

Costs and trade-offs

Sharding gives you scalability, but you pay for it in several ways:

  • Operational complexity – You now manage multiple database servers, backups, monitoring, and upgrades for each.
  • Application changes – Your application code must be shard-aware. It needs to know which shard holds a given piece of data, and it must handle cross-shard logic.
  • Data integrity – Enforcing foreign keys across shards is difficult or impossible. You may need to denormalize data or accept eventual consistency.
  • Analytics – Running analytical queries across all shards is challenging. You often need a separate data warehouse that aggregates data from all shards.

When to shard

Sharding is a significant undertaking. You should consider it only when:

  • Your data size exceeds the capacity of a single server, and you cannot delete or archive old data.
  • Your write throughput exceeds what a single primary can handle, even with replication.
  • You have a clear shard key that aligns with your most common queries.
  • You have the engineering resources to build and maintain a distributed system.

Many teams start with a single database, add caching, then move to replication, and only shard when they absolutely must. If you are unsure, consider using a managed database service that offers sharding as a feature, so the vendor handles some of the complexity.

Frequently Asked Questions

What is the difference between sharding and partitioning?

Partitioning usually refers to splitting a table within a single database server (e.g., by date range). Sharding is a form of partitioning that spans multiple servers. All sharding is partitioning, but not all partitioning is sharding.

Can I shard an existing database without downtime?

It is possible but difficult. You typically need to copy data to new shards while the old database continues serving traffic, then switch over. Tools like pgloader or custom scripts can help, but you must carefully handle writes that happen during the migration. Many teams schedule downtime for a sharding migration to avoid data loss.

What happens if a shard goes down?

Only the data on that shard becomes unavailable. The rest of the system continues to work. To reduce the impact, you can replicate each shard (e.g., with a primary and replica), so if one server fails, its replica takes over. Without replication, a shard failure means partial data loss or downtime.

How do I choose the number of shards?

Start with an estimate of your total data size and growth rate. Divide by the capacity of a single server you plan to use. For example, if you expect 100 GB of data and each server can handle 25 GB, you need at least 4 shards. It is wise to plan for growth — adding shards later is hard, so over-provision slightly at the start.

Is sharding the same as using a NoSQL database?

No. Many NoSQL databases (like MongoDB, Cassandra, and DynamoDB) support sharding natively, but sharding is a technique that can be applied to relational databases as well. The choice between SQL and NoSQL is separate from the choice to shard.

What is the best shard key for a user table?

There is no universal answer. A common choice is user_id if it is a random or hashed value. If users belong to organizations, organization_id might be better so that all users in an organization are on the same shard. The key should match how you query the data most often.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →