Scaling and changing databases · mid
Partitioning or sharding
Partitioning splits one table inside one server. Sharding splits data across several servers. They solve different problems.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Partitioning: one table split into parts (range, list or hash) inside one server. PostgreSQL has declarative partitioning, and MySQL has partitioning.
- Gains: pruning (the query touches only some partitions), cheap removal of old data (drop or detach a partition instead of DELETE), smaller indexes and per-partition maintenance.
- Pruning needs the partition key in the WHERE clause. Without it, every partition is scanned.
- Daily partitions mean a month's query touches 30 partitions, each with its own index lookup and planning cost.
- Unique indexes are per partition, so PostgreSQL can enforce uniqueness only when the key includes the partition columns.
- Sharding: data spread across several servers by a shard key, and the app or a proxy routes each query. It scales writes and storage past one machine.
- After sharding you have cross-shard queries and joins, cross-shard transactions, rebalancing, global unique ids and more operations.
- Tools: Citus (PostgreSQL) and Vitess (MySQL) give routing and rebalancing. Application-level sharding gives control but needs more code.
The red pen
Common traps: mistake → fix
Mistake: Partitioning and sharding are the same
Fix: one stays on one server, the other uses many.
Mistake: Partitioning fixes a missing index
Fix: add the index first.
Mistake: Any query works on any partition layout
Fix: without the key in WHERE it scans all partitions.
Easy to hard
Questions interviewers ask, easy to hard
- What is the difference between partitioning and sharding?
- When do you partition a table?
- What do you lose after sharding?
- Why can a unique index be a problem with partitions?
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.