All three distribute or copy data, but for different goals. Partitioning - Splitting a large table or dataset into smaller pieces called partitions, usually within the same database server. - Types: - Horizontal partitioning: rows are divided by a key — for example orders by month (range partitioning), by region (list partitioning) or by hash of the ID. - Vertical partitioning: columns are split into separate tables — for example moving large, rarely used columns (documents, images) out of a frequently queried table. - Benefits: queries scan only relevant partitions (partition pruning), easier maintenance (archive or drop old partitions quickly with partition switching), better index performance. - Example: SQL Server partition functions and schemes, PostgreSQL declarative partitioning (PARTITION BY RANGE). - The application usually sees one logical table. Sharding - A form of horizontal partitioning across multiple database servers (shards). Each shard holds a subset of the data and runs independently. - Shard key choices: - Hash-based: hash(customerId) mod N — even distribution, but range queries span shards and resharding is complex (consistent hashing helps). - Range-based: customers A–M on shard 1, N–Z on shard 2 — simple range queries, risk of hot shards. - Directory or lookup-based: a mapping service decides which shard holds each tenant — flexible, but the directory must be highly available. - Benefits: scales writes and storage beyond one machine; isolates large tenants. - Challenges: cross-shard queries and joins, distributed transactions, rebalancing when adding shards, hot spots from poorly chosen keys, and more complex operations and backups. - Examples: Cosmos DB and DynamoDB partition keys (automatic sharding), MongoDB sharded clusters, Citus for PostgreSQL, Vitess for MySQL, application-level sharding by tenant. Replication - Copying the same data to multiple servers (replicas). - Purposes: high availability and failover, read scalability (route reads to replicas), disaster recovery across regions, and lower latency for distant users. - Models: - Leader-follower (primary-replica): writes go to the primary and are copied to replicas. Replicas serve reads. - Multi-leader: several nodes accept writes (useful for multi-region writes), requiring conflict resolution. - Leaderless: clients write to and read from several nodes using quorums (Cassandra, DynamoDB-style). - Synchronous vs asynchronous: synchronous replication guarantees replicas are up to date but adds latency; asynchronous replication is faster but replicas can lag (stale reads) and recent writes can be lost on failover. - Examples: SQL Server Always On availability groups, Azure SQL geo-replication, PostgreSQL streaming replication, MySQL replication, MongoDB replica sets. How they combine Large systems use all three: data is sharded across servers for write scale, each shard is replicated for availability, and within a shard large tables may be partitioned by date for maintenance. Comparison - Goal: partitioning manages large tables; sharding scales across servers; replication provides redundancy and read scaling. - Data on each node: partitioning and sharding hold different subsets; replication holds the same data. - Write scalability: sharding improves it; replication does not (writes still go to the leader in leader-follower setups). Interview summary: "Partitioning splits a table into pieces, usually on one server; sharding is horizontal partitioning across multiple servers to scale storage and writes; replication copies the same data to several servers for availability, failover and read scaling. They are often used together."
Ravindra Wadile · · 0 likes