Back to results for “Performance Optimization & Profiling”
Node.js › Databases

What is database sharding, and what problem does it solve that replication alone doesn't address?

Advanced

Answer

Sharding splits a single logical dataset horizontally across multiple separate database instances (shards), each holding a distinct, non-overlapping subset of the data (commonly partitioned by a shard key like customer ID) -- unlike replication, which copies the entire dataset to multiple servers to scale reads, sharding scales both read AND write capacity by distributing the total data volume and write load itself across many independent servers, addressing the case where a dataset has simply grown too large or write-heavy for any single server to handle.
// Simplified shard routing based on customer ID
function getShardForCustomer(customerId) {
  const shardIndex = customerId % NUM_SHARDS;
  return shardConnections[shardIndex];
}

const shard = getShardForCustomer(customerId);
await shard.query('INSERT INTO orders ...', [orderData]);
Real-world example A multi-tenant SaaS platform whose largest customers generated more write volume than a single database server could handle shards its orders table across four separate database instances by customer ID, distributing both the storage and write load across all four rather than concentrating it on one server.

Common follow-ups: What's the operational complexity cost of sharding compared to simply using a larger, more powerful single database server (vertical scaling)?;How do you handle a query that needs to aggregate data across multiple shards?