Check out my last article, Kafka Internals: Topics and Partitions to learn about Kafka storage internals.

In Kafka, each topic is divided into set of partitions. Producers write messages to the tail of the partitions and consumers read them at their own pace. Kafka scales topic consumption by distributing partitions among a consumer group, which is a set of consumers sharing a common group identifier. The following diagram depicts a single topic with three partitions and a consumer group with two members.

For each consumer group, a broker is chosen as a group coordinator. The group coordinator is responsible for:

  • managing consumer group state.
  • assigning a partition to a consumer when:
    • a new consumer is spawned.
    • an old consumer goes down.
    • a topic meta data changes.

The process of reassigning partitions to consumers is called consumer group rebalancing.

When a group is first connected to a broker:

  • consumers start reading from either the earliest or latest offset in each partition based on the configuration auto.offset.reset.
  • messages in each partition are then read sequentially.
  • the consumer commits the offsets of messages it has successfully processed.

In the following figure, the consumer’s position is at offset 6 and its last committed offset is at offset 1.

When a consumer group is rebalanced, a new consumer is assigned to a partition.

  • It starts reading from the last committed offset.
  • It reprocesses some messages if the old consumer has processed some messages but crashed before committing the offset of the processed messages.

In the above diagram, if the current consumer crashes and then the new consumer starts consuming from offset 1 and reprocesses messages until offset 6. Other markings in the above diagram are:

  • Log end offset is the offset of the last message written to the partition.
  • High watermark is the offset of the last message that was successfully replicated to all partition replicas.

Kafka ensures that the consumer can read only up to the high watermark for obvious reasons.

The consumer reads messages in parallel from different partitions from different topics spread across brokers using the KafkaConsumer.poll method in an event loop. The same method is used by Kafka to coordinate and rebalance a consumer group.

Let's discuss how to implement different consumption semantics and then understand how Kafka leverages the poll method to coordinate and rebalance a consumer group.

Here's some sample auto commit consumer code:

 
/** 
 
   this is code for offset auto commit i.e. Kafka Consumer library commits
 
   offset till the messages fetched in the poll call automatically after 
 
   configfured timeout for every poll
 
**/
 
public class ConsumerLoop implements Runnable {
 
 private final KafkaConsumer < String, String > consumer;
 
 private final List < String > topics;
 
 private final int id;
 
 
 public ConsumerLoop(int id,
 
  String groupId,
 
  List < String > topics) {
 
  this.id = id;
 
  this.topics = topics;
 
  Properties props = new Properties();
 
  props.put("bootstrap.servers", "localhost:9092");
 
  props.put(“group.id”, groupId);
 
  props.put(“key.deserializer”, StringDeserializer.class.getName());
 
  props.put(“value.deserializer”, StringDeserializer.class.getName());
 
  this.consumer = new KafkaConsumer < > (props);
 
 }
 
 
 @Override
 
 public void run() {
 
  try {
 
   // 1. Subscribe to topics
 
   consumer.subscribe(topics);
 
   // 2. start event loop
 
   while (true) {
 
    // 3. blocking poll call
 
    ConsumerRecords < String, String > records = consumer.poll(Long.MAX_VALUE);
 
    // 4. Process fetched message records
 
    processMessages(records);
 
   }
 
  } catch (WakeupException e) {
 
   // ignore for shutdown 
 
  } finally {
 
   // 6. close consumer
 
   consumer.close();
 
  }
 
 }
 
 
 public void shutdown() {
 
  consumer.wakeup();
 
 }
 
 
 public void processMessages(ConsumerRecords < String, String > records) {
 
  for (ConsumerRecord < String, String > record: records) {
 
   Map < String, Object > data = new HashMap < > ();
 
   data.put("partition", record.partition());
 
   data.put("offset", record.offset());
 
   data.put("value", record.value());
 
   System.out.println(this.id + ": " + data);
 
  }
 
 }
 
}
 
}
 

If a consumer crashes before the commit offsets successfully processed messages, then a new consumer for the partition repeats the processing of the uncommitted messages that were processed. Frequent commits mitigate the number of duplicates after a rebalance/crash. In the above example code, the Kafka consumer library automatically commits based on the configured auto.commit.interval.ms value and reducing the value increases the frequency of commits.

Certain applications may choose to manually commit for better management of message consumption, so let's discuss different strategies for manual commits. For manual commits, we need to set auto.commit.enable to false and use KafkaConsumer.commitSync appropriately in the event loop.

