Preparing your design workspace

Loading the problem and your latest saved attempt…

Design a Horizontally Scaled PostgreSQL Database

Partition a growing PostgreSQL workload across database shards and move ownership safely as capacity changes.

Problem statement

For this exercise, assume a multi-tenant workflow application has outgrown one PostgreSQL primary. Design a horizontally scaled PostgreSQL data layer that routes tenant-scoped reads and writes, preserves each tenant’s transactional invariants, and can add or remove database shards without a maintenance window.

Explain the partition key and routing method, then focus on how an online downscale avoids rebuilding and deploying one complete key-to-shard map at once. Cross-tenant analytics may run asynchronously and are outside the transactional request path.

What to build

  1. Route every tenant-scoped read and write to the shard that currently owns that tenant.
  2. Add shards or drain selected shards while the application remains available.
  3. Preserve per-tenant uniqueness, ordering, and transaction semantics during normal operation and migration.
  4. Detect, resume, or roll back interrupted tenant moves with an auditable ownership history.

At scale

  • The current dataset is 5 TB and grows by 150 GB per month; design for ten times today’s capacity.
  • Peak traffic is 20,000 reads and 5,000 writes per second, with one tenant allowed to contribute up to 2% of traffic.
  • Normal routed reads should finish within 100 ms at p99 and writes within 200 ms at p99.
  • A shard move may copy hundreds of gigabytes and must tolerate worker restarts, replication lag, and one database failure.