Kafka
StickyAssignor
Consumer Group
Message Delivery
Troubleshooting Kafka

Kafka StickyAssignor breaking delivery to single consumer in the group

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a distributed event streaming platform capable of handling trillions of events a day. One of Kafka's strengths lies in its flexible consumer group feature, where a set of consumers share a common group ID to jointly process records published in the topics they subscribe to. In Kafka, the assignment of topic partitions to consumers within a group is managed by partition assignment strategies, among which the StickyAssignor is a popular choice due to its attempt to minimize partition movement across consumers.

Understanding StickyAssignor

The StickyAssignor algorithm aims to maintain a stable partition assignment even as consumers join or leave the group. It tries to ensure that partition assignments stick with the original consumer as much as possible. This stickiness is beneficial because it reduces the need for extensive rebalancing and minimizes the potential for duplicate message processing during consumer changes.

Issue: Breaking Delivery to Single Consumer in the Group

Occasionally, usage of the StickyAssignor can lead to scenarios where one consumer in a group might end up not receiving any partitions, especially in groups with a small number of partitions relative to the number of consumers. This can occur due to the inherent design of how the StickyAssignor tries to distribute partitions as evenly as possible while trying to maintain previous assignments.

Technical Explanation

Consider a scenario with 3 partitions (P0, P1, P2) and 3 consumers (C0, C1, C2) in a consumer group. If all consumers have been active and are reassigned together, the sticky nature will attempt to allocate the same partitions to the same consumer:

  • C0 gets P0
  • C1 gets P1
  • C2 gets P2

If C1 is then stopped and a reassignment occurs, C0 and C2 might still attempt to stick with P0 and P2, respectively. When C1 rejoins, another reassignment is triggered. Ideally, C1 should get back P1, but based on the state of the current consumers and partitions, C1 might end up without any partitions if the algorithm determines that sticking with previous assignments (C0 with P0 and C2 with P2) as exactly before provides a more minimal change.

Example with Code

Here is a hypothetical snippet illustrating a potential breakpoint where rebalancing might result in a consumer getting no assignments:

java
1// Kafka consumer configuration
2Properties props = new Properties();
3props.put("bootstrap.servers", "localhost:9092");
4props.put("group.id", "test-group");
5props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
6props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
7props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.StickyAssignor");
8
9KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
10consumer.subscribe(Arrays.asList("test-topic"));
11
12// Rebalance listener to track partition assignment
13consumer.subscribe(topics, new ConsumerRebalanceListener() {
14    public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
15        System.out.println("Partitions revoked:" + partitions);
16    }
17
18    public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
19        System.out.println("Partitions assigned:" + partitions);
20    }
21});

Key Points Summary

Key PointDescription
StabilityStickyAssignor maintains a stable partition distribution during normal operations.
Problematic RebalancingCan cause an uneven distribution during certain transient events such as consumers leaving or joining.
Cumbersome in Small GroupsMore apparent in consumer groups with a partition count close to or less than the number of consumers.
Potential SolutionMonitoring and potentially adjusting the partition.assignment.strategy can mitigate issues.

Subtopics for Enhancement

  • Comparative Analysis: Comparison with other assignment strategies like Range or RoundRobin.
  • Consumer Group Health Monitoring: Tools and techniques to monitor and diagnose consumer group behavior.
  • Advanced Configuration and Tuning: How to fine-tune the consumer configs for optimal partition distribution.

Conclusion

While the StickyAssignor provides benefits in maintaining consumer-partition affinity, its drawbacks particularly in corner cases where consumer count matches or exceeds partition count should be accounted for. A deeper understanding and careful monitoring are required to ensure efficient consumption of messages without sacrificing throughput or risking unassigned consumers.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.