A consumer-side load balancing method based on Kafka

Through the unified management of consumer load balancing by the coordinator, the problem of inefficiency in the Kafka consumer load balancing process is solved, and more efficient load balancing and system stability are achieved.

CN113946449BActive Publication Date: 2024-10-29BEIJING UNIV OF TECH
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202111271831.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-10-29
Publication Date
2024-10-29
Estimated Expiration
2041-10-29

AI Technical Summary

Technical Problem

The existing Kafka consumer load balancing algorithm causes increased consumer load and reduced efficiency when processing a large number of messages, and when each consumer independently completes the load balancing task, it increases energy consumption and reduces efficiency.

Method used

A coordinator is used to uniformly manage consumer load balancing, identify abnormal consumers through heartbeat detection, and dynamically adjust the correspondence between partitions and consumers to reduce unnecessary load balancing attempts and improve efficiency.

Benefits of technology

It improves the efficiency of load balancing and the system's message processing capabilities, avoids erroneous load balancing attempts, and improves the system's stability and message processing capabilities.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113946449B_ABST
    Figure CN113946449B_ABST
Patent Text Reader

Abstract

The present invention discloses a consumer-side load balancing method based on Kafka, and belongs to the technical field of message processing. In order to solve the problem that consumers are isolated from each other in the existing consumer-side load balancing method and cannot efficiently handle the problem of decreased consumer message processing capability caused by consumer downtime, an improved consumer-side load balancing method is proposed. In the entire process from producer cluster producing messages, broker storing messages to consumer cluster receiving messages in the Kafka message transmission system, the corresponding situation of consumer allocation and partition is uniformly managed by the coordinator selected at the beginning of load balancing, and different allocation plans are formulated according to the partition load situation, which well solves the problem of consumers being isolated from each other in the existing load balancing strategy, can help the Kafka system to efficiently and quickly handle sudden load balancing events, and improve the applicability of Kafka in the production environment.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention specifically relates to a consumer-side load balancing method based on Kafka, and belongs to the technical field of message processing. Background Art

[0002] Kafka is a leader in message middleware and an important means to solve the message delivery problem of distributed systems. Kafka was developed by LinkedIn using the Scala language and later became an open source project of the Apache Software Foundation. It has the advantages of supporting multi-language development on the application side and supporting real-time large-scale message processing. Kafka mainly consists of producers, consumers, and brokers. Generally speaking, a Kafka cluster consists of multiple broker nodes. When a producer generates a new message, it will be divided and classified into a certain topic. Each topic is divided into multiple partitions, which are deployed on multiple brokers. The producer continuously sends messages to a certain type of topic. The proxy server temporarily stores messages of different topics and then forwards them to consumers. Finally, the consumer processes the messages generated by the producer.

[0003] With the advent of the big data era, more and more scenarios are using message middleware, and the biggest feature of Kafka is that it sends and receives messages very quickly. Kafka can process hundreds of thousands of messages per second. In today's increasingly fast-paced world, Kafka also has good fault tolerance, allowing some nodes in the cluster to crash. When the subscription traffic or the message flow that needs to be transmitted surges, if some producers and some consumers crash, a large number of messages will flood into the remaining surviving consumers. If the number of consumers is far less than the producer at this time, the consumer load will increase, the message processing speed will decrease, and more system resources will be consumed. At this time, the Kafka load balancing mechanism will be triggered, and Kafka will automatically balance the correspondence between consumers and information partitions, reducing the problem of decreased message processing capabilities due to the reduction or increase in consumers, and improving the applicability of Kafka in the production environment.

[0004] Therefore, in view of the above problems in the existing consumer load balancing strategy, the present invention proposes an optimized load balancing method for the existing load balancing method. At present, the algorithm for consumer load balancing of Kafka is completed by each consumer individually through the distributed coordination service Zookeeper, but this algorithm has some problems. The message transmission process cannot always be carried out smoothly. When a large amount of data flows in a short period of time, it will cause the consumer load to increase. At this time, load balancing is required to balance the situation that each consumer is responsible for the partition. In addition, when consumers increase or decrease, the consumers of the client need to perform load balancing independently. Summary of the invention

[0005] In order to solve the above problems, the present invention proposes a consumer load balancing algorithm based on Kafka, which can solve the problem of increased energy consumption and reduced efficiency caused by each consumer independently completing the load balancing task during the consumer load balancing process. The optimized consumer load balancing algorithm includes a producer cluster composed of multiple producers and a consumer cluster composed of multiple consumers. Each consumer in the cluster subscribes to messages from the server, and each consumer obeys the command of the coordinator, and the coordinator uniformly allocates and deploys the transmission of information.

[0006] The specific scheme of the present invention is as follows: A consumer-side load balancing method based on Kafka specifically comprises the following steps:

[0007] 1) The producer cluster generates messages and publishes them to the broker;

[0008] 2) Broker receives and stores messages in a distributed manner to the server disk;

