Introducing a new way to review your PRs.

Read the announcement

aio-libs / aiokafka

asyncio client for kafka

View on GitHub

19 documents · Updated Nov 23, 2026

docs / group-assignment-strategies

Consumer group partition assignment strategies

Consumer group partition assignment strategies decide which member owns each subscribed topic partition after a group join. The group leader uses member subscriptions, current cluster metadata, and, for sticky assignment, prior ownership to create a complete assignment that the coordinator sends back to all members. For the wider consumer lifecycle, see consumer subsystem, consumer group coordination, and consumer fetch and position.

Assignment protocol

The assignment protocol carries two related records: member metadata describes a member's subscription, and member assignment describes the partitions that member may process. The protocol name selects a common assignor that all group members support, while the assignor turns the full member set into one assignment per member.

References: aiokafka/…/protocol.py• L8-47 aiokafka/…/abstract.py• L14-64

Group join inputs

Each member sends one metadata record for every supported strategy during JoinGroup. The record contains the strategy version, the member's topic subscription, and optional user data; the coordinator chooses the strategy that the group can use and the elected leader reads the metadata for every member.

References: aiokafka/…/group_coordinator.py• L429-464 aiokafka/…/group_coordinator.py• L1256-1287 aiokafka/…/protocol.py• L8-17

The assignor also reads cluster metadata to learn the available partition numbers. A partition is eligible only for members subscribed to its topic, and the result includes an assignment record for every current member, including members that receive no partitions.

References: aiokafka/…/abstract.py• L24-40 aiokafka/…/range.py• L42-73 aiokafka/…/roundrobin.py• L59-96

Rebalance result

The leader returns one encoded assignment for the group generation. Each member decodes its record, replaces its local ownership with the listed partitions, and gives the selected assignor a chance to save state for the next rebalance. This makes assignment a group-wide decision even though each member receives only its own result.

References: aiokafka/…/group_coordinator.py• L473-504 aiokafka/…/protocol.py• L20-40

The following flow shows how subscriptions and cluster metadata become member ownership.

flowchart LR
    M[Member subscriptions] --> J[Join group metadata]
    C[Cluster topic and partition metadata] --> L[Group leader]
    J --> L
    L --> A[Selected assignor]
    A --> S[Per-member assignments]
    S --> R[Members replace local ownership]

Range assignment

The range assignor balances each topic independently. It sorts a topic's partitions by number and the interested members by member ID, then gives each member a contiguous range; when a division has a remainder, the first members receive one extra partition.

References: aiokafka/…/range.py• L15-31 aiokafka/…/range.py• L42-73

This per-topic rule can give one member more total partitions across several topics even when each topic is divided as evenly as possible. It is therefore most predictable when topic-local ranges are more useful than an even total across the whole group.

References: aiokafka/…/range.py• L17-30 tests/…/test_assignors.py• L67-88

Round-robin assignment

The round-robin assignor creates one ordered list of all subscribed topic partitions and cycles through members in sorted member-ID order. It skips a member when that member is not subscribed to the current partition's topic.

References: aiokafka/…/roundrobin.py• L17-24 aiokafka/…/roundrobin.py• L59-89

When all members have the same subscriptions, this produces a near-even total: ownership counts differ by no more than one. Different subscriptions break that guarantee because skipped members change which members receive later partitions.

References: aiokafka/…/roundrobin.py• L19-24 aiokafka/…/roundrobin.py• L34-47

Sticky assignment

The sticky assignor balances eligible partitions while trying to keep existing ownership in place during a rebalance. It reads prior assignments and their generation from member user data, removes ownership that is no longer valid, and then moves only enough eligible partitions to improve balance.

References: aiokafka/…/sticky_assignor.py• L73-98 aiokafka/…/sticky_assignor.py• L667-689 aiokafka/…/sticky_assignor.py• L751-834

Balance guarantee

Sticky assignment is fully balanced when member partition counts differ by at most one. If subscriptions make that exact result impossible, it stops when a member cannot receive an eligible partition from a member with two or more extra partitions. It never assigns a partition to a member that is not subscribed to its topic.

References: aiokafka/…/sticky_assignor.py• L671-689 aiokafka/…/sticky_assignor.py• L454-497 tests/…/test_assignors.py• L998-1068

Rebalance preservation

On a rebalance, sticky assignment preserves valid prior ownership before it considers broader moves. A member that leaves, a topic subscription that changes, or a partition that disappears can force revocation; the remaining valid ownership then stays in place unless a move is needed for the balance rule.

References: aiokafka/…/sticky_assignor.py• L245-294 aiokafka/…/sticky_assignor.py• L378-413

The following flow shows the priority between valid ownership, balance, and movement.

