Video in progress

One partition is getting far too much load (a hot partition). How do you find the cause and what do you do?

The cause is usually an uneven key distribution: one big customer, or a null/constant key. The fix depends on changing or "salting" the key, but each option gives up the ordering guarantee to some degree.

📖 Answer

Symptoms: the lag on one partition is always high, the network and disk on its leader broker are much busier than the others, one consumer thread is at 100% while the rest are idle.

Diagnosis: I compare BytesInPerSec and log-end-offset growth per partition, then read a sample from that partition and count the keys:

kafka-console-consumer.sh --bootstrap-server kafka:9092 --topic orders \
  --partition 7 --max-messages 50000 --property print.key=true \
  | cut -f1 | sort | uniq -c | sort -rn | head

Common findings: a single marketplace seller accounts for 40% of all traffic; country=UZ was used as the key; because of a bug the key turned into the text "null".

Fixes:

  • Make the key finer-grained: orderId instead of sellerId. Ordering is now per order rather than per seller, and often that's all that was needed anyway.
  • Salting: only for hot keys, sellerId + "-" + (orderId % 8). The load spreads over 8 partitions, but seller-level ordering is lost; if needed it's reassembled on the consumer side.
  • A separate topic: move a very large customer to its own topic and its own consumers. Operationally simple and gives isolation.
  • A custom Partitioner is the last resort, because all producers must use the same logic, otherwise one key lands in two places.

Adding partitions doesn't help here: the hot key still lands in a single partition.