Stackbook Logo
messagingestablished · high operational burden

Consumer Groups and Partitioning

Also known as: partitioned log consumption, Kafka consumer groups

Intent

Scale stream consumption while preserving per-key order by splitting a topic into partitions and assigning each partition to one consumer in a group.

Problem

A single ordered log cannot be consumed in parallel without losing order, yet one consumer cannot handle the volume. Naive parallelism interleaves events for the same entity and corrupts state.

Forces

  • •Parallelism and ordering conflict
  • •Related events must land on the same worker
  • •Group membership changes during deploys and failures
  • •Hot keys skew load

Solution

Overview

Partitioned logs such as Kafka combine ordering and scale. Order is guaranteed only inside a partition, so the producer picks a key that keeps related events together.

A consumer group is a team of workers that divide the partitions between them. More groups mean more independent readers of the same data.

Choose the key by asking which events must be seen in order. Customer id suits account state, while a random key maximizes balance but gives up ordering. Estimate peak throughput per partition and divide to get the partition count, leaving headroom, since you can add partitions later but doing so changes key placement.

How it works

The producer hashes the key to choose a partition. The group coordinator assigns each partition to one member, and members commit offsets after processing.

key=cust42 -> hash % 4 = partition 2

topic orders: P0 P1 P2 P3
group billing:   c1(P0,P1)  c2(P2,P3)
group analytics: c1(P0..P3)

Rebalancing is the main operational cost. When a member joins or leaves, partitions move and processing pauses briefly. Cooperative rebalancing, static membership and sensible session timeouts reduce disruption. Monitor consumer lag per partition, because an average lag hides a single stuck or hot partition that holds back one slice of your keys.

Pitfalls

  • More consumers than partitions leaves extras idle.
  • Committing offsets before the work completes, which loses messages on crash.
  • Choosing a low-cardinality key that creates hot partitions.
  • Increasing partitions later, which reroutes keys and breaks ordering for in-flight data.

Key takeaways

  • Order exists per partition, so key choice defines ordering.
  • Partition count caps consumer parallelism.
  • Commit offsets after processing.
  • Plan partition counts up front.

✓ When to Use

  • •High-volume streams needing per-key ordering
  • •Several independent applications consume the same events
  • •Replay from stored offsets is needed

✗ When Not to Use

  • •Low volume where one queue suffices
  • •Per-message priority or delayed delivery is needed
  • •Keys are extremely skewed with no way to spread them

Pros

  • +Parallelism with per-key order
  • +Independent groups and replay
  • +Predictable scaling unit

Cons

  • −Parallelism capped by partition count
  • −Rebalances pause consumption
  • −Repartitioning breaks key-to-partition mapping

Cost Profile

Infrastructure

High - partitioned log cluster

Operational

High - capacity and rebalance tuning

Cognitive

High - offsets, rebalances, keys

Failure Modes

  • Hot key overloads one partition while others idle

  • Rebalance storm halts the whole group

  • Offset committed before processing loses messages

Real-World Examples

Alternatives

    Related Patterns

    • competing-consumers
    • publish-subscribe

    Competency Domains

    distribution communicationdata state