When One Machine Is Not Enough
Partitioning, which interviewers also call sharding, splits one data set across machines, so that each machine holds a part and takes the writes for that part. It buys capacity no single machine has, and it is paid for by every query that needs more than one part.
So the interview question is never whether partitioning is good. It is which limit of one machine the design has passed, by how much, and in which year. The answer is a number, and this lesson is about getting to it out loud. Backend Deep Dive ends where one service stops growing; this chapter starts there, with the data.
Three Signals
One machine runs out in one of three ways: disk for the data, capacity for the writes, or memory for the keys that are actually read. The third is the working set. Once the hot keys outgrow memory, reads go to disk and slow down. For a sense of scale, a large memory-optimised cache node holds about 500 GB.
Reads are not on the list. A cache tier and read replicas, both from Chapter 2, take reads without splitting anything, so a read-heavy design reaches for those first. Writes cannot be copied away, because every copy has to take every write: a replica adds read capacity and no write capacity at all. That is why write throughput and storage are the two limits only partitioning fixes.
Bigger First
Scaling up changes nothing in the design. There is still one machine, every query is local, and every transaction runs on one node. It holds while the estimate for the problem's horizon fits the largest node with headroom. Its cost is a price that climbs steeply with size and a ceiling no budget moves. The trigger to leave it is the estimate passing that ceiling inside the horizon.
Partitioning has its own bill. Queries that cross partitions get slower, which the last lesson of this chapter prices. Transactions across partitions need a protocol, which Chapter 5 teaches. And data has to move whenever the cluster grows, which lessons 3 and 4 cover. A design that fits one machine and is partitioned anyway pays all of that for nothing, and the interviewer asks why.
Splitting one big table into many tables on the same server is a different tool, partitioning inside one engine, and PostgreSQL Deep Dive covers it. Here the split is across machines.
Counting Partitions
The count comes from a stated workload and a stated limit for one partition. Assume records under 1 KB, 20,000 writes a second at peak, 30,000 reads a second, and 10 TB stored. For the limit, take one key-value partition as DynamoDB documents it: 1,000 writes and 3,000 reads a second.
Work each signal on its own. Twenty thousand writes at 1,000 a partition need 20 partitions. Thirty thousand reads at 3,000 a partition need 10. Ten terabytes, at an assumed 2 TB of usable disk a node, need at least 5 partitions, so that none outgrows one node. The largest answer wins, so the writes decide, and the count is 20.
Then say the headroom. Assume each partition runs at half its limit, so that traffic above the estimate, or keys a little hotter than the average, does not tip it over. That doubles the count to 40, and 40 is the number you give.
Partitions Are Not Machines
The 40 is a logical count. The machines come from the bytes and the copies. Three copies of 10 TB are 30 TB, and at 2 TB a node that is 15 nodes. Forty partitions with three copies each are 120 copies, so each of the 15 nodes holds about 8.
Many small partitions on each node is what makes growth cheap. A new node takes whole partitions from the others, and no key changes its partition; the third lesson of this chapter calls this fixed slots. Replication is what keeps a partition alive when its node dies. Partitioning alone only makes each failure smaller, and Chapter 4 picks up the copies.
Saying When
The trigger is the estimate, never a feeling that the database is slow. Name the dimension, the number, and the year it passes one node. You might say: "Writes fit one node for years. Storage is what runs out: it passes one node's 2 TB in year three, so I'll choose the partition key now and split before then."
That is the shape of the answer for a design that fits today and passes later: it starts on one node with its partition key already chosen, which the last lesson of this chapter is about. The split, when it comes, is then a move of data and not a change of schema.
Vertical scaling keeps one machine and every query local, and stops at the largest machine there is. Use it while the horizon's estimate fits that machine with headroom in storage, writes and working set.
Horizontal scaling splits the data by a key and costs every query and transaction that crosses the split. Use it when one of the three signals passes the largest machine inside the horizon.
How Interviewers Probe This
The first probe is "Why not just buy a bigger machine?" A good answer agrees while the estimate fits, names the dimension and the year it stops fitting, and says what partitioning will cost when it comes.
The second is "How many shards do you need?" A good answer works each signal against a stated limit, takes the largest, adds the headroom out loud, and keeps partitions apart from machines. You might say: "Writes decide it: twenty thousand a second at a thousand a partition is twenty, and forty at half load, spread over fifteen nodes."
The third is "Reads are a hundred times the writes. Do you shard?" A good answer says not for the reads: the cache and the replicas take them, and sharding waits on writes, bytes and the working set.
- "Partitioning makes the store highly available." It makes each failure smaller, not rarer. A partition without replicas is lost with its node, and losing one of 20 is still losing data.
- "A read-heavy system needs sharding." Reads are the job of the cache and the replicas. Partition when writes, bytes or the working set pass one machine.
- "More partitions are always safer." Each one is a unit to place, move and watch, and a query that crosses partitions waits for the slowest of them, so more partitions make it slower.
- "Partitioning fixes a slow query." A slow query is a missing index or a bad plan on one machine first. Splitting the data runs the same slow query on twenty machines.
- "The partition count is the machine count." Partitions are logical, and many on each machine is what lets a cluster grow by moving whole partitions.
- Name which of storage, writes or working set passes one machine, with the number, before you draw a second database.
- Count partitions from each signal against a stated limit for one partition, take the largest, and say the headroom you assume.
- Scale up first while the horizon's estimate fits the largest node, and say the year it stops fitting.
- Choose the partition key even for a one-node first design, so that the later split moves data and changes no schema.
Knowledge Check
A store takes 15,000 writes and 30,000 reads a second and holds 6 TB. One partition takes 1,000 writes and 3,000 reads a second, and a node holds 2 TB. Which signal sets the partition count?
- The writes, at 15 partitions, or 30 with half-load headroom
- The reads, because 30,000 a second is the largest rate of the three
- The storage, because the bytes have to fit before any traffic does
- All three together, adding 15, 10 and 3 into 28 partitions
Reads are a hundred times the writes and growing. Why is that, on its own, not a reason to partition?
- Partitioning spreads reads unevenly, so the hottest keys only get slower
- A cache tier and read replicas take reads without splitting the data
- Reads never grow past what one machine can serve at any real scale
- Partitions only take writes, and reads always go to one primary first
Forty partitions are kept in three copies each on 15 nodes. About how many partition copies does each node hold?
- About 3, from forty partitions spread over fifteen nodes
- Exactly 1, since each partition copy needs a machine of its own
- About 8, from 120 partition copies spread over fifteen nodes
- About 40, since every node keeps a copy of every partition
A store is split into 20 partitions on 20 nodes with no replicas, and one node dies. What happens?
- Nothing is lost, because the risk is spread over the 20 nodes
- The whole store stops until the failed node comes back again
- The other nodes rebuild its partition from the data they hold
- Its partition is lost with it, and the other 19 keep on serving
The estimate for the next three years fits the largest node with room to spare in storage, writes and working set. The interviewer asks about sharding. Which answer is strongest?
- Shard from the first day, since splitting later means a full migration
- Shard by default, because one machine is a single point of failure
- One node for now, with the key chosen and the year it stops fitting
- Add read replicas now and shard once the database starts to feel slow
You got correct