Kafka 生产者保证

Kafka Producer guarantees

我正在使用 Kafka Producer,我的应用程序将所有具有相同密钥的各个 ProducerRecords 发送到一个分区,然后这些 ProducerRecords 在之前进行批处理(使用 batch.size 和 linger.ms 参数)被发送给经纪人。我有 enable.idempotence=true 和 acks=all.

如果一个batch中间有一条记录写入失败,比如主机挂掉,网络故障,磁盘故障,或者这条记录没有被最小replicas确认,Kafka是否保证所有以后的记录还会不会写?或者是否有可能丢失批次中间的记录?

由于您的所有记录都将进入同一个分区,因此您可以安全地假设所有以前的记录也将在那里。

Kafka 保证给定分区中的顺序,因此如果您将消息 m1 和 m2(按顺序)发送到分区,批处理和延迟逻辑将不会覆盖顺序.换句话说,如果您在消费者处看到消息 m2,您可以放心地假设 m1 也已安全传递。

If one record in the middle of a batch fails to be written, for example if a host crashes or a network failure or disk failure occurs or the record failed to be acked by the minimum replicas, does Kafka guarantee that all subsequent records will also not be written?

是的,如果一个批次中的任何 条消息失败,则同一批次中的所有消息都失败。因此,批处理中的 none 消息将写入代理的磁盘。

Or is there a possibility that a record in the middle of a batch could be missing?

否,该批处理的所有或 none 条消息都已写入代理。

这是通过生产者客户端线程与本地缓冲区之间的分离实现的,本地缓冲区在将数据物理发送到代理之前对数据进行排队和批处理。