Skip to main content

Sharding vs Partitioning: Core Concepts

Partitioning and sharding both segment data, but they operate at fundamentally different scopes. Partitioning splits a table into child segments within a single database process; the query planner handles routing transparently. Sharding distributes data across independent database instances; the application or proxy must route each request explicitly. This distinction dictates routing complexity, transaction guarantees, and maintenance workflows. Database Partitioning Fundamentals & Architecture establishes the baseline before diving into the tradeoffs here.

Architectural Boundaries & Data Locality

Partitioning (single-instance):

  • Child tables share the parentโ€™s connection pool and storage engine.
  • Local joins execute at memory speed without network overhead.
  • The query planner routes predicate-matching queries to the correct child table automatically.
  • ACID guarantees remain fully local.

Sharding (multi-instance):

  • Each shard maintains its own connection pool, WAL, and storage engine.
  • Application or proxy layers must map each request to a shard before executing.
  • Cross-shard requests introduce measurable network latency and serialization overhead.
  • Distributed ACID requires two-phase commit (2PC) or saga patterns.
-- PostgreSQL declarative partitioning: single-instance, transparent routing
CREATE TABLE events (
  id BIGINT GENERATED ALWAYS AS IDENTITY,
  created_at TIMESTAMPTZ NOT NULL,
  payload JSONB
) PARTITION BY RANGE (created_at);

CREATE TABLE events_2024 PARTITION OF events
  FOR VALUES FROM ('2024-01-01') TO ('2025-01-01');

No application-level routing changes are required. EXPLAIN will show partition pruning automatically when created_at appears in the WHERE clause.

Routing Logic & Query Execution

Partitioning relies on static partition metadata cached by the database engine. Sharding requires dynamic shard maps that update during scaling events. Cross-partition joins in partitioned tables leverage local indexes efficiently; sharded joins require scatter-gather execution or denormalization.

Transaction boundaries in partitioned tables remain fully local ACID. In sharded architectures, writes spanning two shards require distributed coordination. Teams must evaluate Consistency Models in Distributed Databases when designing write paths across independent nodes.

# Sharding router configuration
router_config:
  algorithm: consistent_hash
  replicas: 3
  vnodes: 150
  fallback: local_cache

Consistent hashing ensures minimal data movement during node additions. ORM layers must integrate this routing by intercepting connection acquisition and resolving shard keys before query execution.

Operational Workflows: Rebalancing & Maintenance

Partitioning maintenance involves attaching or detaching child tables. Backups run at the instance level with standard tooling (pg_dump, mysqldump). DDL for adding a new partition is non-blocking in PostgreSQL 12+ (ATTACH PARTITION) when the child table pre-exists.

Sharding maintenance requires coordinated data migration across independent nodes. Hot shards demand automated rebalancing pipelines. Write amplification spikes during migration windows and replication lag must be monitored before scaling out.

-- Detect partition skew before triggering rebalancing (PostgreSQL)
SELECT
  c.relname AS segment,
  pg_size_pretty(pg_total_relation_size(c.oid)) AS size,
  c.reltuples AS row_count
FROM pg_class c
JOIN pg_inherits i ON c.oid = i.inhrelid
WHERE i.inhparent = 'events'::regclass
ORDER BY pg_total_relation_size(c.oid) DESC;

Use this query to detect data skew before triggering rebalancing. For zero-downtime shard expansion, implement dual-write migration pipelines: route a small percentage of traffic to the new shard, validate checksums, then incrementally shift write weight.

Evaluating infrastructure overhead via Scaling Limits and Cost Tradeoffs ensures capacity planning aligns with budget constraints.

Debugging & Observability

Distributed data requires explicit correlation identifiers. Inject trace IDs at the ingress proxy before routing to shards. Aggregate query execution plans across nodes to identify scatter-gather bottlenecks. Standardize log schemas to include shard identifiers and routing latency.

Use decision matrices to validate architecture against SLA requirements. Reference How to Choose Between Sharding and Partitioning for High-Traffic Apps when mapping workload characteristics to operational capacity.

Common Mistakes

  • Treating sharding as a drop-in replacement for partitioning: Sharding requires application-level routing changes and breaks ACID guarantees across nodes. Native partitioning remains transparent to the application layer.
  • Selecting high-cardinality, rapidly changing shard keys: Causes severe data skew and hot shards, triggering inefficient rebalancing that degrades query performance.
  • Ignoring cross-segment query performance degradation: Distributed joins bypass index locality and require explicit denormalization, materialized views, or pre-aggregation pipelines to maintain acceptable latency.

FAQ

Can I convert a partitioned table to a sharded architecture without downtime? Yes, but it requires a dual-write migration phase, data synchronization between the old and new schemas, and traffic cutover via feature flags to avoid data loss.

How do I handle cross-shard transactions? Implement two-phase commit (2PC) for strong consistency, or the Saga pattern for eventual consistency. Both add latency; design for idempotent retries in either case.

When does partitioning become insufficient for scaling? When a single-node CPU, memory, or I/O ceiling is reached despite vertical scaling and query/index optimization. At that point, horizontal distribution across independent clusters is necessary.

Articles in This Section