Consumption Semantics

Consume at Least Once

 
   // 2. start event loop
 
   while (true) {
 
    // 3. blocking poll call
 
    ConsumerRecords < String, String > records = consumer.poll(Long.MAX_VALUE);
 
    // 4. Process fetched message records
 
    processMessages(records);
 
    // 5. Commit after processing messages
 
    try {
 
     consumer.commitSync();
 
    } catch (CommitFailedException e) {
 
     // application specific failure handling
 
    }
 
   }
 
   } catch (WakeupException e) {
 
    // ignore for shutdown 
 
   }
 

The following diagram depicts partition traversal by a consumer from the above code:

The above code commits an offset after processing the fetched messages, so if the consumer crashes before committing then the newly chosen consumer has to repeat the processing of the messages though they are processed by the old consumer but failed to commit.

Note that auto commit ensures 'at least once consumption' as the commit is automatically done only after messages are fetched by the  poll method.

Consume at Most Once

 
   // 2. start event loop
 
   while (true) {
 
    // 3. blocking poll call
 
    ConsumerRecords < String, String > records = consumer.poll(Long.MAX_VALUE);
 
    // 4. Commit after processing messages
 
    try {
 
     consumer.commitSync();
 
    } catch (CommitFailedException e) {
 
     // application specific failure handling
 
    }
 
    // 5. Process fetched message records
 
    processMessages(records);
 
   }
 
   } catch (WakeupException e) {
 
    // ignore for shutdown 
 
   }
 

The following diagram depicts the partition traversal by the consumer performed in the above code:

The above code commits an offset before processing the fetched messages, so if the consumer crashes before processing any committed messages then all such messages are literally lost as the newly chosen consumer starts from the last committed offset, which is ahead of the last processed message offset.

Consume Almost Once

 
try {
 
 // 2. start event loop
 
 while (running) {
 
  // 3. poll for messages
 
  ConsumerRecords < String, String > records = consumer.poll(1000);
 
 
  try {
 
   // 4. iterate each message
 
   for (ConsumerRecord < String, String > record: records) {
 
    System.out.println(record.offset() + ": " + record.value());
 
    // 5. commit message that is just processed
 
    consumer.commitSync(Collections.singletonMap(record.partition(), 
 
                                                 new OffsetAndMetadata(record.offset() + 1)));
 
   }
 
  } catch (CommitFailedException e) {
 
   // application specific failure handling
 
  }
 
 }
 
} finally {
 
 consumer.close();
 
}
 

The above code iterates over messages and commits each message before immediately processing it. So, if the consumer crashes:

  • after committing a message then the new consumer will not repeat the message.
  • while processing/committing a message a new consumer has to repeat the only message that was being processed when the consumer crashed as the last commit offset.

commitSync is a blocking IO call so a consumption strategy should be based on application use case as it effects throughput of the message processing rate. To avoid blocking a commit, commitAsync can be used.

 
try {
 
 // 2. start event loop
 
 while (running) {
 
  // 3. poll for messages
 
  ConsumerRecords < String, String > records = consumer.poll(1000);
 
  // 4. iterate each message
 
  for (ConsumerRecord < String, String > record: records)
 
  // process message
 
  processMessage(record);
 
  Map < TopicPartition, OffsetAndMetadata > offsets = prepareCommitOffsetFor(record);
 
  consumer.commitAsync(Map < TopicPartition, OffsetAndMetadata > offsets, new OffsetCommitCallback() {
 
   @Override
 
   public void onComplete(Map < TopicPartition, OffsetAndMetadata > offsets,
 
    Exception exception) {
 
    if (exception != null) {
 
     // application specific failure handling
 
    }
 
   }
 
  });
 
 }
 
} finally {
 
 consumer.close();
 
}
 

Note that, if the commit of any message fails it will lead to one of the following:

  • duplicate consumption - if the consumer crashes before the next successful commit and the new consumer starts processing from the last committed offset.
  • no duplication - if the consumer successfully commits subsequent messages and crashes.

So, this approach provides more throughput than commitSync.

Consume Exactly Once

