{"id":59350,"date":"2023-11-09T14:53:38","date_gmt":"2023-11-09T09:23:38","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=59350"},"modified":"2023-11-20T23:03:50","modified_gmt":"2023-11-20T17:33:50","slug":"consumer-rebalancing-in-kafka-an-overview","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/consumer-rebalancing-in-kafka-an-overview\/","title":{"rendered":"Consumer Rebalancing in Kafka- An Overview"},"content":{"rendered":"<h2><strong>Introduction<\/strong><\/h2>\n<p>Apache Kafka is a distributed system consisting of servers and clients that communicate via a high-performance TCP network protocol. We have components generating events (<em class=\"or\">Producers<\/em>) and components that consume those events (<em class=\"or\">Consumers<\/em>). Consumers label themselves with a consumer group name so that each record published on a topic will be delivered to one and only one consumer in the consumer group. We can use this feature to implement load balancing between consumer nodes.<\/p>\n<p><strong>Consumer Rebalancing<\/strong> in Kafka is a process by which partitions get reassigned among consumers in a group to ensure that each consumer gets an equal number of partitions to process data.<\/p>\n<p>As a result, Kafka efficiency and effective performance are maintained, thus preventing any overload or underutilization of any consumer.<\/p>\n<h2><strong>Why Consumer rebalancing triggers?<\/strong><\/h2>\n<p>Consumer rebalancing in Kafka can happen in the following conditions:<\/p>\n<p>1. A consumer leaves the group<\/p>\n<p>2. A consumer joins a group<\/p>\n<p>3. Partitions are added to a topic<\/p>\n<p>When a consumer experiences a temporary network failure or stays idle for too long, Kafka may consider it a failed consumer and remove it from the group. Meanwhile, Kafka initiates a rebalance to distribute the partitions among the active consumers in the group.<\/p>\n<p>Let\u2019s take topic T1 with three partitions, a consumer C1, which is the only consumer in group G1, and use it to subscribe to topic T1. Consumer C1 will get all messages from all three partitions.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-59344 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/11\/first.jpeg\" alt=\"\" width=\"964\" height=\"552\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/11\/first.jpeg 964w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-300x172.jpeg 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-768x440.jpeg 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-624x357.jpeg 624w\" sizes=\"(max-width: 964px) 100vw, 964px\" \/><\/p>\n<p>If we add two consumers, C2 and C3, to group G1, each consumer will only get messages from a single partition.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-59345 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-1.jpeg\" alt=\"\" width=\"981\" height=\"566\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-1.jpeg 981w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-1-300x173.jpeg 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-1-768x443.jpeg 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-1-624x360.jpeg 624w\" sizes=\"(max-width: 981px) 100vw, 981px\" \/><\/p>\n<p>If we add more consumers to this group with a single topic than we have partitions, some of the consumers will be idle and get no messages at all.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-59346 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-2.jpeg\" alt=\"\" width=\"955\" height=\"514\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-2.jpeg 955w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-2-300x161.jpeg 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-2-768x413.jpeg 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-2-624x336.jpeg 624w\" sizes=\"(max-width: 955px) 100vw, 955px\" \/><\/p>\n<h2><strong>How does Rebalancing work?<\/strong><\/h2>\n<p>There are two types of rebalancing, depending on the partition assignment strategy that the consumer group uses.<\/p>\n<p><strong>Eager Rebalancing<\/strong>: Kafka performs eager rebalancing by default, which states that all consumers stop consuming and give up their membership of partitions for a short period. This is also called a &#8220;stop the world&#8221; event.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-59348 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-4.jpeg\" alt=\"\" width=\"2500\" height=\"483\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-4.jpeg 2500w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-4-300x58.jpeg 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-4-1024x198.jpeg 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-4-768x148.jpeg 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-4-1536x297.jpeg 1536w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-4-2048x396.jpeg 2048w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-4-624x121.jpeg 624w\" sizes=\"(max-width: 2500px) 100vw, 2500px\" \/><\/p>\n<p>Afterward, Consumer rejoins the group, and reassignment of partitions occurs, this time, there is no guarantee that the consumer will get the same partition as before<\/p>\n<p><strong>Cooperative Rebalancing<\/strong>: Also referred to as incremental rebalancing. This approach involves reassigning only a small subset of the partitions from one consumer to another and allowing consumers to continue processing records from all the partitions that are not reassigned.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-59349 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-5-1024x198.jpeg\" alt=\"\" width=\"625\" height=\"121\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-5-1024x198.jpeg 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-5-300x58.jpeg 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-5-768x148.jpeg 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-5-1536x297.jpeg 1536w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-5-2048x396.jpeg 2048w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/first-5-624x121.jpeg 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>Compared to eager, there are clear advantages of using cooperative rebalancing. Kafka consumers who are not concerned by the rebalance can keep processing data without interruptions. So, this algorithm, fortunately, is the default strategy for new Kafka stream applications.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Introduction Apache Kafka is a distributed system consisting of servers and clients that communicate via a high-performance TCP network protocol. We have components generating events (Producers) and components that consume those events (Consumers). Consumers label themselves with a consumer group name so that each record published on a topic will be delivered to one and [&hellip;]<\/p>\n","protected":false},"author":1685,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":345},"categories":[446],"tags":[5550,5549,4844,1604],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59350"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1685"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=59350"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59350\/revisions"}],"predecessor-version":[{"id":59409,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59350\/revisions\/59409"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=59350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=59350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=59350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}