Try
↑ ↓ move↵ openesc close

Interview prep / Partitioning

Mid · Partitioning · System design interview question

A table keyed by timestamp is range partitioned and all the traffic is new inserts. What goes wrong and how do you fix it?

Sketch your own answer first, then compare it with the one below.

A strong answer

Every new key is larger than every existing key, so all inserts go to the partition holding the highest range. That partition takes all the write traffic while the others only serve reads of old data. Adding machines does not help.

Fixes change the key so that writes spread. Put a prefix in front of the time, such as a hash of an entity id, a shard number, or a bucket number chosen at write time, so concurrent writes land on different partitions. Or hash the whole key, at the cost of ordered scans. Or choose a different primary key, such as a user id first and the time second.

The trade: with a prefix, a time-range query must go to every prefix and be merged. Choose the prefix from the query pattern, so the common queries stay on one partition.

Sources

Learn it by running it

The partitioning lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms