advanced 2 min answer

A stream requires ordering per user but not globally. How should partitioning be designed, and what breaks it?

partitioningorderinghot-keysrebalancingredditdesign
Show the full answer Hide the answer

The design

Partition by the key whose ordering is required. Ordering is guaranteed within a partition, so partitioning by user identifier gives per-user ordering with as much parallelism as there are partitions.

Global ordering requires a single partition, which is a single consumer and therefore a hard throughput ceiling. Almost no system genuinely needs it, and the requirement should be challenged when it appears.

What breaks it

1. Hot partitions. A viral item, a very active user, or a bot concentrates traffic on one partition. Aggregate throughput looks healthy while one partition is saturated and its consumers fall behind.

This requires per-partition monitoring, not aggregate, and a strategy planned in advance — because partition count is difficult to change on a live system.

2. Rebalancing. A consumer joining or leaving triggers a reassignment, during which processing pauses across the group. Frequent restarts therefore mean frequent pauses, which is invisible until it matters.

3. Key changes. If an entity's partition key changes — a user merged, an item recategorised — its events move to a different partition and ordering across the change is not guaranteed.

4. Reprocessing across a partition change. Replaying history after altering the partitioning scheme does not reproduce the original ordering.

Handling the hot key

  • Salt the key for known-hot entities, splitting them across partitions and accepting that ordering within the salted set is lost — acceptable when the hot entity's ordering matters less than its throughput.
  • A dedicated partition or consumer for the hottest keys.
  • Detect hotness continuously, since which keys are hot changes and a static assignment ages badly.

The design question worth asking early

What is the smallest scope over which ordering is genuinely required? Teams frequently specify global ordering because it is simpler to state, then discover the throughput ceiling.

Per-entity ordering is almost always sufficient, and it is the difference between a system that scales and one that does not.