ML
Apache Kafka

Partitioning Strategy: Keys, Hot Partitions, and Ordering

Ordering guarantees in Kafka are per-partition. Getting the key right is the whole game.

October 02, 20257 min readKafkaDesign

Kafka partitions are the unit of parallelism, of ordering, and — when you pick wrong — of all your performance problems.

The one rule

Ordering is guaranteed per partition. If two records need to be processed in order, they must land on the same partition. The only way to guarantee that is to give them the same key.

// Orders for the same customer must stay in order:
producer.send(new ProducerRecord<>("orders", customerId, payload));

Hot partitions

The default partitioner hashes the key with murmur2. If 5% of your keys produce 80% of your traffic, five partitions will be saturated while the rest sit idle. Signs you have a hot partition:

  • One broker pegged at disk/network limits, others quiet.
  • Consumer lag concentrated on one partition.
  • kafka.server:type=BrokerTopicMetrics,name=BytesInPerSec shows high skew.

Fixing it

Three approaches, from least to most invasive:

  1. Composite keys. Append a bounded suffix: customerId + (ts % 8). You fan out to 8 partitions per customer while keeping related records close.
  2. Sticky partitioner with null key when you don't need ordering. Default since 2.4 — it batches records to a single partition per window, which improves compression without skew.
  3. Custom partitioner that maps known hot keys to a reserved sub-pool of partitions.

Number of partitions

A partition is also a cost — more partitions mean more file handles, more leader elections, and slower rebalances. A rough planning rule:

partitions = max(
  target_throughput / per_partition_throughput,
  max_consumers_in_group
)

And never reduce partition count in place — you can only add. Plan for 2–3× growth from day one.

SharePostLinkedIn

Reader Discussion

7 replies// weighed in

TopNewestAuthor
Add to the thread
Disagree, agree harder, or share your own experience…
Email instead →markdown okbe kind
  1. Highlighted by author
    Tuấn Phạm🇻🇳 HCMC· Staff Engineer · Tiki Data PlatformStory

    min.insync.replicas=2 với acks=all là combo chuẩn. Bọn em từng để mặc định min.insync.replicas=1, một broker rolling restart là mất 4 message — xui là đúng cái event payment confirm. Ngồi viết postmortem từ 2h sáng đến 7h.

    Oct 03, 2025·1 day later
    • ML
      Minh LeAuthor

      Đúng cái khoảnh khắc realize default = 1 thì đã muộn. Cảm ơn bro share — mình sẽ thêm warning box vô post.

      Oct 03, 2025
  2. Sven Bergström· Senior SWEStory

    fresh transactional.id on each pod restart is THE silent killer. We had it for ~14 months. Realised when a zombie producer wrote 80k duplicates after a node went catatonic and came back.

    Oct 07, 2025·5 days later
  3. Quốc Anh· Backend Lead · FinhayAgrees

    Đoạn segment files giải thích quá ngắn gọn. Bọn em hay quên log.segment.bytes vs log.retention.bytes là 2 thằng khác nhau, bị retention không kick in là vì segment chưa rolled — đúng cái rule of thumb cuối bài.

    Oct 04, 2025·2 days later
  4. Amelia Brooks· Distributed SystemsPushback

    tiny nit but acks=0 is not literally fire-and-forget at the protocol layer — the producer still writes to its socket buffer. The 'forget' is the broker side. Pedantic but bites people in metrics dashboards.

    Oct 11, 2025·1 week later
  5. Priya Ramaswamy· SRE · Checkout InfraFrom experience

    Static membership single-handedly cut our deploy-induced p95 lag from ~28s to under 2s. group.instance.id is the most under-rated config in the entire client lib. We tell every new SRE about it on day one now.

    Oct 06, 2025·4 days later
  6. Rachel Gold· Staff SREAgrees

    the on-call framing throughout this piece is what makes it land. too many infra articles assume you never get paged. those are written by people who never got paged.

    Oct 05, 2025·3 days later

Worked on something similar? Email ducminhldm@gmail.com — I read every one. The good ones become future posts.

Comments seeded · live discussion via email