Within a consumer group, each partition is consumed by a single consumer. When consumers join the group, one of them computes the assignment which consists of the list of partitions each consumer will handle.
In your client, this can be configured via partition.assignment.strategy
. This defaults to range
which follows the implementation of Apache Kafka's RangeAssignor
.
Quoting the Javadoc:
The range assignor works on a per-topic basis. For each topic, we lay out the available partitions in numeric order and the consumers in lexicographic order. We then divide the number of partitions by the total number of consumers to determine the number of partitions to assign to each consumer. If it does not evenly divide, then the first few consumers will have one extra partition.
For example, suppose there are two consumers C0 and C1, two topics t0 and t1, and each topic has 3 partitions, resulting in partitions t0p0, t0p1, t0p2, t1p0, t1p1, and t1p2.
The assignment will be:
C0: [t0p0, t0p1, t1p0, t1p1]
C1: [t0p2, t1p2]
Consumers are ordered by their member ID which is a generated on the broker side. It is based on the consumer client.id
and a random UUID.
In practice, I does not matter which consumer gets assigned each partition so I wouldn't focus too much of that part. Instead it's important to understand how partitions are assigned and identify the strategy that works best for your use cases.
For completeness, confluent-kafka-go
also supports other strategies such as: roundrobin
and cooperative-sticky
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…