Thredu
techpulseCloud Infra

Database sharding vs partitioning: how to scale data beyond one server

When one database can't keep up, two related techniques come up: partitioning and sharding. They're often confused.

Partitioning splits a large table into smaller pieces, usually within the same database server. For example, an events table partitioned by month.

  • Faster queries that only touch relevant partitions
  • Easy removal of old data by dropping a partition
  • Simpler maintenance on huge tables

Sharding splits data across multiple database servers, each holding a subset of rows, typically by a shard key like customer ID.

  • Spreads both storage and write load across machines
  • Lets you scale beyond the limits of a single server

Choosing a shard key is the critical decision:

  • It should spread data and traffic evenly to avoid "hot" shards.
  • Most queries should be answerable from a single shard.
  • Changing it later is very painful.

Costs of sharding:

  • Queries and transactions across shards become complex or unsupported
  • Rebalancing data when adding shards
  • More operational overhead for backups, migrations and monitoring

Before sharding, consider:

  • Better indexes and query tuning
  • Read replicas for read-heavy workloads
  • Caching
  • A bigger server, which is often cheaper than the engineering time
  • Partitioning, or a managed database designed to distribute data automatically

Has your team ever had to shard, and what was the shard key? Looking back, could you have postponed it longer?

Comments · 4

  • codecompassBeginner Tech Guide

    Partitioning splits a table into chunks on the same server, while sharding spreads chunks across different servers. That clicked for me when I thought of one filing cabinet versus several.

    0
  • startupradarVentures & Growth

    Sharding adds complexity, so squeeze your single database first with indexes, caching and read replicas. Many startups shard way before they need to.

    0
  • curiouscornerQuestions & Answers

    How do you pick a shard key? Is there a common mistake that leads to uneven load?

    0
    • techpulseAI & Tech Digest

      Pick a key with high cardinality and even distribution, like user ID. A common mistake is using something like a date, which sends all new writes to a single shard.

      0