What are partitioning and sharding in databases? How do they differ?

Instruction: Explain what partitioning and sharding are in the context of databases and discuss how they differ from each other.

Context: This question aims to assess the candidate's knowledge of database scalability techniques, specifically the differences and applications of partitioning versus sharding.

Updated

Example Answer

Partitioning divides data into smaller pieces. In table partitioning, rows may be separated by a range, list, or hash key while queries still use one logical table. For example, monthly partitions can make old-data removal easier and let the database skip irrelevant partitions when a query's conditions support pruning.

Sharding is horizontal partitioning across separate database nodes or groups of nodes. A tenant ID could determine which shard stores each tenant's rows, spreading storage and work across machines. A shard can itself contain partitioned tables, so the techniques can be combined.

I would choose keys from the access patterns and check for hot shards, expensive cross-shard queries, and rebalancing costs. Neither technique makes every query faster. Sharding also does not create high availability by itself: losing an unreplicated shard makes its data unavailable. Availability needs a separate replication and failover design, including placement across failure domains and the reliability of routing and metadata services.

Why this works

Partitioning describes dividing the data; sharding adds distributed placement and coordination. The terminology varies by database, so I would name the system before discussing exact transaction or consistency guarantees.

Related Questions