[0009] 3) The consumer cluster subscribes to messages from the Kafka cluster;

[0010] 4) The Kafka cluster sends the message to the consumer cluster;

[0011] 5) The coordinator obtains partition and consumer information, and sends heartbeat detection to all consumers at regular intervals. The coordinator is the consumer with the longest survival time in all consumer groups of Kafka. If the survival time is the same, a consumer is randomly selected as the coordinator; the coordinator is used to receive the subscription information sent by each consumer, and uniformly allocates the corresponding relationship between partitions and consumers;

[0012] 6) When the coordinator fails to receive the heartbeat detection signal returned by a consumer, it indicates that the consumer is abnormal at this time, and the load balancing is immediately triggered. The coordinator detects the load of the consumer group where the abnormal consumer is located and the partition corresponding to the abnormal consumer;

[0013] If there is a partition with the largest load, all partitions in the topic where the partition with the largest load is located are sorted and stored in the data table. The number of partitions is n, and all consumers in the consumer group corresponding to the topic are sorted in reverse order. The number of consumers is m. If n=m at this time, partitions and consumers are matched one by one; if n>m at this time, one consumer corresponds to n / m partitions. When n / m is not divisible, each consumer corresponds to n / m partitions rounded up. Regardless of whether n / m is an integer, when the partitions are allocated to the last time, the remaining partitions are allocated to the last consumer;

[0014] If there is no partition with the heaviest load, x consumers will be added or subtracted to ensure that the number of consumers after the change is equal to the minimum integer multiple of the number of partitions, and then the partitions will be evenly redistributed to consumers in order;

[0015] 7) After the load balancing is completed on the consumer side, the message transmission process is resumed.

[0016] In the above method, the heartbeat detection means that the coordinator sends a heartbeat to each consumer at a certain interval to prove that the consumer is alive. If the heartbeat returned by the consumer is not received within the set time threshold, the consumer is considered to be down and is removed from the consumer group.

[0017] After the present invention adopts the above technical solution, compared with the existing solution, it better improves the corresponding allocation solution in the process of consumer load balancing. In this solution, each load balancing is initiated only by the coordinator, and each consumer does not need to constantly detect whether there is a downtime in its consumer group or other abnormal situations that cannot contact other consumers in the group. Each load balancing is uniformly managed and controlled by the coordinator, which improves the load balancing efficiency of the consumer group and avoids erroneous load balancing attempts. At the same time, this method can initiate load balancing in time when an abnormal trend appears in the consumer group, adjust the working state of the entire system, and improve the system's message processing capabilities and system stability. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] Figure 1 This is the Kafka system block diagram.

[0019] Figure 2 Flowchart of the optimization algorithm.

[0020] Figure 3 Load balancing effect Figure 1

[0021] Figure 4 Load balancing effect Figure 2 DETAILED DESCRIPTION

[0022] The following is combined with Figure 1-4 The present invention is further described as follows:

[0023] The present invention provides a load balancing method based on Kafka, which is based on the Kafka message system. The specific process of the system is as follows: Figure 1 As shown, first, the producer cluster composed of producers publishes messages and sends the information to be transmitted to the proxy server. The broker classifies the messages and stores them on the broker in different Topics according to the different types of messages. The topic will be divided into several partitions. Different partitions of the same topic may not be on the same machine, but may be deployed on multiple machines. The consumer group composed of consumers will subscribe to different partitions of the Topic if they want to subscribe to information. In the Kafka cluster, there is a consumer that acts as the coordinator of the entire consumer group. It is responsible for the consumption displacement submission and load balancing of one or more consumers. Each consumer will submit the subscription status to the coordinator, and the coordinator will uniformly allocate the initial partition. Generally speaking, the identity of the coordinator will not change unless it goes offline for special reasons, so the coordinator itself is also a consumer. When the coordinator does not receive the heartbeat of a consumer for a period of time, it means that the consumer has an abnormal situation and cannot continue to consume the information of the corresponding partition. At this time, the load balancing of the coordinator will be triggered.

[0024] Due to emergencies such as consumers going offline or crashing due to their own reasons, the messages in the partitions that these consumers are responsible for cannot be received and processed in time. At this time, the entire Kafka message system will have problems such as untimely message transmission and delayed messages. In order to solve the problems caused by such emergencies, the coordinator will first determine whether there is a partition with the largest load. If so, all partitions in the topic where the partition with the largest load is located will be sorted. If not, the number of consumers will be adjusted first and then redistributed. The specific allocation work is to sort the IDs of consumers in the consumer group. If the number of partitions is equal to the number of consumers, the two will be matched one by one; if the number of partitions is greater than the number of consumers, the number of consumers will be rounded up and then matched with the partitions. At this time, there will be no imbalanced information distribution in the entire Kafka system, and the load balancing work has been completed.