As discussed above, in any case there is te possibility of reading a message more than once. Thus it is not possible to Consume Exactly Once with only Kafka APIs. But, it is certainly possible to achieve 'process exactly once,' though the message will be consumed more than once. This is demosntrated in the below code:

 
try {
 
 // 2. start event loop
 
 while (running) {
 
  // 3. poll for messages
 
  ConsumerRecords < String, String > records = consumer.poll(1000);
 
  // 4. iterate each message
 
  for (ConsumerRecord < String, String > record: records)
 
  // if message is already processed, skip processing
 
  if (isMessageProcessedAlready(record.offset(), record.partition(), record.topic)) {
 
    commitOffsetForRecord(record);
 
    continue;
 
  }
 
  // process message
 
  processMessage(record);
 
  // now persist offset, partition and topic of the message i.e.
 
  // processd just now
 
  persistProcessedMessageDetails(record.offset(), record.partition(), record.topic);
 
  commitOffsetForRecord(record);
 
 }
 
} finally {
 
 consumer.close();
 
}
 
 
// commit logic
 
private void commitOffsetForRecord(ConsumerRecord record) {
 
  Map < TopicPartition, OffsetAndMetadata > offsets = prepareCommitOffsetFor(record);
 
  consumer.commitAsync(Map < TopicPartition, OffsetAndMetadata > offsets, new OffsetCommitCallback() {
 
   @Override
 
   public void onComplete(Map < TopicPartition, OffsetAndMetadata > offsets,
 
    Exception exception) {
 
    if (exception != null) {
 
     // application specific failure handling
 
    }
 
   }
 
  });
 
}
 

Note that the above code eliminates duplicate processing as:

  • Processed message details are persisted (line 17).
  • Message is already processed (line 9).
    • Message offset is commited as an old consumer would have failed to commit the message after successfully processing it, so it has reconsumed/commited it (line 10).
    • Message processing is skipped (line 11).

Consumer Liveliness

Let's discuss how a group coordinator coordinates a consumer group.

Each consumer in a group is assigned to a subset of the partitions from topics it has subscribed to. This is basically a group lock on the partitions. As long as the lock is held, no other consumer in the group can read messages from the partitions. This is the way to avoid duplicate consumption when a consumer assigned to a partition is alive and holding the lock. But if the consumer dies/crashes, the lock needs to be released so that other live consumers can be assigned the partitions. The Kafka group coordination protocol accomplishes this using a heartbeat mechanism.

All live consumer group members send periodic heartbeat signals to the group coordinator. As long as the coordinator receives heartbeats, it assumes that members are live. On every received heartbeat, the coordinator starts (or resets) a timer. If no heartbeat is received when the timer expires, the coordinator marks the consumer dead and signals other consumers in the group that they should rejoin so that partitions can be reassigned. The duration of the timer can be configured using session.timeout.ms.

What if the consumer is still sending heartbeats to the coordinator but the application is not healthy such that it cannot process message it has consumed. Kafka solves the problem with a poll loop design. All network IO is done in the foreground when you call  poll or one of the other blocking APIs. The consumer does not use background threads so heartbeats are only sent to the coordinator when the consumer calls poll. If the application stops polling (whether that's because the processing code has thrown an exception or not), then no heartbeats will be sent, the session timeout will expire, and the group will be rebalanced. The only problem with this is that a spurious rebalance might be performed if the consumer takes longer than the session timeout to process messages (such as the processMessage method in the above code samples). So, the session timeout should be large enough to mitigate this. The default session timeout is 30 seconds, but it’s not unreasonable to set it as high as several minutes. The only problem of a larger session timeout is that the coordinator takes longer to detect consumer crashes.

Kafka FAQ Kafka Internals - FAQ

Kafka Internals: Consumers的更多相关文章

  1. Kafka Eagle Consumers不显示

    原因: kafka.eagle.offset.storage配置有误 该配置的作用:# kafka offset storage -- Offset stored in a Kafka cluster ...

  2. Windbg调优Kafka.Client内存泄露

    从来没写过Blog,想想也是,工作十多年了,搞过N多的架构.技术,不与大家分享实在是可惜了.另外,从传统地ERP行业转到互联网,也遇到了很所前所未有的问题,原来知道有一些坑,但是不知道坑太多太深.借着 ...

  3. Flink写入kafka时,只写入kafka的部分Partitioner,无法写所有的Partitioner问题

    1. 写在前面 在利用flink实时计算的时候,往往会从kafka读取数据写入数据到kafka,但会发现当kafka多个Partitioner时,特别在P量级数据为了kafka的性能kafka的节点有 ...

  4. Flink解析kafka canal未压平数据为message报错

    canal使用非flatmessage方式获取mysql bin log日志发至kafka比直接发送json效率要高很多,数据发到kafka后需要实时解析为json,这里可以使用strom或者flin ...

  5. 【Kafka专栏】-Kafka从初始到搭建到应用

    一.前述 Kafka是一个分布式的消息队列系统(Message Queue). kafka集群有多个Broker服务器组成,每个类型的消息被定义为topic. 同一topic内部的消息按照一定的key ...

  6. 超详细“零”基础kafka入门篇

    1.认识kafka 1.1 kafka简介 Kafka 是一个分布式流媒体平台 kafka官网:http://kafka.apache.org/ (1)流媒体平台有三个关键功能: 发布和订阅记录流,类 ...

  7. Scalability of Kafka Messaging using Consumer Groups

    May 10, 2018 By Suhita Goswami No Comments Categories: Data Ingestion Flume Kafka Use Case Tradition ...

  8. Kafka 温故(二):Kafka的基本概念和结构

    一.Kafka中的核心概念 Producer: 特指消息的生产者Consumer :特指消息的消费者Consumer Group :消费者组,可以并行消费Topic中partition的消息Broke ...

  9. Kafka学习入门

    最近工作中用到了两个很给力的项目,一个是Kafka,一个是Strom.本着自我学习并方便他人的目的,我会将我觉得比较有用的英文文档翻译在此(保留系统专有名词不作翻译). 1kafka介绍 在流式计算中 ...

随机推荐

  1. (原)pytorch中使用TensorRT

    转载请注明出处: https://www.cnblogs.com/darkknightzh/p/11332155.html 代码网址: https://github.com/darkknightzh/ ...

  2. 《linux就该这么学》课堂笔记18 squid服务

    Squid服务程序正向解析和反向解析 正向代理模式不仅可以让用户使用Squid代理服务器上网,还可以基于指定的IP地址.域名关键词.网站地址或下载文件后缀等信息,实现类似于访问控制列表的功能.反向代理 ...

  3. 最常见Linux操作

    命令 含义 cd /home/hadoop #把/home/hadoop设置为当前目录 cd .. #返回上一级目录 cd ~ #进入到当前Linux系统登录用户的主目录(或主文件夹).在 Linux ...

  4. U-Boot的常用命令详解

    U-Boot还提供了更加详细的命令帮助,通过help命令还可以查看每个命令的参数说明.由于开发过程的需要,有必要先把U-Boot命令的用法弄清楚.接下来,根据每一条命令的帮助信息,解释一下这些命令的功 ...

  5. 11-赵志勇机器学习-DBSCAN聚类

    (草稿) 两点关系的三种定义: 1. 直接密度可达:A在B的邻域内: 2. 密度可达:AB之间存在,直接密度可达的点串: 3. 密度连接:AB之间存在点k,使得Ak和Bk都密度可达: 过程: 1. 对 ...

  6. NOIP 2008 火柴棒等式

    洛谷 P1149 火柴棒等式 洛谷传送门 JDOJ 1540: [NOIP2008]火柴棒等式 T2 JDOJ传送门 Description 给你n根火柴棍,你可以拼出多少个形如"A+B=C ...

  7. Codeforces Beta Round #19

    A. World Football Cup #include <bits/stdc++.h> using namespace std;   ; char name[N][N]; map&l ...

  8. Win10解决修改host没有权限问题(其他文件同理) 一步都不能少哦:先添加再授权

    Step1:右键文件选择属性,选择安全,点击编辑: Step2:在弹窗中点击添加,在弹窗中点击高级: Step3:在弹窗中点击立即查找,选中当前用户,点击确定: Step4:此时选中用户已经被加入进来 ...

  9. 阿里云centos 7上面安装mysql5.7的详细步骤!!!

    前言: 网上太多的linux 的安装mysql教程,很多教程不全或者因为环境不一致导致无法成功安装,以下是亲测的可行性的方法,请参考. 步骤: Centos7操作系统YUM库列表里默认不再提供MySQ ...

  10. bzoj3589 动态树 求链并 容斥

    bzoj3589 动态树 链接 bzoj 思路 求链并. 发现只有最多5条链子,可以容斥. 链交求法:链顶是两条链顶深度大的那个,链底是两个链底的\(lca\) 如果链底深度小于链顶,就说明两条链没有 ...