flowchart TD
    P[Prior assignments] --> V[Keep valid ownership]
    S[Current subscriptions] --> E[Find eligible members]
    M[Current cluster metadata] --> E
    V --> B[Balance eligible partitions]
    E --> B
    B --> N[New member assignments]
    B -->|Must revoke invalid ownership| R[Remove invalid ownership]
    R --> B

Sticky user data is empty before a member has a prior assignment. After assignment, the member stores its assigned partitions and generation for the next join. If that data cannot be decoded, the assignor ignores the old ownership and starts that member without a prior assignment.

References: aiokafka/…/sticky_assignor.py• L79-98 aiokafka/…/sticky_assignor.py• L803-834 aiokafka/…/sticky_assignor.py• L836-863

The sticky assignor keeps its candidate members ordered by current partition count. This order lets the balance pass consider the least-loaded eligible member first and the most-loaded member last while it moves partitions. The ordering view is backed by a set, so changes to a member's assignment remove and add its current entry before the next balance decision.

References: aiokafka/…/sticky_assignor.py• L117-128 aiokafka/…/sticky_assignor.py• L434-452 aiokafka/…/sorted_set.py• L12-84

Strategy selection

The consumer supplies an ordered list of assignors. Members advertise the strategies in that list, and the group keeps using a strategy that all members support until the group can move to a new common choice. The default consumer preference in this repository is round-robin.

References: aiokafka/…/consumer.py• L119-127 aiokafka/…/consumer.py• L401-417 aiokafka/…/group_coordinator.py• L1267-1287

The strategy choice affects only automatic group ownership. It does not change the partition data or the consumer's fetch position; it changes which member is responsible for processing each partition after a rebalance.

References: aiokafka/…/group_coordinator.py• L479-504 aiokafka/…/protocol.py• L20-40

Strategy comparison

The strategies use the same member and cluster inputs but apply different priorities to balance and movement.

StrategyMain orderingBalance shapeRebalance behavior
RangePartitions and members are ordered per topicEach topic is divided as evenly as possibleRebuilds topic ranges from current subscriptions
Round-robinAll subscribed partitions share one member cycleNear-even total only when subscriptions matchRebuilds the global cycle and can move many partitions
StickyEligible partitions and prior ownership guide reassignmentAs even as possible under subscription limitsPreserves valid ownership unless balance requires movement

References: aiokafka/…/range.py• L17-31 aiokafka/…/roundrobin.py• L19-47 aiokafka/…/sticky_assignor.py• L671-689

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the strategy name, protocol version, metadata shape, and assignment shape aligned across ConsumerProtocol, each assignor, and group join handling.
  • When adding or changing a strategy, keep the member preference order compatible during rollout so all members can agree on one protocol.
  • If sticky user data changes, preserve the link between on_assignment, the next metadata call, and generation handling.
  • If subscription or cluster metadata rules change, re-check that no assignor gives a partition to an ineligible member or omits a current member's assignment record.
  • If the sticky balance ordering changes, keep the sorted view in sync with each assignment mutation. A stale first or last member can change which partition moves and weaken the balance or stickiness result.

Important failure modes

TriggerConsequenceGuard
Members do not share a supported strategyThe group cannot select a common assignment protocolKeep old and new strategies in the preference list during a strategy change
Cluster metadata has no partition data for a subscribed topicThat topic contributes no partitions to the resultWait for current metadata before the leader performs assignment
A member changes its subscription while retaining old ownershipThe old ownership becomes invalid and must be revokedSticky assignment checks topic eligibility before preserving prior ownership
Sticky member user data is unreadableThe member loses the benefit of prior ownership for that rebalanceTreat the data as empty prior assignment and continue with current subscriptions
More members are eligible than available partitionsSome members receive an empty assignmentThe assignors return an assignment record for every current member
The sticky ordering view does not reflect an assignment changeThe balance pass can choose the wrong least- or most-loaded memberRemove the old member entry and add its updated assignment after each move

Key files

FileWhy to read it
aiokafka/…/protocol.pyDefines the metadata and assignment records exchanged by the group protocol.
aiokafka/…/group_coordinator.pyShows how join metadata, the elected leader, assignor selection, and member assignments connect.
aiokafka/…/abstract.pyStates the common contract shared by assignment strategies.
aiokafka/…/range.pyShows per-topic range assignment and its remainder rule.
aiokafka/…/roundrobin.pyShows global partition ordering and subscription-aware cycling.
aiokafka/…/sticky_assignor.pyShows balance limits, prior-assignment data, and movement preservation.
aiokafka/…/sorted_set.pyShows the ordered view that supports sticky balance decisions.
aiokafka/…/consumer.pyDocuments strategy preference order and the default strategy passed to group coordination.