[0025] The entire Kafka system consists of producers, consumers, and brokers that store information, such as Figure 1As shown in the figure, broker is a physical definition. Each server that stores information in the Kafka message system is called a broker. Compared with physical partitions, logical partitions are based on topics, that is, Topics. A topic can be regarded as a collection of messages. Each Topic can be pushed (push) by many producers and received (pull) by many consumers, and each Topic can be divided into multiple partitions. Different partitions under the same Topic contain different messages. When each message is added to a partition, it will be assigned an offset, which is the unique number of the message in the partition. Kafka uses offsets to ensure the order of messages in the partition.

[0026] If a consumer crashes and loses the ability to receive information, the message pull work will be suspended and the corresponding relationship between consumers and partitions will be redistributed, which triggers load balancing. Figure 3 Figure 4 As shown. Figure 3 In the case of consumer 3 going offline due to downtime, no one will receive the messages in Partition 3 that were originally handled by consumer 3, which will cause a delay in message transmission in partition 3. Since the coordinator cannot receive the heartbeat detection feedback from consumer 3 at this time, consumer 3 will be directly removed, and load balancing will be started, message transmission will be suspended, and the number of consumers in the consumer group that are capable of receiving messages will be checked. The number of partitions in the current topic will also be checked. At this time, it is known that partition 3 has the largest load and there are many messages to be processed. All partitions in this topic will be sorted according to actual conditions, and the surviving consumers will be sorted. Then, according to the sequence order, it is calculated that one consumer should correspond to two partitions at this time. Therefore, partitions 2 and 3 will correspond to consumer 2 in reverse order. At this time, partition 1 is left, and the number does not meet the requirement of one consumer corresponding to two partitions. At this time, partition 1 will correspond to consumer 1. Before the down consumer is restarted, this partition will be accepted by a new consumer. Figure 4 In the example, two consumer groups with a total of four consumers receive four partitions in the same topic respectively. Each partition corresponds to two consumers in different consumer groups. If a consumer in consumer group 2 suddenly goes offline, the coordinator will not receive the heartbeat signal from consumer 4 and will immediately trigger load balancing. However, at this time, all partitions in the topic do not have information that cannot be received, that is, there is no partition with the largest load. At this time, the coordinator will add a new consumer to replace the previous offline consumer, and the new consumer will re-receive messages from partitions 3 and 4, so as to achieve a balance between one partition and two consumers, and load balancing is completed at this time.

Claims

1. A consumer-side load balancing method based on Kafka, based on a Kafka distributed log system consisting of a producer cluster, a consumer cluster, and a Kafka cluster, wherein: The producer cluster is composed of several producers that publish messages to Kafka topics, and the consumer cluster is composed of several consumers that subscribe to Kafka topic messages. The Kafka cluster is composed of several independent Kafka servers, namely brokers. Brokers are used to receive messages from producers and store them on server disks. During the message transmission process, due to the downtime of a consumer in the consumer group in the system, other consumers automatically reallocate the subscription topic partitions. This process is called load balancing, which is characterized by the following steps: 1) The producer cluster generates messages and publishes them to the broker; 2) The broker receives and sends distributed storage messages to the server disk; 3) The consumer cluster subscribes to messages from the Kafka cluster; 4) The Kafka cluster sends the message to the consumer cluster; 5) The coordinator obtains partition and consumer information, and sends heartbeat detection to all consumers regularly. The coordinator is the consumer with the longest survival time in all consumer groups of Kafka. If the survival time is the same, a consumer is randomly selected as the coordinator; the coordinator is used to receive the subscription information sent by each consumer, and the coordinator uniformly allocates partitions and determines the corresponding relationship between partitions and consumers; 6) When the coordinator fails to receive the heartbeat detection signal returned by a consumer, it indicates that the consumer is abnormal at this time, and the load balancing is immediately triggered. The coordinator detects the load of the consumer group where the abnormal consumer is located and the partition corresponding to the abnormal consumer; If there is a partition with the largest load, all partitions in the topic where the partition with the largest load is located are sorted and stored in the data table. The number of partitions is n, and all consumers in the consumer group corresponding to the topic are sorted in reverse order. The number of consumers is m. If n=m at this time, partitions and consumers are matched one by one; if n>m at this time, one consumer corresponds to n / m partitions. When n / m is not divisible, each consumer corresponds to n / m partitions rounded up. Regardless of whether n / m is an integer, when the partitions are allocated to the last time, the remaining partitions are allocated to the last consumer; If there is no partition with the heaviest load, x consumers will be added or subtracted to ensure that the number of consumers after the change is equal to the minimum integer multiple of the number of partitions, and then the partitions will be evenly redistributed to consumers in order; 7) After the load balancing is completed on the consumer side, the message transmission process is resumed.

Citation Information

Patent Citations

  • Kafka cluster load balancing method, system and device and medium

    CN112272217A

  • Distributed scheduling device of hardware transcoding acceleration equipment and use method thereof

    CN113347430A