Updated on 2024-10-16 GMT+08:00

Handling Message Accumulation

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 is slower than the server's sending. Accumulated messages cannot be consumed in time.

Causes of accumulation

The following are some main causes:

  • Producers produce messages too fast for consumers to keep up.
  • Incapable consumers (low concurrency and long processing) cause lower efficiency of consumption than production.
  • Abnormal consumers (faulty and network error) cannot consume messages.
  • Improper topic partitions, or no consumption in new partitions.
  • Frequent topic rebalancing reduces consumption efficiency.

Solution

Accumulation can be avoided by the consumer, producer, and server.

  • Consumer
    • Add consumers (for consumption concurrency) based on actual needs. Use the same number of consumers as the number of partitions, or ensure that the number of partitions is an integer multiple of the number of consumers.
    • 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.
  • 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.

  • Server
    • 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.