Architecture Sharding PostgreSQL Databases

Database Sharding Strategies: When and How to Partition Your Database

10 min readWANOVA

Sharding is one of the most used and most misunderstood terms in the database engineering world. Engineers who have never operated a system at true scale frequently conclude that their performance problems require sharding, when the actual cause is a missing index, a suboptimal query, or a lack of connection pooling. The cost of premature sharding is enormous: cross-shard queries become performance nightmares, distributed transactions require complex two-phase commit protocols, schema changes need to be coordinated across N databases simultaneously, and the development environment complexity multiplies. This guide covers when sharding is genuinely necessary, the alternatives to exhaust first, and how to implement it correctly when the time comes.

When Do You Actually Need Sharding?

Real signals for needing sharding are about data volume and write throughput — not user counts. Ten million users reading educational content generate far less database load than 100,000 users doing analytics-heavy operations. Measure before deciding, and exhaust the optimization steps in the alternatives section before committing to sharding's operational complexity.

  • Data size exceeds the capacity of the most powerful available server (usually > 10TB)
  • Write throughput exceeds a single server's capacity (> 100,000 writes/second)
  • A single database has become a single point of failure that can't be avoided
  • GDPR requires storing certain countries' data on geographically separate servers
  • In most cases: read replicas + caching + indexing is enough for 10 years
  • Measurement test: if pg_database_size returns < 500GB, you almost certainly don't need sharding
  • Write bottleneck test: if insert latency is < 10ms at p99, you don't need sharding
  • Compliance-driven sharding: data residency requirements are a legitimate non-performance reason to shard
→ Discord and Stack Overflow served millions of users on a single optimized PostgreSQL server. Don't over-complicate without reason.

Sharding Strategies: Trade-offs and Use Cases

StrategyHow It WorksBest ForDrawbacks
Hash Shardinghash(key) % NBalanced distributionHard rebalancing
Range ShardingBy range (date, A-M)Time-series, analyticsUneven distribution
GeographicBy geographic regionGDPR, latencyComplex routing
DirectoryMapping tableFull flexibilityMapping table = bottleneck

PostgreSQL Table Partitioning: Easier Than Full Sharding

Table partitioning splits a large table into smaller physical pieces within the same PostgreSQL instance. This is dramatically simpler than full sharding: the application needs zero changes, queries work identically, and transactions remain fully ACID. Partitioning delivers most of the performance benefits of sharding for time-series and tenant-partitioned data, without any of the distributed systems complexity.

  • Range Partitioning by date: events table → events_2024, events_2025, events_2026
  • Partition Pruning: query on events_2026 only doesn't touch 2024 data
  • Partition Detach: automatically move old partitions to cold storage
  • List Partitioning: by tenant_id in SaaS — each tenant in a partition
  • Advantages: transparent to the application, VACUUM on smaller partitions is faster
  • Partition-wise aggregation: PostgreSQL can parallelize GROUP BY across partitions
  • Index per partition: each partition has its own indexes — smaller, faster to scan
  • Partition key constraint: the WHERE clause must include the partition key for pruning to work

Read Replicas: The Step Before Sharding

In most LMS applications, 80-90% of queries are reads. Distributing read load across read replicas effectively multiplies read capacity by the number of replicas with no application complexity beyond routing reads to the replica pool. This is the highest-leverage database scaling technique and should always precede any consideration of sharding.

  • 80% of LMS queries are reads (SELECT) — route them to a read replica
  • PostgreSQL Streaming Replication: replica synced with master within seconds
  • Connection pooling: PgBouncer automatically routes reads and writes
  • Single interface for the application: configure master and replica in pool — app doesn't care
  • Win: double read capacity without sharding complexity
  • Read replica lag: monitor replication lag — stale reads are acceptable for analytics, not for user data
  • Multiple replicas: 3 replicas triple read capacity — each additional replica is a linear improvement
  • Replica for analytics: dedicate one replica to heavy reporting queries that would otherwise impact the primary

Real Sharding Challenges: The Full Cost

The hidden costs of sharding are not in the initial implementation — they are in the ongoing operational burden. Every aspect of database work becomes more complex in a sharded environment: schema changes require coordinated rollouts, debugging requires knowing which shard to inspect, and cross-shard analytics requires a data warehouse or expensive scatter-gather queries.

  • Cross-shard queries: JOINs between data in different shards — performance disaster
  • Rebalancing: adding a new shard requires moving data without downtime — very complex
  • Distributed transactions: ACID across shards requires 2-Phase Commit
  • Schema changes: ALTER TABLE across N shards simultaneously
  • Testing: a development environment simulating sharding is complex
  • Hot spots: hash sharding distributes evenly, but some keys still receive disproportionate traffic
  • Operational tooling: backups, monitoring, and alerting must be multiplied across all shards
  • Developer experience: every query must specify the shard or go through a routing layer

The Checklist to Exhaust Before Sharding

Run through this optimization checklist in order before concluding that sharding is needed. Each step has delivered dramatic improvements for platforms that skipped them and jumped prematurely to architectural solutions. In our experience, teams that exhaust this list typically find the performance they need without sharding.

  • 1. Correct indexing — most lost performance is an index problem
  • 2. Query optimization — EXPLAIN ANALYZE on your slowest queries
  • 3. Connection pooling with PgBouncer
  • 4. Read Replicas to distribute load
  • 5. Caching with Redis for hot data
  • 6. Archival: move old data to cold storage
  • 7. Vertical scaling: may be cheaper and easier than sharding
  • 8. Table partitioning: horizontal partitioning within a single instance

Citus: PostgreSQL Horizontal Sharding Extension

When you genuinely need sharding and want to stay in the PostgreSQL ecosystem, Citus is the most mature option. Citus extends PostgreSQL with distributed tables and a coordinator node that transparently routes queries to the correct shards. It preserves the PostgreSQL API and tooling while enabling horizontal scaling across multiple nodes — significantly reducing the operational complexity compared to managing sharding logic in application code.

  • Citus extension: adds distributed tables to standard PostgreSQL — no new DB engine to learn
  • Coordinator node: routes queries to the correct shard transparently
  • Distributed tables: selected high-volume tables are sharded; reference tables are replicated everywhere
  • Co-location: tables sharded on the same key are co-located — JOINs work without cross-shard queries
  • Citus Cloud: managed Citus on Azure — reduces operational overhead
  • Parallel query execution: distributed aggregations run in parallel across all shards
  • Migration path: start with a single-node Citus installation, add nodes without code changes

Real Case Study

Nur Al Huda Academy — Full LMS Platform

How we built an educational platform serving thousands of students from scratch — architecture, challenges, and real results.

Read the Case Study

Free 30-minute consultation

Get an initial concept for your platform.

شعار وانوفا WANOVA

Built for the future. Premium digital platforms and cinematic software solutions.

Quick Links

© 2026 WANOVA. All rights reserved.