The labs on this site need JavaScript to run. The same content is written out below.
Learn › Architecture and operations › Messaging and asynchronous processing
I added consumers, so why is the throughput the same?
Why it exists — In an ordinary message queue, the consumer deletes a message once it is processed, so it is hard for another system to read the same message again, and attaching several consumers mixes up the processing order. Kafka divides a topic into several partitions, keeps the order within each partition while consumers divide the reading among themselves, and does not delete a message just because it was read, so that each consumer group can read separately.
Where it's used today — It is used in structures where one order event is received and processed by the payment, shipping, and notification services separately, and where records that pile up many per second, such as clicks and logs, are collected. Messages whose order matters, such as events of the same order, are sent to the same partition with the same key. When there are few messages and only task distribution is needed, a queue such as RabbitMQ is sometimes used.
A topic is split into partitions, and the consumers of a consumer group divide the partitions among themselves. Within a group, a partition can be read by only one consumer.
Checked against real execution · All 324 combinations were run on Apache Kafka 4.1.1 (Docker apache/kafka:4.1.1) with kafka-clients 4.2.1 and checked (time converted to counts, default assignor range)
// Only the lines inside a method are shown (consumer setup, the example Order and handle declarations, class, and imports omitted)
// Topic orders: 3 partitions
// Consumer group order-service: 2 consumers
while (true) {
ConsumerRecords<String, Order> records = consumer.poll(Duration.ofMillis(500));
for (var r : records) {
handle(r.value()); // first, process
}
consumer.commitSync(); // then, record "read up to here"
}
All 324 combinations (1, 3, and 6 partitions, 1, 2, and 6 consumers, an inflow of 30, 120, and 300 per second, processing of 10, 50, and 100 per second, 2 commit timings, with and without a failure) were run on a real Kafka and checked. The broker is Apache Kafka 4.1.1 (the Docker image apache/kafka:4.1.1, 1 broker, only inside 127.0.0.1 of this computer), the client is kafka-clients 4.2.1, and the JDK is 21.0.12.1. Time was converted to counts so that every run gives the same numbers. "r messages per second arrive for 10 seconds" was done by putting r×10 messages into the topic, the same number in each partition (the producer gives the partition number directly), and "one consumer processes k messages per second" was done by letting each consumer process up to k×10 messages and then stop. So the throughput per second is the number of messages the consumers really processed divided by 10, and the messages waiting are the sum of the end offset minus the committed offset that the broker held at that moment. The loop of the consumer has the same shape as the code on the screen (poll, handle, commitSync), and the settings are enable.auto.commit=false, auto.offset.reset=earliest, max.poll.records=5 (one poll returns 5 records), session.timeout.ms=6000, and heartbeat.interval.ms=1500, with the rest left at the defaults. The partition assignment is the result that came out of the default settings of kafka-clients 4.2.1 (group.protocol=classic, and the assignor name the broker reported is range). This assignor grouped the partitions in number order and handed them out starting with the consumer whose client.id comes first (with 3 partitions and 2 consumers, consumer 1 gets partitions 0 and 1, and consumer 2 gets partition 2). If you change the assignor (for example CooperativeStickyAssignor) or use the new consumer group protocol (group.protocol=consumer), which consumer handles which partition and how much stops during a rebalance can be different, and those cases were not run. How the capacity of a consumer that handles several partitions is divided among them depends on the order in which messages are fetched, so the screen shows only the totals per consumer. In this run the messages were put in beforehand, so the partition with the lower number was emptied first. In the failure combinations, consumer 1 ran as a separate Java process, fetched 5 records, and the process was ended as it was right after it processed the third one (no close, and no request to leave the group). After that it was checked that the committed offset on the broker was the start of the 5 fetched records with commit after processing, and right after the 5 records with commit before processing. The broker removed that consumer from the group and finished rebalancing 3.7 to 7.2 seconds later (this run reduced session.timeout.ms from the default of 45 seconds to 6 seconds, and this time changes on every run). At that time the remaining consumers gave up all the partitions they were handling and were assigned again (checked with onPartitionsRevoked of ConsumerRebalanceListener). The first offset that the consumer taking over processed on that partition was the same as the committed position, and after the consumers were made to process everything that was waiting, all the handle records were counted, which confirmed 3 duplicates (the first three) with commit after processing and 2 lost messages (the last two) with commit before processing. The throughput and the amount waiting after the reassignment were checked against values measured by first emptying everything that was waiting and then putting in another 10 seconds of messages with the new assignment. In the failure combinations with only one consumer, it was checked that the group becomes empty, that the lag grew by exactly the amount of another 10 seconds of messages, and that a consumer started again read from the committed position. The time a rebalance takes and the delay during it are not given as numbers on the screen. It was also checked by running that the processing order within a partition was the same as the offset order, that trying to reduce the partitions from 3 to 2 raises InvalidPartitionsException while increasing them to 6 works, and that messages with the same key go to the same partition and, when the partitions were increased from 3 to 6, the partition changed for 6 of 10 keys. That messages remain for the retention period and the names of the delivery guarantees (at-least-once, at-most-once) follow the official Kafka documentation. The check is done by verify/net/cmp_kafka.js.
Previous lab: Message queue basics · Next lab: Circuit breaker
Struck-through text was removed from the first version of the code; highlighted text was added.