Skip to the page

Scaling databases · Concept

Partitioning or sharding

The interviewer asks

What's the difference between partitioning and sharding? When would you partition a table, and when would you shard a database?

Then

Follow-ups the interviewer asks next

  1. Zerodha found daily partitions made inserts faster but date-range queries slower, so they use monthly ones. Why?
  2. What is partition pruning, and when doesn't it happen?
  3. Why must a unique key in PostgreSQL include the partition key?
  4. What problems appear only after you shard?
  5. Citus, Vitess, or your own routing: how do you choose?

The answer

What a strong answer covers

  • Partitioning splits one table into parts inside one server (PostgreSQL declarative partitioning by range, list or hash; MySQL partitioning). It gives pruning, cheap removal of old data (drop or detach a partition instead of DELETE), smaller indexes and per-partition maintenance.
  • Sharding splits data across several servers by a shard key; the app or a proxy routes each query. It scales writes and storage past one machine.
  • Daily partitions mean a month's query touches 30 partitions, each with its own index lookups and planning cost.
  • Pruning needs the partition key in the WHERE clause; without it, every partition is scanned.
  • Unique indexes are per partition, so PostgreSQL can only enforce uniqueness when the key includes the partition columns.
  • After sharding: cross-shard queries and joins, cross-shard transactions, rebalancing, global unique ids, more operational work.
  • Citus (PostgreSQL) and Vitess (MySQL) give routing and rebalancing; application-level sharding (Notion, Instagram) gives control. Zerodha chose postgres_fdw over Citus because Citus would have meant rewriting many queries.

The red pen

Common traps

Avoid:using the words as the same thing; partitioning to fix a missing index.

Practise this out loud

Say your answer out loud to an AI interviewer that follows up, and get a score for it.

Practise by voice

Practise questions like this out loud

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview