Avoiding MQS Message Accumulation
Solution 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 will 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.
Procedure
Accumulation can be avoided by the consumer, producer, and server.
- Consumer
- Add consumers (consumption concurrency) based on actual needs. The number of consumers should accord 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 pull: Pulling/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.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot