Medium system-design fundamentals

How do you shard data, and how do you choose a shard key?

The 30 second answer

Sharding splits one dataset across several databases by a key, so each node holds and serves only part of it. Range sharding keeps nearby keys together and is good for range scans but can create hot ranges; hash sharding spreads load evenly but loses ordering. I pick a key with many distinct values that matches the main access pattern, like tenant or user id, and I use consistent hashing or many small virtual partitions so adding nodes moves little data.

Could you say this in an interview?

Partitioning versus sharding. Partitioning splits a table into pieces; sharding puts those pieces on different machines. PostgreSQL's declarative partitioning supports range, list and hash partitioning on one server, and uses partition pruning so a query only touches the partitions that can match. Sharding adds the routing problem: which node holds this key?

Range versus hash.

  • Range (by date, by name prefix): efficient range queries and easy archiving of old ranges, but sequential keys like timestamps send all new writes to the last range.
  • Hash (hash of the key modulo the partition count): even spread, but a range query must ask every shard.
  • Directory / lookup table: a service maps keys or tenants to shards. Most flexible, at the cost of one more component.

Consistent hashing. With plain hash % N, changing N moves almost every key. Consistent hashing places nodes and keys on a ring so that adding a node only moves the keys next to it. The Dynamo paper uses it with virtual nodes, many positions per physical node, to even out load and to give a bigger machine more positions; Cassandra documents the same idea as multiple tokens per node (vnodes).

Hot keys. A good key spreads both data and traffic. DynamoDB's guidance is a clear example: each partition is designed for a maximum of 3,000 read units and 1,000 write units per second, so a single popular key can be throttled while the table as a whole is idle. Their fix for write-heavy keys is write sharding, adding a suffix so one logical key is spread over several partitions, and reading them back together.

Resharding. Plan it from day one: start with many logical partitions mapped to few physical nodes, so growing means moving whole partitions rather than rehashing rows. Move data online with a copy phase, dual writes or change-data capture, a verification step, and then a switch of the routing table.

Common mistakes interviewers listen for.

  • Sharding before it is needed; check the estimate and the scaling ladder first.
  • A key that breaks the main query, for example sharding orders by order id when every screen lists orders by customer.
  • Forgetting cross-shard work: joins, global unique constraints and transactions get much harder.
Sources (4)
  1. PostgreSQL: Table partitioningpostgresql.org
  2. AWS: Best practices for designing and using partition keys in DynamoDBdocs.aws.amazon.com
  3. DeCandia and others: Dynamo, Amazon's highly available key-value store (SOSP 2007)allthingsdistributed.com
  4. Apache Cassandra: Dynamo architecture (consistent hashing and vnodes)cassandra.apache.org