04 / 05

When might a custom partitioning strategy be justified?

Difficulty: 8/10
Hot partitions, Partitioning, Ordering

When to Use a Custom Partitioner

A custom partitioner is justified only when the default partitioner cannot meet a specific requirement. The default partitioner hashes the key and maps it to a partition; it is simple, deterministic, and works well when the key distribution is reasonably uniform. You might need a custom partitioner when the key distribution is skewed and you cannot change the key, when you need to route certain keys to specific partitions for operational reasons, or when you need to implement a partitioning scheme that is not based on a simple hash, such as range partitioning or geo-based routing. Another case is when you need to co-partition two topics that use different key schemas; a custom partitioner can map both keys to the same partition. The bar for a custom partitioner should be high because it introduces coupling between your application and the partition layout, and it makes the system harder to reason about and operate.

The mechanism for implementing a custom partitioner in Kafka is to implement the Partitioner interface and configure the producer with partitioner.class. The partitioner receives the key, value, and number of partitions, and returns the partition number. It must be deterministic: the same key must always map to the same partition, otherwise ordering is broken. It must also handle the case where the number of partitions changes; if you add partitions, the mapping changes for some keys, and the partitioner must be updated or the ordering guarantee is lost across the change. A common approach for skewed keys is to use a composite key: the partitioner takes the original key and appends a shard suffix derived from a hash of another field, such as the event ID. This spreads the hot key across multiple partitions, but it breaks per-key ordering because records for the same key now go to different partitions. This is a deliberate trade-off: you gain throughput and balance at the cost of ordering. For co-partitioning two topics with different keys, the partitioner must map both keys to the same partition number, which usually requires a shared mapping function or a shared key space.

A common mistake is to write a custom partitioner to fix a hot partition without understanding the ordering implications. If you spread a hot key across multiple partitions, you lose ordering for that key, which may break downstream processing. Another mistake is to write a non-deterministic partitioner, for example one that uses a random number or the current time; this breaks ordering and makes debugging impossible. A third mistake is to use a custom partitioner when the real problem is the key choice; often, choosing a better key is simpler and more effective. The trade-off is between control and complexity. A custom partitioner gives you control over the mapping, but it adds code to maintain, it couples your application to the partition layout, and it can be a source of bugs. Use it only when the default partitioner cannot meet a hard requirement. Version note: the Partitioner interface has been stable, but the default partitioner changed in Kafka 2.4 with KIP-480 (sticky partitioner for null keys). If you implement a custom partitioner, be aware of the client version and test thoroughly. Also note that custom partitioners are not supported in some managed Kafka services, so check the provider's limitations.

javascript
  1. 1

    Use a custom partitioner only when the default partitioner cannot meet a hard requirement.

  2. 2

    Common cases: skewed keys you cannot change, routing specific keys, co-partitioning topics with different keys.

  3. 3

    Custom partitioners must be deterministic; non-deterministic ones break ordering.

  4. 4

    Sharding a hot key spreads load but breaks per-key ordering.

  5. 5

    Adding partitions changes the mapping; the custom partitioner must handle it.

  6. 6

    Custom partitioners are not supported in some managed Kafka services.

  7. 7

    Prefer fixing the key choice over writing a custom partitioner when possible.

Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.