Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 4 OF 5 · 2 MIN READ

Database Replication, Partitioning and Sharding

When one database cannot keep up, you replicate it for reads and availability, partition big tables, and finally shard data across several databases. Interviewers ask how each works and what it costs.

What you will learn

  • Explain primary-replica replication and replication lag
  • Compare partitioning and sharding
  • Choose a shard key
  • Know the costs: cross-shard queries, rebalancing, transactions

Key concepts

Replication

The primary takes writes and streams changes to replicas that serve reads. Asynchronous replicas can lag, so a user may not see their own write if you read from a replica right after.

Read-your-writes

Route a user's reads to the primary for a short time after they write, or wait until the replica has caught up to that write.

Partitioning

Splitting one large table into pieces inside the same database, for example orders by month. Queries for one month touch one partition and old partitions are easy to drop.

Sharding

Splitting data across separate database servers by a shard key (user_id). Each shard holds a subset of rows, so writes scale too.

Step by step

Choosing a shard key

  • High cardinality and even spread (user_id, tenant_id)
  • Most queries include it, so they hit one shard
  • Avoid keys that create hot spots (country, created_at)
  • Hash-based spreads evenly; range-based keeps ranges together but can be uneven

Examples

Range partitioning in PostgreSQL

CREATE TABLE events (id bigserial, user_id bigint, type text, created_at timestamptz NOT NULL)
  PARTITION BY RANGE (created_at);

CREATE TABLE events_2026_11 PARTITION OF events FOR VALUES FROM ('2026-11-01') TO ('2026-12-01');
CREATE TABLE events_2026_12 PARTITION OF events FOR VALUES FROM ('2026-12-01') TO ('2027-01-01');
-- dropping a month of old data is instant: DROP TABLE events_2025_11;

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Pick a shard key for a multi-tenant SaaS and for a social network's posts.
  • Explain how you would run "top 10 posts across all users" on a sharded database.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

Synchronous vs asynchronous replication?

Synchronous waits for a replica to confirm before committing: no data loss on failover but slower writes. Asynchronous commits immediately: fast, but the last few writes can be lost if the primary dies.

Why is sharding a last resort?

It makes joins, transactions and unique constraints across shards hard, complicates operations and rebalancing, and is difficult to undo. Indexes, caching, replicas, partitioning and bigger hardware usually come first.

Summary

Replicas scale reads and add failover but lag; partitioning splits big tables inside one database; sharding spreads data and writes across servers using a shard key, at a real cost in complexity.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus