Kafka 设计之消息传递保障

目录

一. 前言

二. Kafka 消息传递保障设计


一. 前言

    消息传递保障对于分布式系统的可靠性至关重要。在分布式系统中消息传递保障是确保系统可靠性的核心问题之一。系统需要确保消息能够按照预期的方式进行传递,以满足业务需求。

    Kafka 是一种分布式的消息队列系统,作为消息中间件常用于实现基于发布/订阅模型的消息传递服务。因此在 Kafka 中需要提供消息传递保障。

二. Kafka 消息传递保障设计

原文引用:Now that we understand a little about how producers and consumers work, let's discuss the semantic guarantees Kafka provides between producer and consumer. Clearly there are multiple possible message delivery guarantees that could be provided:

  • At most once—Messages may be lost but are never redelivered.
  • At least once—Messages are never lost but may be redelivered.
  • Exactly once—this is what people actually want, each message is delivered once and only once.

    现在我们了解一些关于生产者和消费者是如何工作的,接下来我们来讨论 Kafka 提供了生产者和消费者之间的担保语义。有多种可能的消息传递保证可以提供:

  • 最多一次 ---- 消息可能丢失,但绝不会重发。
  • 至少一次 ---- 消息绝不会丢失,但有可能重新发送。
  • 正好一次 ---- 这是人们真正想要的,每个消息传递一次且仅一次。

原文引用:It's worth noting that this breaks down into two problems: the durability guarantees for publishing a message and the guarantees when consuming a message. 

    值得注意的是,这分为两个问题:发送消息时的持久性保证和消费消息的保障。

原文引用:Many systems claim to provide "exactly once" delivery semantics, but it is important to read the fine print, most of these claims are misleading (i.e. they don't translate to the case where consumers or producers can fail, or cases where there are multiple consumer processes, or cases where data written to disk can be lost). 

    很多消息系统声称提供“正好一次”的传递语义,但阅读细节很重要,这些说法大多具有误导性(例如,它们没有解释消费者或生产者可能失败的情况,有多个消费者进程的情况,或写入磁盘的数据可能丢失的情况)。

原文引用:Kafka's semantics are straight-forward. When publishing a message we have a notion of the message being "committed" to the log. Once a published message is committed it will not be lost as long as one broker that replicates the partition to which this message was written remains "alive". The definition of alive as well as a description of which types of failures we attempt to handle will be described in more detail in the next section. For now let's assume a perfect, lossless broker and try to understand the guarantees to the producer and consumer. If a producer attempts to publish a message and experiences a network error it cannot be sure if this error happened before or after the message was committed. This is similar to the semantics of inserting into a database table with an autogenerated key. 

    Kafka 的语义是很直接的,我们有一个概念,当发布一条消息时,该消息 “committed(提交)” 到了日志,一旦提交了已发布的消息,只要副本分区写入了此消息的一个 Broker 仍然"活着”,它就不会丢失。“活着”的定义以及以及我们试图处理的故障类型的描述将在下一节中进行更详细的描述。现在让我们假设一个完美的不会丢消息的 Broker,并去了解如何保障生产者和消费者的。如果一个生产者发布消息并且正好遇到网络错误,就不能确定已提交的消息是否是在这个错误发生之前或之后。这类似于用自动生成的键插入到一个数据库表。

原文引用:Prior to 0.11.0.0, if a producer failed to receive a response indicating that a message was committed, it had little choice but to resend the message. This provides at-least-once delivery semantics since the message may be written to the log again during resending if the original request had in fact succeeded. Since 0.11.0.0, the Kafka producer also supports an idempotent delivery option which guarantees that resending will not result in duplicate entries in the log. To achieve this, the broker assigns each producer an ID and deduplicates messages using a sequence number that is sent by the producer along with every message. Also beginning with 0.11.0.0, the producer supports the ability to send messages to multiple topic partitions using transaction-like semantics: i.e. either all messages are successfully written or none of them are. The main use case for this is exactly-once processing between Kafka topics (described below).

    在0.11.0.0之前,如果一个生产者没有收到消息提交的响应,那么只能重新发送消息。 这提供了至少一次传递保障,因为如果原始请求实际上已成功,则消息可以在重新发送期间再次写入到日志中。自0.11.0.0起,Kafka 生产者支持幂等传递选项,保证重新发送不会导致日志中数据重复。 Broker 为每个生产者分配一个 ID,并使用生产者与每条消息一起发送的序列号为每条消息进行去重。从0.11.0.0开始,生产者支持使用类似事务的语义将消息发送到多个 Topic 分区的能力:即要么所有消息都成功写入,要么都没有成功写入。这个主要用于 Kafka Topic 之间“正好一次”的处理(如下所述)。

原文引用:Not all use cases require such strong guarantees. For uses which are latency sensitive we allow the producer to specify the durability level it desires. If the producer specifies that it wants to wait on the message being committed this can take on the order of 10 ms. However the producer can also specify that it wants to perform the send completely asynchronously or that it wants to wait only until the leader (but not necessarily the followers) have the message.

    并不是所有的情况都需要这么强力的保障。对于延迟敏感的,我们允许生产者指定它想要的耐用性水平。如生产者可以指定它获取需等待10毫秒量级上的响应。生产者也可以指定异步发送,或只等待 Leader(不需要副本的响应)响应。

原文引用:Now let's describe the semantics from the point-of-view of the consumer. All replicas have the exact same log with the same offsets. The consumer controls its position in this log. If the consumer never crashed it could just store this position in memory, but if the consumer fails and we want this topic partition to be taken over by another process the new process will need to choose an appropriate position from which to start processing. Let's say the consumer reads some messages -- it has several options for processing the messages and updating its position.

  1. It can read the messages, then save its position in the log, and finally process the messages. In this case there is a possibility that the consumer process crashes after saving its position but before saving the output of its message processing. In this case the process that took over processing would start at the saved position even though a few messages prior to that position had not been processed. This corresponds to "at-most-once" semantics as in the case of a consumer failure messages may not be processed.
  2. It can read the messages, process the messages, and finally save its position. In this case there is a possibility that the consumer process crashes after processing messages but before saving its position. In this case when the new process takes over the first few messages it receives will already have been processed. This corresponds to the "at-least-once" semantics in the case of consumer failure. In many cases messages have a primary key and so the updates are idempotent (receiving the same message twice just overwrites a record with another copy of itself).

    现在让我们从消费者的角度描述语义。所有的副本都有相同的日志、相同的偏移量。消费者控制offset 在日志中的位置。如果消费者永不宕机,它可以将此位置存储在内存中,但是如果消费者故障,我们希望这个 Topic 分区被另一个进程接管,新进程需要选择一个合适的位置开始处理。我们假设消费者读取了一些消息,有几种选项用于处理消息和更新它的位置。

  1. 读取消息,然后在日志中保存它的位置,最后处理消息。在这种情况下,有可能消费者保存了位置之后,但是在处理消息输出之前崩溃了。在这种情况下,接管处理的进程会在已保存的位置开始,即使该位置之前有几个消息尚未处理。这对应于“最多一次” ,在消费者处理失败消息的情况下,不进行处理。
  2. 读取消息,处理消息,最后保存消息的位置。在这种情况下,可能消费进程处理消息之后,但在保存它的位置之前崩溃了。在这种情况下,当新的进程接管了它,这将接收已经被处理的前几个消息。这就符合了“至少一次”的语义。在许多情况下,消息有一个主键,因此更新是幂等的(其任意多次执行所产生的影响均与一次执行的影响相同)。

原文引用:So what about exactly once semantics (i.e. the thing you actually want)? When consuming from a Kafka topic and producing to another topic (as in a Kafka Streams application), we can leverage the new transactional producer capabilities in 0.11.0.0 that were mentioned above. The consumer's position is stored as a message in a topic, so we can write the offset to Kafka in the same transaction as the output topics receiving the processed data. If the transaction is aborted, the consumer's position will revert to its old value and the produced data on the output topics will not be visible to other consumers, depending on their "isolation level." In the default "read_uncommitted" isolation level, all messages are visible to consumers even if they were part of an aborted transaction, but in "read_committed," the consumer will only return messages from transactions which were committed (and any messages which were not part of a transaction).

    那么,什么是“正好一次”语义(也就是你真正想要的东西)呢? 当从 Kafka 主题消费并生产到另一个 Topic 时(例如 Kafka Stream),我们可以利用之前提到0.11.0.0中的生产者新事务功能。消费者的位置作为消息存储到 Topic 中,因此我们可以与接收处理后的数据的输出 Topic 使用相同的事务写入 offset 到 Kafka。如果事务中断,则消费者的位置将恢复到老的值,根据其“隔离级别”,其他消费者将不会看到输出 Topic 的生成数据,在默认的“read_uncommitted”隔离级别中,所有消息对消费者都是可见的,即使是被中断的事务的消息。但是在“read_committed”中,消费者将只返回已提交事务的消息。

原文引用:When writing to an external system, the limitation is in the need to coordinate the consumer's position with what is actually stored as output. The classic way of achieving this would be to introduce a two-phase commit between the storage of the consumer position and the storage of the consumers output. But this can be handled more simply and generally by letting the consumer store its offset in the same place as its output. This is better because many of the output systems a consumer might want to write to will not support a two-phase commit. As an example of this, consider a Kafka Connect connector which populates data in HDFS along with the offsets of the data it reads so that it is guaranteed that either data and offsets are both updated or neither is. We follow similar patterns for many other data systems which require these stronger semantics and for which the messages do not have a primary key to allow for deduplication.

    当写入到外部系统时,需要将消费者的位置与实际存储为输出的位置进行协调。实现这一目标的典型方法是在消费者位置的存储和消费者输出的存储之间引入两阶段提交。但是,这可以通过让消费者将其 offset 存储在与其输出相同的位置来更简单、更普遍地处理。这样更好,因为大多数的输出系统不支持两阶段提交。举个例子,考虑一个 Kafka Connect 连接器,它在 HDFS 中填充数据以及它读取的数据的 offset,这样就可以保证数据和 offset 都被更新或者都不更新。我们对许多其他数据系统采用了类似的模式,这些系统需要这些更强的语义,并且消息没有允许重复数据消除的主键。

原文引用:So effectively Kafka guarantees at-least-once delivery by default and allows the user to implement at most once delivery by disabling retries on the producer and committing its offset prior to processing a batch of messages. Exactly-once delivery requires co-operation with the destination storage system but Kafka provides the offset which makes implementing this straight-forward.

    Kafka 默认情况下有效地保证了“至少一次”传递,并允许用户通过禁止生产者重试和处理一批消息前提交它的偏移量来实现“最多一次”传递。而“正好一次”传递需要与目标存储系统合作,但 Kafka 提供了偏移量,所以实现这个很简单。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/728786.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

智能部署之巅:Amazon SageMaker 引领机器学习革新

本篇文章授权活动官方亚马逊云科技文章转发、改写权,包括不限于在 亚马逊云科技开发者社区, 知乎,自媒体平台,第三方开发者媒体等亚马逊云科技官方渠道。 (全球 TMT 2023年12月6日讯)亚马逊云科技在 2023 re:Invent 全…

2024 GoLand激活,分享几个GoLand激活的方案

文章目录 GoLand公司简介我这边使用GoLand的理由GoLand 最新变化GoLand 2023.3 最新变化AI Assistant 正式版GoLand 中的 AI Assistant:_Rename_(重命名)GoLand 中的 AI Assistant:_Write documentation_(编写文档&…

Linux编程3.5 进程-进程创建

1、相关函数 #include<unistd.h> #include<sys/types.h> pid_t fork(void); 返回&#xff1a;子进程中为0&#xff0c;父进程中为子进程ID&#xff0c;出错为-1pid_t vfork(void); 返回&#xff1a;子进程中为0&#xff0c;父进程中为子进程ID&#xff0c;出…

HBase介绍、特点、应用场景、生态圈

目录: 一、HBase简介 二、NoSQL和关系型数据库对比 三、HBase特点 四、应用场景 五、HBase生态圈技术 一、HBase简介 HBase是一个领先的NoSQL数据库 是一个面向列存储的NoSQL数据库 是一个分布式Hash Map&#xff0c;底层数据是Key-Value格式 基于Coogle Big Table论文 使用HD…

Unity零基础到进阶 | Unity中 屏蔽指定UI点击事件 的多种方法整理

Unity零基础到进阶 | Unity中 屏蔽指定UI点击事件 的多种方法整理一、Unity中 屏蔽透明区域的点击事件1.1 使用Image组件自带的参数检测1.2 根据点击的坐标计算该点的像素值是否满足阈值 二、Unity中屏蔽 不规则图片按钮点击的事件 总结 &#x1f3ac; 博客主页&#xff1a;htt…

遗传算法优化BP神经网络时间序列回归分析,ga-bp回归分析

目录 BP神经网络的原理 BP神经网络的定义 BP神经网络的基本结构 BP神经网络的神经元 BP神经网络的激活函数, BP神经网络的传递函数 遗传算法原理 遗传算法主要参数 遗传算法流程图 完整代码包含数据下载链接: 遗传算法优化BP神经网络时间序列回归分析,ga-bp回归分析(代码完…

Qt 类的前置声明和头文件包含

1. 在头文件中引入另一个类经常有两种写法 1&#xff09;前置声明 2&#xff09;头文件包含 #ifndef FRMCOUPLE2_H #define FRMCOUPLE2_H#include <QWidget> //头文件包含namespace Ui { class frmcouple2; }//前置声明&#xff1a;QPushButton frmchkeyboard…

moi3D安装

下载文件双击文件 下一步 同意下一步 下一步 下一步 下一步 安装下一步 完成 破解 将如图中的文件复制到文件目录下 汉化 在目录中进入ui文件夹下 在安装包中找到如下的文件复制到ui目录下 在打开 另存为 另存为时改一下编码格式如图 打开软件 找到如图options进入…

图像分类技术在城市垃圾分类与处理中的应用与实践

一、引言 在当今世界&#xff0c;城市化进程不断加快&#xff0c;随之而来的是日益增长的垃圾处理压力。城市生活垃圾、工业固体废物和危险废物的处理已经成为环境保护领域的一大挑战。为了应对这一挑战&#xff0c;卫生填埋、垃圾堆肥和垃圾焚烧等技术路线应运而生。其中&…

Cloud-Eureka服务治理-Ribbon负载均衡

构建Cloud父工程 父工程只做依赖版本管理 不引入依赖 pom.xml <packaging>pom</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.9.RELEA…

微服务架构下的接口调用链路梳理方案(AI问答)

接口调用链路梳理 我有很多的接口&#xff0c;不知道有哪些微服务在调用它&#xff0c;我应该怎么把调用链路梳理出来&#xff1f; 在微服务架构中&#xff0c;梳理接口调用链路是一项关键的运维和调试任务。为了找出哪些微服务正在调用特定的接口&#xff0c;可以采用以下几种…

【系统学习】2-Java进阶知识总结-3-集合-1-补充【泛型、树、数据结构】

文章目录 泛型什么是泛型&#xff1f;常见的泛型标识符泛型类泛型方法泛型接口通配符 树树的基本概念什么是二叉树&#xff1f;二叉树--普通二叉树二叉树--二叉查找树定义规则优缺点 二叉树--平衡二叉树定义规则旋转机制 二叉树--红黑树定义规则红黑规则 常见数据结构总体特点结…

【自然语言处理】NLP入门(五):1、正则表达式与Python中的实现(5):字符串常用方法:对齐方式、大小写转换详解

文章目录 一、前言二、正则表达式与Python中的实现1.字符串构造2. 字符串截取3. 字符串格式化输出4.字符转义符5. 字符串常用函数函数与方法之比较 6. 字符串常用方法1. 对齐方式center()ljust()rjust() 2. 大小写转换lower()upper()capitalize()title()swapcase() 一、前言 本…

RabbitMQ实战:docker compose 搭建RabbitMQ

目录 一、yml文件准备二、启动RabbitMQ三、开启图形化管理界面四、验证参考资料 一、yml文件准备 docker-compose-rabbitmq.yml文件如下所示 version: "3.8" services:rabbitmq:image: rabbitmq:3.11-alpine container_name: rabbitmqrestart: alwaysvolumes:- /ho…

软考高级:UML 4+1 视图概念和例题

作者&#xff1a;明明如月学长&#xff0c; CSDN 博客专家&#xff0c;大厂高级 Java 工程师&#xff0c;《性能优化方法论》作者、《解锁大厂思维&#xff1a;剖析《阿里巴巴Java开发手册》》、《再学经典&#xff1a;《Effective Java》独家解析》专栏作者。 热门文章推荐&am…

Java高频面试之总纲篇

Java高频 面试之基础篇 Java高频面试之集合篇 Java高频面试之异常篇 Java高频面试之并发篇Java高频面试之SSM篇 Java高频面试之Mysql篇 Java高频面试之Redis篇 Java高频面试之消息队列与分布式篇 50道SQL面试题 奇奇怪怪的面试题 五花八门的内存溢出

Golang内存管理

# golang内存管理golang是一种编译型的静态类型语言&#xff0c;它提供了一种简洁和高效的方式来管理内存。golang的内存管理主要依赖于两个机制&#xff1a;**栈**和**堆**。## 栈栈是一种后进先出&#xff08;LIFO&#xff09;的数据结构&#xff0c;它用于存储函数的局部变量…

在Jetson Xavier NX 开发板上使用VScode执行ROS程序详细过程

1.创建 ROS 工作空间ws 在home下打开终端输入下面指令 mkdir -p xxx_ws/src(必须得有 src) cd 自己命名_ws catkin_make2.启动 vscode cd 自己命名_ws code .3.vscode 中编译 ros 快捷键 ctrl shift B 调用编译&#xff0c;在上方弹窗位置选择:catkin_make:build 可以点击…

jenkins配置

jenkins前端常用插件&#xff1a; Git Parameter 、Maven Integration 、Proxmox&#xff08;snapshot&#xff09;、Pipeline: Multibranch&#xff08;多分支流水线&#xff09;、Pipeline: Stage Step、pipeline Git plugin、NodeJS Plugin、Publish Over SSH、SSH server…

python界面开发 - Menu (popupmenu) 右键菜单

文章目录 1. python图形界面开发1.1. Python图形界面开发——Tkinter1.2. Python图形界面开发——PyQt1.3. Python图形界面开发——wxPython1.4. Python图形界面开发—— PyGTK&#xff1a;基于GTK1.5. Python图形界面开发—— Kivy1.6. Python图形界面开发——可视化工具1.7. …