Go Kafka Consumer Group Usage
Contents
Go Kafka Consumer Group Usagegogolangkafka
Based on Go 1.18 and Kafka 2.4.1, this post consumes messages via a Kafka Consumer Group, provides a best-practice example and configuration tips, and reduces timeout issues caused by Rebalance.
Copyright notice: This is an original article by xwi88, licensed under CC BY-NC 4.0. Commercial use is prohibited; please cite the source when reposting. Follow at https://github.com/xwi88
Consumer Group
A consumer group is Kafka’s scalable, fault-tolerant consumer mechanism. A few properties:
- A
consumer groupis made up of one or moreconsumers that together consume messages from one or moretopics - A
consumer groupis uniquely identified by itsgroup ID; consumers in the group share thatgroup ID - Each partition of a
topiccan be consumed by only oneconsumerwithin the same group, but can be consumed by consumers in different groups
Tip
- The partition count should exceed the number of consumers; if throughput is insufficient, consider adding consumers
- When simply adding consumers no longer boosts throughput notably, optimize your consumption logic
- The number of consumers must be bounded to avoid excessively long
rebalances
Rebalance
Partition rebalancing (Rebalance) is a key Kafka feature that guarantees high availability and horizontal scaling. The following trigger a Kafka rebalance:
- Consumer-group membership changes:
- A new consumer joins
- An existing consumer leaves: voluntary leave → voluntary
rebalance - An existing consumer fails or crashes: if it stops sending heartbeats beyond a threshold it’s considered dead; detection takes one
session.timeoutcycle → involuntaryrebalance
- The number of subscribed
topics changes (e.g. regex subscription where matchingtopics change) - The partition count of a subscribed
topicchanges
Note
- Pros: guarantees high availability and scalability
- Cons: during a rebalance the whole consumer group is unavailable; rebalancing forces consumers to refresh state, and old consumer state expires, lowering consumer capacity
Go Consumer Group
ConsumerGroup interface & notes
| |
ConsumerGroup Demo
| |
Selected config parameters
Net.ReadTimeout: How long to wait for a response.Group.Rebalance.Timeout: The maximum allowed time for each worker to join the group once a rebalance has begun.This is basically a limit on the amount of time needed for all tasks to flush any pending data and commit offsets. If the timeout is exceeded, then the worker will be removed from the group, which will cause offset commit failures (default 60s).Group.Session.Timeout: The timeout used to detect consumer failures when using Kafka’s group management facility. The consumer sends periodic heartbeats to indicate its liveness to the broker. If no heartbeats are received by the broker before the expiration of this session timeout, then the broker will remove this consumer from the group and initiate a rebalance. Note that the value must be in the allowable range as configured in the broker configuration bygroup.min.session.timeout.msandgroup.max.session.timeout.ms(default 10s)Group.Heartbeat.Interval: The expected time between heartbeats to the consumer coordinator when using Kafka’s group management facilities. Heartbeats are used to ensure that the consumer’s session stays active and to facilitate rebalancing when new consumers join or leave the group. The value must be set lower than Consumer.Group.Session.Timeout, but typically should be set no higher than 1/3 of that value.Group.Rebalance.Strategy: Strategy for allocating topic partitions to members (default BalanceStrategyRange), [“range”, “roundrobin”, “sticky”]MaxWaitTime:The maximum amount of time the broker will wait for Consumer.Fetch.Min bytes to become available before it returns fewer than that anyways.Retry.Backoff: How long to wait after a failing to read from a partition before trying again.
Configuration recommendations
Net.ReadTimeout: default30s; if consumers often hitread tcp xxx i/o timeout, consider raising it> session.timeout> rebalance.timeout
Group.Rebalance.Timeout: default60s; with many consumers, consider raising it; for low-TPStopics it can be loweredGroup.Session.Timeout: default10s; if a consumer stops sending heartbeats beyond this, it’s considered dead and arebalanceis triggeredshall >= 1/3 * Group.Heartbeat.Interval
Group.Heartbeat.Interval: default3s, the consumer’s heartbeat intervalGroup.Rebalance.Strategy: the rebalance strategyrange: default; per topic, n = partitions/consumers, m = partitions % consumers; the first m consumers each get n+1 partitions, the remaining (consumers − m) each get nroundrobin: sort all consumers in the group and all partitions of their subscribedtopics lexicographically, then assign partitions to consumers round-robinsticky: sticky strategy; keeps consumption as balanced as possible- partition allocation should be as even as possible — the partition counts assigned to any two consumers differ by at most one
- the allocation should stay as close to the previous one as possible
MaxWaitTime: default250ms; data is sent to the consumer after at most this long, or oncefetch.min.bytesis reachedRetry.Backoff: default2s