Overview
Kafka divides each topic into multiple partitions for distributed message storage. Within the same consumer group, each consumer can consume multiple partitions at the same time, but each partition can be consumed by only one consumer at a time.
Unprocessed messages accumulate if the client's consumption speed is slower than the broker's sending speed. Accumulated messages cannot be consumed in time and cause message consumption delay.
Causes of accumulation:
l Producers produce messages too fast for consumers to keep up.
l Incapable consumers (low concurrency and long processing) cause lower efficiency of consumption than production.
l Abnormal consumers (faults and network errors) cannot consume messages.
l Improper topic partitions (leader replicas are concentrated in some brokers or insufficient partitions), or no consumption in new partitions.
l Frequent topic rebalancing reduces consumption efficiency.
Procedure
Accumulation can be avoided by the consumer, producer, and broker.
l Consumer
¡ Add consumers (consumption concurrency) based on actual needs. The number of consumers should be consistent with the number of partitions.
¡ Speed up consumption by optimizing the consumer processing logic (less complicated computing, API invoking, and database reading).
¡ Increase the number of messages in each poll: Polling/Processing speed should be equal to or higher than the production speed.
l Producer
¡ Attach a random suffix to each message key so that messages can be evenly distributed in partitions. (In actual scenarios, attaching a random suffix to each message key compromises global message sequence. Decide whether a suffix is required by your service.)
l Broker
¡ Set the number of topic partitions properly. Add partitions without affecting processing efficiency.
¡ Stop production when messages are accumulating or forward them to other topics.