欢迎光临
我们一直在努力

Kafka消息不丢的底层保障:ACK机制、ISR同步与Leader Epoch详解

一、Kafka消息可靠性概述

1.1 消息丢失的主要原因分析

Kafka作为分布式消息系统,虽然设计了多重保障机制来防止消息丢失,但在特定场景下仍可能发生消息丢失问题。主要原因包括:

  • 生产端问题:生产者配置不当,未正确设置ACK机制,导致消息发送后未确认即认为发送成功。
  • 网络分区:网络故障导致Broker集群中的节点间通信中断,造成数据不一致。
  • Broker故障:Leader副本故障,如果数据未完全同步到Follower副本,选举新的Leader后可能导致部分消息丢失。
  • 存储故障:磁盘故障或存储空间耗尽导致消息未能持久化到磁盘。
  • 消费端问题:消费偏移量提交不当,导致消费失败后重复消费或消息丢失。
  • 了解这些原因有助于我们针对性地配置Kafka的可靠性机制,构建更加健壮的消息系统。

    1.2 Kafka消息可靠性保障机制概述

    Kafka通过多层次的机制来保障消息可靠性,主要包括:

  • ACK确认机制:通过生产者、Broker和消费者之间的确认机制确保消息被正确处理。
  • ISR副本同步机制:维护一个与Leader保持同步的副本集合,确保数据一致性。
  • Leader Epoch机制:避免旧的Leader继续处理请求,解决数据不一致问题。
  • 消息持久化:将消息持久化到磁盘,确保即使节点重启也不会丢失数据。
  • 消费偏移量管理:通过消费者组的偏移量管理机制确保消息被正确消费。
  • 这些机制相互配合,共同构建了Kafka的消息可靠性保障体系。

    1.3 消息可靠性评估指标

    评估Kafka消息可靠性的关键指标包括:

  • 消息丢失率:系统中丢失的消息占总消息的比例。
  • 数据一致性程度:不同副本间数据的同步程度。
  • 故障恢复时间(FTR):从故障发生到系统恢复正常的时间。
  • 生产者确认延迟:生产者发送消息到收到确认的时间间隔。
  • 消息持久化时间:消息从发送到持久化到磁盘的时间间隔。
  • 这些指标帮助我们量化Kafka系统的可靠性水平,为系统优化提供依据。

    1.4 可靠性保障机制的协同工作

    上述可靠性保障机制并非独立工作,而是相互协作形成完整的保障体系。下面展示这些机制如何协同工作:

    #publish-mermaid-1788278821848-0{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-1788278821848-0 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278821848-0 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278821848-0 .error-icon{fill:#552222;}#publish-mermaid-1788278821848-0 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1788278821848-0 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1788278821848-0 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1788278821848-0 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1788278821848-0 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1788278821848-0 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1788278821848-0 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1788278821848-0 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1788278821848-0 .marker.cross{stroke:#333333;}#publish-mermaid-1788278821848-0 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1788278821848-0 p{margin:0;}#publish-mermaid-1788278821848-0 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1788278821848-0 .cluster-label text{fill:#333;}#publish-mermaid-1788278821848-0 .cluster-label span{color:#333;}#publish-mermaid-1788278821848-0 .cluster-label span p{background-color:transparent;}#publish-mermaid-1788278821848-0 .label text,#publish-mermaid-1788278821848-0 span{fill:#333;color:#333;}#publish-mermaid-1788278821848-0 .node rect,#publish-mermaid-1788278821848-0 .node circle,#publish-mermaid-1788278821848-0 .node ellipse,#publish-mermaid-1788278821848-0 .node polygon,#publish-mermaid-1788278821848-0 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788278821848-0 .rough-node .label text,#publish-mermaid-1788278821848-0 .node .label text,#publish-mermaid-1788278821848-0 .image-shape .label,#publish-mermaid-1788278821848-0 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1788278821848-0 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1788278821848-0 .rough-node .label,#publish-mermaid-1788278821848-0 .node .label,#publish-mermaid-1788278821848-0 .image-shape .label,#publish-mermaid-1788278821848-0 .icon-shape .label{text-align:center;}#publish-mermaid-1788278821848-0 .node.clickable{cursor:pointer;}#publish-mermaid-1788278821848-0 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1788278821848-0 .arrowheadPath{fill:#333333;}#publish-mermaid-1788278821848-0 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1788278821848-0 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1788278821848-0 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1788278821848-0 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1788278821848-0 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1788278821848-0 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1788278821848-0 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1788278821848-0 .cluster text{fill:#333;}#publish-mermaid-1788278821848-0 .cluster span{color:#333;}#publish-mermaid-1788278821848-0 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#publish-mermaid-1788278821848-0 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1788278821848-0 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1788278821848-0 .icon-shape,#publish-mermaid-1788278821848-0 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1788278821848-0 .icon-shape p,#publish-mermaid-1788278821848-0 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1788278821848-0 .icon-shape .label rect,#publish-mermaid-1788278821848-0 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1788278821848-0 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1788278821848-0 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1788278821848-0 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1788278821848-0 [data-look=\”neo\”].node rect,#publish-mermaid-1788278821848-0 [data-look=\”neo\”].cluster rect,#publish-mermaid-1788278821848-0 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278821848-0 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1788278821848-0 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788278821848-0 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278821848-0 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1788278821848-0 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278821848-0 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1788278821848-0 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278821848-0 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278821848-0 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}acks=0acks=1acks=all

    生产者发送消息

    Broker收到消息

    写入Leader副本

    配置ACK级别

    不等待确认,立即返回

    Leader写入后确认

    ISR全部同步后确认

    发送确认给生产者

    可能存在丢失风险

    消息持久化到磁盘

    Consumer消费消息

    更新消费偏移量

    二、ACK机制详解

    2.1 生产者ACK机制原理

    Kafka的ACK(Acknowledgment)机制是保障消息可靠性的重要组成部分,它定义了生产者需要收到多少确认后才认为消息发送成功。Kafka提供了三种ACK级别配置:

  • acks=0:生产者不等待Broker的任何确认,消息发送即认为成功。这种方式吞吐量最高,但可靠性最低,消息可能丢失。
  • acks=1:生产者等待Leader副本成功写入消息后即收到确认。这种方式在正常情况下可靠性较高,但如果Leader副本在确认后立即崩溃,而Follower副本尚未同步该消息,则仍可能导致消息丢失。
  • acks=all(或acks=-1):生产者等待ISR(In-Sync Replicas)中所有副本都成功写入消息后才收到确认。这种方式可靠性最高,但吞吐量相对较低。
  • 下面展示生产者ACK机制的决策流程:

    ISR集合Follower副本Leader副本生产者ISR集合Follower副本Leader副本生产者#publish-mermaid-1788278821975-1{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-1788278821975-1 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278821975-1 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278821975-1 .error-icon{fill:#552222;}#publish-mermaid-1788278821975-1 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1788278821975-1 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1788278821975-1 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1788278821975-1 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1788278821975-1 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1788278821975-1 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1788278821975-1 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1788278821975-1 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1788278821975-1 .marker.cross{stroke:#333333;}#publish-mermaid-1788278821975-1 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1788278821975-1 p{margin:0;}#publish-mermaid-1788278821975-1 .actor{stroke:#9370DB;fill:#ECECFF;stroke-width:1;}#publish-mermaid-1788278821975-1 rect.actor.outer-path[data-look=\”neo\”]{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278821975-1 rect.note[data-look=\”neo\”]{stroke:#aaaa33;fill:#fff5ad;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278821975-1 text.actor>tspan{fill:black;stroke:none;}#publish-mermaid-1788278821975-1 .actor-line{stroke:#9370DB;}#publish-mermaid-1788278821975-1 .innerArc{stroke-width:1.5;stroke-dasharray:none;}#publish-mermaid-1788278821975-1 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#publish-mermaid-1788278821975-1 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#publish-mermaid-1788278821975-1 [id$=\”-arrowhead\”] path{fill:#333;stroke:#333;}#publish-mermaid-1788278821975-1 .sequenceNumber{fill:white;}#publish-mermaid-1788278821975-1 [id$=\”-sequencenumber\”]{fill:#333;}#publish-mermaid-1788278821975-1 [id$=\”-crosshead\”] path{fill:#333;stroke:#333;}#publish-mermaid-1788278821975-1 .messageText{fill:#333;stroke:none;}#publish-mermaid-1788278821975-1 .labelBox{stroke:#9370DB;fill:#ECECFF;filter:none;}#publish-mermaid-1788278821975-1 .labelText,#publish-mermaid-1788278821975-1 .labelText>tspan{fill:black;stroke:none;}#publish-mermaid-1788278821975-1 .loopText,#publish-mermaid-1788278821975-1 .loopText>tspan{fill:black;stroke:none;}#publish-mermaid-1788278821975-1 .sectionTitle,#publish-mermaid-1788278821975-1 .sectionTitle>tspan{fill:black;stroke:none;}#publish-mermaid-1788278821975-1 .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:#9370DB;fill:#9370DB;}#publish-mermaid-1788278821975-1 .note{stroke:#aaaa33;fill:#fff5ad;}#publish-mermaid-1788278821975-1 .noteText,#publish-mermaid-1788278821975-1 .noteText>tspan{fill:black;stroke:none;font-weight:normal;}#publish-mermaid-1788278821975-1 .activation0{fill:#f4f4f4;stroke:#666;}#publish-mermaid-1788278821975-1 .activation1{fill:#f4f4f4;stroke:#666;}#publish-mermaid-1788278821975-1 .activation2{fill:#f4f4f4;stroke:#666;}#publish-mermaid-1788278821975-1 .actorPopupMenu{position:absolute;}#publish-mermaid-1788278821975-1 .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#publish-mermaid-1788278821975-1 .actor-man circle,#publish-mermaid-1788278821975-1 line{fill:#ECECFF;stroke-width:2px;}#publish-mermaid-1788278821975-1 g rect.rect{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));stroke:#9370DB;}#publish-mermaid-1788278821975-1 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1788278821975-1 [data-look=\”neo\”].node rect,#publish-mermaid-1788278821975-1 [data-look=\”neo\”].cluster rect,#publish-mermaid-1788278821975-1 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278821975-1 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1788278821975-1 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788278821975-1 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278821975-1 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1788278821975-1 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278821975-1 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1788278821975-1 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278821975-1 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278821975-1 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}alt[acks=0][acks=1][acks=all]发送消息写入本地日志立即返回不等待确认返回确认同步到ISR所有副本确认同步完成返回确认

    2.2 Broker端ACK处理流程

    Broker端的ACK处理流程是Kafka可靠性保障的核心环节。下面详细解析Broker如何处理不同ACK级别的请求:

    acks=0的处理流程

  • 生产者发送消息到Broker。
  • Leader副本接收消息并将其写入内存缓冲区。
  • 不等待任何确认,立即向生产者返回成功响应。
  • 后台线程负责将内存中的消息异步写入磁盘。
  • 这种模式的优势是吞吐量高,但缺点是消息可能在写入磁盘前丢失,例如Broker突然崩溃。

    acks=1的处理流程

  • 生产者发送消息到Broker,设置acks=1。
  • Leader副本接收消息并将其写入本地日志(包括内存和磁盘)。
  • Leader副本向生产者发送确认响应。
  • 后台线程将消息同步到Follower副本。
  • 这种模式在大多数情况下能保证消息不丢失,但如果Leader在发送确认后、Follower同步前崩溃,则可能导致消息丢失。

    acks=all的处理流程

  • 生产者发送消息到Broker,设置acks=all。
  • Leader副本接收消息并将其写入本地日志(包括内存和磁盘)。
  • Leader副本将消息发送给ISR集合中的所有副本。
  • ISR中的所有副本确认收到消息。
  • Leader副本向生产者发送确认响应。
  • 这种模式能最大程度保证消息不丢失,但吞吐量相对较低。

    下面展示Broker端ACK处理的详细流程:

    #publish-mermaid-1788278822025-2{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-1788278822025-2 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278822025-2 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278822025-2 .error-icon{fill:#552222;}#publish-mermaid-1788278822025-2 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1788278822025-2 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1788278822025-2 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1788278822025-2 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1788278822025-2 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1788278822025-2 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1788278822025-2 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1788278822025-2 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1788278822025-2 .marker.cross{stroke:#333333;}#publish-mermaid-1788278822025-2 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1788278822025-2 p{margin:0;}#publish-mermaid-1788278822025-2 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1788278822025-2 .cluster-label text{fill:#333;}#publish-mermaid-1788278822025-2 .cluster-label span{color:#333;}#publish-mermaid-1788278822025-2 .cluster-label span p{background-color:transparent;}#publish-mermaid-1788278822025-2 .label text,#publish-mermaid-1788278822025-2 span{fill:#333;color:#333;}#publish-mermaid-1788278822025-2 .node rect,#publish-mermaid-1788278822025-2 .node circle,#publish-mermaid-1788278822025-2 .node ellipse,#publish-mermaid-1788278822025-2 .node polygon,#publish-mermaid-1788278822025-2 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788278822025-2 .rough-node .label text,#publish-mermaid-1788278822025-2 .node .label text,#publish-mermaid-1788278822025-2 .image-shape .label,#publish-mermaid-1788278822025-2 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1788278822025-2 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1788278822025-2 .rough-node .label,#publish-mermaid-1788278822025-2 .node .label,#publish-mermaid-1788278822025-2 .image-shape .label,#publish-mermaid-1788278822025-2 .icon-shape .label{text-align:center;}#publish-mermaid-1788278822025-2 .node.clickable{cursor:pointer;}#publish-mermaid-1788278822025-2 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1788278822025-2 .arrowheadPath{fill:#333333;}#publish-mermaid-1788278822025-2 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1788278822025-2 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1788278822025-2 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1788278822025-2 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822025-2 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822025-2 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1788278822025-2 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1788278822025-2 .cluster text{fill:#333;}#publish-mermaid-1788278822025-2 .cluster span{color:#333;}#publish-mermaid-1788278822025-2 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#publish-mermaid-1788278822025-2 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1788278822025-2 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1788278822025-2 .icon-shape,#publish-mermaid-1788278822025-2 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1788278822025-2 .icon-shape p,#publish-mermaid-1788278822025-2 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1788278822025-2 .icon-shape .label rect,#publish-mermaid-1788278822025-2 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822025-2 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1788278822025-2 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1788278822025-2 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1788278822025-2 [data-look=\”neo\”].node rect,#publish-mermaid-1788278822025-2 [data-look=\”neo\”].cluster rect,#publish-mermaid-1788278822025-2 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822025-2 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1788278822025-2 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788278822025-2 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822025-2 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1788278822025-2 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822025-2 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1788278822025-2 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822025-2 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822025-2 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}acks=0acks=1acks=all

    生产者发送消息到Broker

    Leader副本接收消息

    消息写入本地日志

    ACK级别设置

    立即返回成功

    Leader写入成功后返回确认

    等待ISR所有副本确认

    ISR副本同步消息

    ISR副本确认收到

    后台线程持久化到磁盘

    消息处理完成

    2.3 ACK级别配置与影响分析

    正确配置ACK级别对于平衡系统性能和可靠性至关重要。下面分析不同ACK级别的适用场景及其影响:

    acks=0的适用场景与影响

    适用场景:

    • 对消息丢失不敏感的业务场景
    • 高吞吐量优先的场景
    • 可以接受偶尔消息丢失的场景

    影响:

    • 优势:吞吐量最高,延迟最低,Broker负载小
    • 劣势:可靠性最低,可能出现消息丢失
    • 适用场景:日志收集、监控数据等允许少量丢失的场景

    acks=1的适用场景与影响

    适用场景:

    • 对可靠性有一定要求但非绝对关键的业务
    • 需要较高吞吐量的场景
    • 可以接受极小概率消息丢失的场景

    影响:

    • 优势:平衡了可靠性和性能
    • 劣势:仍有少量消息丢失风险
    • 适用场景:一般业务系统、非核心数据处理流程

    acks=all的适用场景与影响

    适用场景:

    • 对消息可靠性要求极高的业务
    • 关键业务数据交换场景
    • 数据完整性要求严格的应用

    影响:

    • 优势:可靠性最高,几乎不会丢失消息
    • 劣势:吞吐量较低,延迟较高,Broker负载大
    • 适用场景:金融交易、订单处理、关键业务数据等

    下面是一个对比表格,展示不同ACK级别的关键特性:

    | ACK级别 | 可靠性 | 吞吐量 | 延迟 | Broker负载 | 适用场景 |

    |———|——–|——–|——|————|———-|

    | acks=0 | 低 | 高 | 低 | 低 | 日志收集、监控数据 |

    | acks=1 | 中 | 中 | 中 | 中 | 一般业务系统 |

    | acks=all | 高 | 低 | 高 | 高 | 金融交易、订单处理 |

    三、ISR同步机制深入解析

    3.1 ISR集合定义与作用

    ISR(In-Sync Replicas)是Kafka中保障数据一致性的核心机制之一。ISR指的是与Leader副本保持同步的Follower副本集合,这些副本与Leader的落后程度在可接受范围内。

    ISR集合的定义

    ISR集合包含以下类型的副本:

  • Leader副本:当前负责处理读写请求的副本。
  • 与Leader保持同步的Follower副本:这些副本与Leader的差距在配置的阈值内。
  • ISR集合是动态维护的,通过以下标准判断Follower是否在ISR中:

    • Follower副本与Leader副本的滞后程度不超过配置的阈值(参数replica.lag.time.max.ms)。
    • Follower副本保持与Leader的通信正常。

    ISR集合的作用

    ISR集合的主要作用包括:

  • 数据一致性保障:确保ISR中的副本数据与Leader保持一致。
  • 故障恢复基础:当Leader故障时,从ISR中选举新的Leader,保证数据不丢失。
  • 高可用性支持:通过ISR的副本扩展,提高系统的可用性。
  • 下面展示ISR集合的构成及其作用:

    #publish-mermaid-1788278822131-3{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-1788278822131-3 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278822131-3 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278822131-3 .error-icon{fill:#552222;}#publish-mermaid-1788278822131-3 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1788278822131-3 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1788278822131-3 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1788278822131-3 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1788278822131-3 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1788278822131-3 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1788278822131-3 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1788278822131-3 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1788278822131-3 .marker.cross{stroke:#333333;}#publish-mermaid-1788278822131-3 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1788278822131-3 p{margin:0;}#publish-mermaid-1788278822131-3 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1788278822131-3 .cluster-label text{fill:#333;}#publish-mermaid-1788278822131-3 .cluster-label span{color:#333;}#publish-mermaid-1788278822131-3 .cluster-label span p{background-color:transparent;}#publish-mermaid-1788278822131-3 .label text,#publish-mermaid-1788278822131-3 span{fill:#333;color:#333;}#publish-mermaid-1788278822131-3 .node rect,#publish-mermaid-1788278822131-3 .node circle,#publish-mermaid-1788278822131-3 .node ellipse,#publish-mermaid-1788278822131-3 .node polygon,#publish-mermaid-1788278822131-3 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788278822131-3 .rough-node .label text,#publish-mermaid-1788278822131-3 .node .label text,#publish-mermaid-1788278822131-3 .image-shape .label,#publish-mermaid-1788278822131-3 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1788278822131-3 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1788278822131-3 .rough-node .label,#publish-mermaid-1788278822131-3 .node .label,#publish-mermaid-1788278822131-3 .image-shape .label,#publish-mermaid-1788278822131-3 .icon-shape .label{text-align:center;}#publish-mermaid-1788278822131-3 .node.clickable{cursor:pointer;}#publish-mermaid-1788278822131-3 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1788278822131-3 .arrowheadPath{fill:#333333;}#publish-mermaid-1788278822131-3 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1788278822131-3 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1788278822131-3 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1788278822131-3 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822131-3 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822131-3 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1788278822131-3 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1788278822131-3 .cluster text{fill:#333;}#publish-mermaid-1788278822131-3 .cluster span{color:#333;}#publish-mermaid-1788278822131-3 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#publish-mermaid-1788278822131-3 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1788278822131-3 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1788278822131-3 .icon-shape,#publish-mermaid-1788278822131-3 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1788278822131-3 .icon-shape p,#publish-mermaid-1788278822131-3 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1788278822131-3 .icon-shape .label rect,#publish-mermaid-1788278822131-3 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822131-3 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1788278822131-3 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1788278822131-3 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1788278822131-3 [data-look=\”neo\”].node rect,#publish-mermaid-1788278822131-3 [data-look=\”neo\”].cluster rect,#publish-mermaid-1788278822131-3 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822131-3 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1788278822131-3 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788278822131-3 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822131-3 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1788278822131-3 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822131-3 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1788278822131-3 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822131-3 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822131-3 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    Broker集群

    Topic分区

    Leader副本

    Follower副本1

    Follower副本2

    ISR集合

    选举新的Leader

    数据一致性保障

    高可用性支持

    3.2 ISR动态维护机制

    Kafka通过一系列机制动态维护ISR集合,确保ISR中的副本与Leader保持同步。下面详细解析ISR的动态维护机制。

    ISR维护的核心参数

    ISR维护主要依赖于以下配置参数:

  • replica.lag.time.max.ms:Follower副本与Leader同步的最大允许滞后时间,默认为30000毫秒(30秒)。如果Follower副本在指定时间内未与Leader同步,将被移出ISR。
  • min.insync.replicas:ISR中最小副本数量,默认为1。用于控制数据写入的安全级别。
  • ISR动态维护流程

    ISR的动态维护主要包括以下步骤:

  • Follower副本向Leader发起FetchRequest请求,拉取数据。
  • Leader副本记录Follower的拉取偏移量。
  • Leader监控每个Follower的拉取情况,如果某个Follower在replica.lag.time.max.ms时间内没有拉取新数据,则将其标记为可能需要移出ISR。
  • 定期检查各Follower的滞后情况,将滞后过多的Follower移出ISR。
  • 当Follower重新赶上Leader时,将其重新加入ISR。
  • 下面展示ISR动态维护的详细流程:

    #publish-mermaid-1788278822221-4{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-1788278822221-4 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278822221-4 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278822221-4 .error-icon{fill:#552222;}#publish-mermaid-1788278822221-4 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1788278822221-4 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1788278822221-4 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1788278822221-4 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1788278822221-4 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1788278822221-4 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1788278822221-4 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1788278822221-4 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1788278822221-4 .marker.cross{stroke:#333333;}#publish-mermaid-1788278822221-4 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1788278822221-4 p{margin:0;}#publish-mermaid-1788278822221-4 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1788278822221-4 .cluster-label text{fill:#333;}#publish-mermaid-1788278822221-4 .cluster-label span{color:#333;}#publish-mermaid-1788278822221-4 .cluster-label span p{background-color:transparent;}#publish-mermaid-1788278822221-4 .label text,#publish-mermaid-1788278822221-4 span{fill:#333;color:#333;}#publish-mermaid-1788278822221-4 .node rect,#publish-mermaid-1788278822221-4 .node circle,#publish-mermaid-1788278822221-4 .node ellipse,#publish-mermaid-1788278822221-4 .node polygon,#publish-mermaid-1788278822221-4 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788278822221-4 .rough-node .label text,#publish-mermaid-1788278822221-4 .node .label text,#publish-mermaid-1788278822221-4 .image-shape .label,#publish-mermaid-1788278822221-4 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1788278822221-4 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1788278822221-4 .rough-node .label,#publish-mermaid-1788278822221-4 .node .label,#publish-mermaid-1788278822221-4 .image-shape .label,#publish-mermaid-1788278822221-4 .icon-shape .label{text-align:center;}#publish-mermaid-1788278822221-4 .node.clickable{cursor:pointer;}#publish-mermaid-1788278822221-4 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1788278822221-4 .arrowheadPath{fill:#333333;}#publish-mermaid-1788278822221-4 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1788278822221-4 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1788278822221-4 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1788278822221-4 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822221-4 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822221-4 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1788278822221-4 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1788278822221-4 .cluster text{fill:#333;}#publish-mermaid-1788278822221-4 .cluster span{color:#333;}#publish-mermaid-1788278822221-4 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#publish-mermaid-1788278822221-4 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1788278822221-4 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1788278822221-4 .icon-shape,#publish-mermaid-1788278822221-4 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1788278822221-4 .icon-shape p,#publish-mermaid-1788278822221-4 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1788278822221-4 .icon-shape .label rect,#publish-mermaid-1788278822221-4 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822221-4 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1788278822221-4 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1788278822221-4 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1788278822221-4 [data-look=\”neo\”].node rect,#publish-mermaid-1788278822221-4 [data-look=\”neo\”].cluster rect,#publish-mermaid-1788278822221-4 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822221-4 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1788278822221-4 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788278822221-4 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822221-4 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1788278822221-4 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822221-4 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1788278822221-4 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822221-4 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822221-4 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}是否是否

    Leader副本

    记录Follower拉取偏移量

    监控Follower同步状态

    Follower是否滞后

    检查滞后时间

    是否超过阈值

    移出ISR

    Follower重新同步

    检查是否赶上Leader

    重新加入ISR

    ISR变化的传播机制

    当ISR集合发生变化时,Kafka通过以下机制确保集群中所有节点知晓最新的ISR状态:

  • Leader副本将最新的ISR信息写入ZooKeeper。
  • 其他Broker通过监听ZooKeeper中的ISR变化获取最新信息。
  • Controller节点负责协调ISR变更的传播。
  • 生产者和消费者通过元数据请求获取最新的ISR信息。
  • 这种机制确保了ISR信息在整个集群中的同步,避免了因ISR信息不一致导致的数据问题。

    3.3 ISR与数据持久化的关系

    ISR机制与Kafka的数据持久化密切相关,两者共同确保了消息的可靠存储和可恢复性。下面详细分析ISR与数据持久化的关系。

    ISR与数据持久化的协同作用

    ISR与数据持久化通过以下方式协同工作:

  • 消息先写入Leader副本的内存缓冲区,然后异步刷新到磁盘。
  • Leader副本将消息发送给ISR中的所有Follower副本。
  • ISR中的所有副本确认收到消息后,Leader才向生产者发送确认。
  • 即使某些Broker节点重启,ISR中的副本已经持久化了消息,保证了数据不丢失。
  • 数据持久化对ISR的影响

    数据持久化过程也会影响ISR的维护:

  • 当磁盘IO压力大时,Follower副本的拉取速度可能变慢,导致其滞后于Leader。
  • 消息批量写入策略会影响Follower的同步效率,进而影响ISR的稳定性。
  • 磁盘故障可能导致副本无法持久化消息,从而被移出ISR。
  • ISR与数据恢复的关系

    在数据恢复过程中,ISR起到关键作用:

  • 当Leader副本故障时,从ISR中选举新的Leader,保证已确认的消息不会丢失。
  • 新的Leader继续处理ISR中未同步的消息,确保数据一致性。
  • 如果ISR中的副本数量不足,会导致消息写入失败(取决于min.insync.replicas配置)。
  • 下面展示ISR与数据持久化的协同工作流程:

    #publish-mermaid-1788278822320-5{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-1788278822320-5 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278822320-5 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278822320-5 .error-icon{fill:#552222;}#publish-mermaid-1788278822320-5 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1788278822320-5 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1788278822320-5 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1788278822320-5 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1788278822320-5 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1788278822320-5 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1788278822320-5 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1788278822320-5 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1788278822320-5 .marker.cross{stroke:#333333;}#publish-mermaid-1788278822320-5 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1788278822320-5 p{margin:0;}#publish-mermaid-1788278822320-5 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1788278822320-5 .cluster-label text{fill:#333;}#publish-mermaid-1788278822320-5 .cluster-label span{color:#333;}#publish-mermaid-1788278822320-5 .cluster-label span p{background-color:transparent;}#publish-mermaid-1788278822320-5 .label text,#publish-mermaid-1788278822320-5 span{fill:#333;color:#333;}#publish-mermaid-1788278822320-5 .node rect,#publish-mermaid-1788278822320-5 .node circle,#publish-mermaid-1788278822320-5 .node ellipse,#publish-mermaid-1788278822320-5 .node polygon,#publish-mermaid-1788278822320-5 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788278822320-5 .rough-node .label text,#publish-mermaid-1788278822320-5 .node .label text,#publish-mermaid-1788278822320-5 .image-shape .label,#publish-mermaid-1788278822320-5 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1788278822320-5 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1788278822320-5 .rough-node .label,#publish-mermaid-1788278822320-5 .node .label,#publish-mermaid-1788278822320-5 .image-shape .label,#publish-mermaid-1788278822320-5 .icon-shape .label{text-align:center;}#publish-mermaid-1788278822320-5 .node.clickable{cursor:pointer;}#publish-mermaid-1788278822320-5 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1788278822320-5 .arrowheadPath{fill:#333333;}#publish-mermaid-1788278822320-5 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1788278822320-5 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1788278822320-5 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1788278822320-5 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822320-5 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822320-5 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1788278822320-5 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1788278822320-5 .cluster text{fill:#333;}#publish-mermaid-1788278822320-5 .cluster span{color:#333;}#publish-mermaid-1788278822320-5 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#publish-mermaid-1788278822320-5 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1788278822320-5 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1788278822320-5 .icon-shape,#publish-mermaid-1788278822320-5 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1788278822320-5 .icon-shape p,#publish-mermaid-1788278822320-5 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1788278822320-5 .icon-shape .label rect,#publish-mermaid-1788278822320-5 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822320-5 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1788278822320-5 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1788278822320-5 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1788278822320-5 [data-look=\”neo\”].node rect,#publish-mermaid-1788278822320-5 [data-look=\”neo\”].cluster rect,#publish-mermaid-1788278822320-5 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822320-5 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1788278822320-5 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788278822320-5 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822320-5 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1788278822320-5 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822320-5 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1788278822320-5 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822320-5 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822320-5 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}是否

    生产者发送消息

    Leader写入内存

    异步写入磁盘

    发送消息给ISR中的Follower

    Follower接收并持久化

    Follower确认收到

    Leader向生产者确认

    消息处理完成

    Leader故障

    从ISR选举新Leader

    继续处理下一消息

    恢复未同步消息

    四、Leader Epoch机制详解

    4.1 Leader Epoch的提出背景

    在早期的Kafka版本中,当Broker发生故障并恢复后,如果旧的Leader在故障前已经处理了一些请求但未能同步到Follower,而这些请求在重启后又被当作新请求处理,就可能导致数据不一致问题。具体表现为:

  • 数据重复:旧Leader已处理但未同步的消息在新周期中再次被处理。
  • 数据丢失:新选举的Leader未包含某些已确认的消息。
  • 顺序错乱:消息处理顺序在不同副本间不一致。
  • 为了解决这些问题,Kafka引入了Leader Epoch机制,通过为每个Leader任期分配一个唯一的递增数字,并记录每个消息对应的Leader Epoch,从而避免旧Leader继续处理已经过期的请求。

    下面展示没有Leader Epoch机制时可能出现的问题:

    #publish-mermaid-1788278822440-6{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-1788278822440-6 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278822440-6 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278822440-6 .error-icon{fill:#552222;}#publish-mermaid-1788278822440-6 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1788278822440-6 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1788278822440-6 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1788278822440-6 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1788278822440-6 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1788278822440-6 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1788278822440-6 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1788278822440-6 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1788278822440-6 .marker.cross{stroke:#333333;}#publish-mermaid-1788278822440-6 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1788278822440-6 p{margin:0;}#publish-mermaid-1788278822440-6 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1788278822440-6 .cluster-label text{fill:#333;}#publish-mermaid-1788278822440-6 .cluster-label span{color:#333;}#publish-mermaid-1788278822440-6 .cluster-label span p{background-color:transparent;}#publish-mermaid-1788278822440-6 .label text,#publish-mermaid-1788278822440-6 span{fill:#333;color:#333;}#publish-mermaid-1788278822440-6 .node rect,#publish-mermaid-1788278822440-6 .node circle,#publish-mermaid-1788278822440-6 .node ellipse,#publish-mermaid-1788278822440-6 .node polygon,#publish-mermaid-1788278822440-6 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788278822440-6 .rough-node .label text,#publish-mermaid-1788278822440-6 .node .label text,#publish-mermaid-1788278822440-6 .image-shape .label,#publish-mermaid-1788278822440-6 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1788278822440-6 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1788278822440-6 .rough-node .label,#publish-mermaid-1788278822440-6 .node .label,#publish-mermaid-1788278822440-6 .image-shape .label,#publish-mermaid-1788278822440-6 .icon-shape .label{text-align:center;}#publish-mermaid-1788278822440-6 .node.clickable{cursor:pointer;}#publish-mermaid-1788278822440-6 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1788278822440-6 .arrowheadPath{fill:#333333;}#publish-mermaid-1788278822440-6 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1788278822440-6 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1788278822440-6 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1788278822440-6 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822440-6 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822440-6 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1788278822440-6 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1788278822440-6 .cluster text{fill:#333;}#publish-mermaid-1788278822440-6 .cluster span{color:#333;}#publish-mermaid-1788278822440-6 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#publish-mermaid-1788278822440-6 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1788278822440-6 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1788278822440-6 .icon-shape,#publish-mermaid-1788278822440-6 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1788278822440-6 .icon-shape p,#publish-mermaid-1788278822440-6 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1788278822440-6 .icon-shape .label rect,#publish-mermaid-1788278822440-6 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822440-6 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1788278822440-6 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1788278822440-6 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1788278822440-6 [data-look=\”neo\”].node rect,#publish-mermaid-1788278822440-6 [data-look=\”neo\”].cluster rect,#publish-mermaid-1788278822440-6 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822440-6 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1788278822440-6 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788278822440-6 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822440-6 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1788278822440-6 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822440-6 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1788278822440-6 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822440-6 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822440-6 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    正常状态

    Leader故障

    选举新Leader

    旧Leader恢复

    问题出现

    旧Leader继续处理请求

    与Leader不一致

    数据重复或丢失

    4.2 Leader Epoch实现原理

    Leader Epoch机制通过以下原理实现数据一致性保障:

    Leader Epoch的概念

    Leader Epoch由两部分组成:

  • Epoch号:一个单调递增的数字,每次新的Leader选举都会增加该数字。
  • 起始偏移量:该Epoch起始时的消息偏移量。
  • 每个Leader维护一个Leader Epoch记录,记录每个Epoch的起始偏移量。这些记录持久化到本地,并在集群间传播。

    Leader Epoch的实现机制

    Leader Epoch的实现机制主要包括:

  • Leader选举时,为新的Leader分配递增的Epoch号。
  • Leader在处理请求时,将当前Epoch号记录在日志中。
  • Follower在拉取数据时,同步Leader的Epoch信息。
  • Broker恢复时,根据最新的Epoch信息确定应该从何处继续处理请求,避免处理已过期的请求。
  • 下面展示Leader Epoch的实现原理:

    新LeaderFollower旧Leader新LeaderFollower旧Leader#publish-mermaid-1788278822512-7{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-1788278822512-7 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278822512-7 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278822512-7 .error-icon{fill:#552222;}#publish-mermaid-1788278822512-7 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1788278822512-7 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1788278822512-7 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1788278822512-7 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1788278822512-7 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1788278822512-7 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1788278822512-7 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1788278822512-7 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1788278822512-7 .marker.cross{stroke:#333333;}#publish-mermaid-1788278822512-7 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1788278822512-7 p{margin:0;}#publish-mermaid-1788278822512-7 .actor{stroke:#9370DB;fill:#ECECFF;stroke-width:1;}#publish-mermaid-1788278822512-7 rect.actor.outer-path[data-look=\”neo\”]{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822512-7 rect.note[data-look=\”neo\”]{stroke:#aaaa33;fill:#fff5ad;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822512-7 text.actor>tspan{fill:black;stroke:none;}#publish-mermaid-1788278822512-7 .actor-line{stroke:#9370DB;}#publish-mermaid-1788278822512-7 .innerArc{stroke-width:1.5;stroke-dasharray:none;}#publish-mermaid-1788278822512-7 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#publish-mermaid-1788278822512-7 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#publish-mermaid-1788278822512-7 [id$=\”-arrowhead\”] path{fill:#333;stroke:#333;}#publish-mermaid-1788278822512-7 .sequenceNumber{fill:white;}#publish-mermaid-1788278822512-7 [id$=\”-sequencenumber\”]{fill:#333;}#publish-mermaid-1788278822512-7 [id$=\”-crosshead\”] path{fill:#333;stroke:#333;}#publish-mermaid-1788278822512-7 .messageText{fill:#333;stroke:none;}#publish-mermaid-1788278822512-7 .labelBox{stroke:#9370DB;fill:#ECECFF;filter:none;}#publish-mermaid-1788278822512-7 .labelText,#publish-mermaid-1788278822512-7 .labelText>tspan{fill:black;stroke:none;}#publish-mermaid-1788278822512-7 .loopText,#publish-mermaid-1788278822512-7 .loopText>tspan{fill:black;stroke:none;}#publish-mermaid-1788278822512-7 .sectionTitle,#publish-mermaid-1788278822512-7 .sectionTitle>tspan{fill:black;stroke:none;}#publish-mermaid-1788278822512-7 .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:#9370DB;fill:#9370DB;}#publish-mermaid-1788278822512-7 .note{stroke:#aaaa33;fill:#fff5ad;}#publish-mermaid-1788278822512-7 .noteText,#publish-mermaid-1788278822512-7 .noteText>tspan{fill:black;stroke:none;font-weight:normal;}#publish-mermaid-1788278822512-7 .activation0{fill:#f4f4f4;stroke:#666;}#publish-mermaid-1788278822512-7 .activation1{fill:#f4f4f4;stroke:#666;}#publish-mermaid-1788278822512-7 .activation2{fill:#f4f4f4;stroke:#666;}#publish-mermaid-1788278822512-7 .actorPopupMenu{position:absolute;}#publish-mermaid-1788278822512-7 .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#publish-mermaid-1788278822512-7 .actor-man circle,#publish-mermaid-1788278822512-7 line{fill:#ECECFF;stroke-width:2px;}#publish-mermaid-1788278822512-7 g rect.rect{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));stroke:#9370DB;}#publish-mermaid-1788278822512-7 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1788278822512-7 [data-look=\”neo\”].node rect,#publish-mermaid-1788278822512-7 [data-look=\”neo\”].cluster rect,#publish-mermaid-1788278822512-7 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822512-7 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1788278822512-7 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788278822512-7 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822512-7 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1788278822512-7 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822512-7 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1788278822512-7 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822512-7 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822512-7 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}处理消息1记录Epoch 1同步消息1记录Epoch 1Leader故障选举为Leader分配Epoch 2恢复检查Epoch发现自己的Epoch已过期同步Epoch 2更新Epoch信息处理后续消息

    Leader Epoch的持久化与传播

    Leader Epoch的持久化与传播机制确保了Epoch信息在整个集群中的一致性:

  • Leader Epoch记录持久化到本地文件中,包含每个Epoch的起始偏移量。
  • 当新的Leader选举时,最新的Epoch信息会被记录到ZooKeeper中。
  • 其他Broker通过监听ZooKeeper中的Leader变化获取最新的Epoch信息。
  • 生产者和消费者通过元数据请求获取最新的Epoch信息,避免与过期的Leader交互。
  • 4.3 Leader Epoch在故障恢复中的应用

    Leader Epoch机制在故障恢复中起到关键作用,确保数据一致性和系统稳定性。下面详细解析Leader Epoch在故障恢复中的应用。

    Leader故障恢复流程

    当Broker中的Leader副本发生故障时,Leader Epoch机制的工作流程如下:

  • Controller检测到Leader故障,触发Leader选举。
  • 从ISR集合中选择新的Leader,并为其分配递增的Epoch号。
  • 新的Leader记录新的Epoch信息,并开始处理请求。
  • 旧Leader恢复后,通过比较自己的Epoch与集群中的最新Epoch,确定自己是否仍然有效。
  • 如果旧Leader的Epoch较旧,它将停止处理请求,转为Follower角色。
  • 下面展示Leader故障恢复的详细流程:

    #publish-mermaid-1788278822561-8{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-1788278822561-8 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278822561-8 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278822561-8 .error-icon{fill:#552222;}#publish-mermaid-1788278822561-8 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1788278822561-8 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1788278822561-8 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1788278822561-8 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1788278822561-8 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1788278822561-8 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1788278822561-8 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1788278822561-8 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1788278822561-8 .marker.cross{stroke:#333333;}#publish-mermaid-1788278822561-8 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1788278822561-8 p{margin:0;}#publish-mermaid-1788278822561-8 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1788278822561-8 .cluster-label text{fill:#333;}#publish-mermaid-1788278822561-8 .cluster-label span{color:#333;}#publish-mermaid-1788278822561-8 .cluster-label span p{background-color:transparent;}#publish-mermaid-1788278822561-8 .label text,#publish-mermaid-1788278822561-8 span{fill:#333;color:#333;}#publish-mermaid-1788278822561-8 .node rect,#publish-mermaid-1788278822561-8 .node circle,#publish-mermaid-1788278822561-8 .node ellipse,#publish-mermaid-1788278822561-8 .node polygon,#publish-mermaid-1788278822561-8 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788278822561-8 .rough-node .label text,#publish-mermaid-1788278822561-8 .node .label text,#publish-mermaid-1788278822561-8 .image-shape .label,#publish-mermaid-1788278822561-8 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1788278822561-8 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1788278822561-8 .rough-node .label,#publish-mermaid-1788278822561-8 .node .label,#publish-mermaid-1788278822561-8 .image-shape .label,#publish-mermaid-1788278822561-8 .icon-shape .label{text-align:center;}#publish-mermaid-1788278822561-8 .node.clickable{cursor:pointer;}#publish-mermaid-1788278822561-8 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1788278822561-8 .arrowheadPath{fill:#333333;}#publish-mermaid-1788278822561-8 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1788278822561-8 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1788278822561-8 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1788278822561-8 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822561-8 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822561-8 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1788278822561-8 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1788278822561-8 .cluster text{fill:#333;}#publish-mermaid-1788278822561-8 .cluster span{color:#333;}#publish-mermaid-1788278822561-8 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#publish-mermaid-1788278822561-8 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1788278822561-8 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1788278822561-8 .icon-shape,#publish-mermaid-1788278822561-8 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1788278822561-8 .icon-shape p,#publish-mermaid-1788278822561-8 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1788278822561-8 .icon-shape .label rect,#publish-mermaid-1788278822561-8 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822561-8 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1788278822561-8 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1788278822561-8 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1788278822561-8 [data-look=\”neo\”].node rect,#publish-mermaid-1788278822561-8 [data-look=\”neo\”].cluster rect,#publish-mermaid-1788278822561-8 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822561-8 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1788278822561-8 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788278822561-8 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822561-8 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1788278822561-8 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822561-8 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1788278822561-8 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822561-8 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822561-8 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}是否

    Leader故障

    Controller检测故障

    从ISR选举新Leader

    分配递增的Epoch号

    新Leader开始处理请求

    旧Leader恢复

    比较Epoch

    是否过期

    转为Follower

    继续作为Leader

    Follower故障恢复

    当Follower副本发生故障时,Leader Epoch机制的工作流程如下:

  • Follower故障后,Leader将其从ISR中暂时移除。
  • Follower恢复后,重新连接到当前的Leader。
  • Follower将自己的Epoch与Leader的Epoch进行比较,确定需要同步的数据范围。
  • Follower从Leader拉取缺失的数据,确保数据一致性。
  • 当Follower追上Leader时,将其重新加入ISR。
  • Unclean Leader选举的规避

    Leader Epoch机制有效规避了Unclean Leader选举的问题:

  • 传统的Unclean Leader选举可能导致已确认的消息丢失。
  • 引入Leader Epoch后,只有ISR中的副本才能被选为新的Leader。
  • 这确保了已确认的消息至少在ISR的一个副本中存在,不会丢失。
  • 如果ISR中的所有副本都不可用,生产者的请求会失败,但不会导致数据不一致。
  • 下面展示Leader Epoch如何规避Unclean Leader选举问题:

    | 问题场景 | 传统解决方案 | Leader Epoch解决方案 | 结果对比 |

    |———|————–|———————|———-|

    | Leader故障,Follower未同步全部消息 | 从Follower中选举新Leader,可能丢失已确认消息 | 只从ISR中选举新Leader,确保数据不丢失 | Leader Epoch: 无数据丢失 |

    | ISR中所有副本都不可用 | 可能从非ISR副本选举,导致数据不一致 | 写入请求失败,保证数据一致性 | Leader Epoch: 数据一致性 |

    五、综合实践与优化建议

    5.1 消息可靠性配置最佳实践

    为了在Kafka中实现最佳的消息可靠性保障,需要综合考虑ACK机制、ISR同步和Leader Epoch等多个方面的配置。以下是一些最佳实践建议:

    生产者端配置最佳实践

  • 设置合适的acks级别:
    • 关键数据业务:acks=all,确保最高可靠性
    • 一般业务:acks=1,平衡性能与可靠性
    • 日志类业务:acks=0,追求高吞吐量
  • 调整retries和max.in.flight.requests.per.connection参数:
  • ```properties

    retries=3

    max.in.flight.requests.per.connection=1

    ```

    retries设置重试次数,max.in.flight.requests.per.connection=1确保消息发送顺序并避免重试乱序

  • 设置合理的linger.ms和batch.size参数,平衡吞吐量和延迟:
  • ```properties

    linger.ms=10

    batch.size=16384

    ```

  • 启用idempotent producer,确保生产者幂等性:
  • ```properties

    enable.idempotence=true

    ```

    Broker端配置最佳实践

  • 设置合适的replication.factor和min.insync.replicas:
  • ```properties

    replication.factor=3

    min.insync.replicas=2

    ```

    确保每个分区至少有2个副本可用

  • 调整replica.lag.time.max.ms,平衡ISR稳定性和故障恢复速度:
  • ```properties

    replica.lag.time.max.ms=30000

    ```

  • 设置适当的unclean.leader.election.enable,避免数据不一致:
  • ```properties

    unclean.leader.election.enable=false

    ```

  • 启用auto.leader.rebalance.enable,自动平衡Leader负载:
  • ```properties

    auto.leader.rebalance.enable=true

    ```

    消费者端配置最佳实践

  • 设置合理的auto.offset.reset,避免消息丢失或重复消费:
  • ```properties

    auto.offset.reset=earliest

    ```

  • 在消费处理完成后手动提交偏移量,避免处理失败导致消息丢失:
  • ```java

    consumer.commitSync();

    ```

  • 设置enable.auto.commit=false,改为手动提交偏移量:
  • ```properties

    enable.auto.commit=false

    ```

    下面是一个配置对比表,展示不同场景下的推荐配置:

    | 配置项 | 高可靠性场景 | 高性能场景 | 平衡场景 |

    |——–|————–|————|———-|

    | acks | all | 0 | 1 |

    | replication.factor | 3-5 | 2-3 | 3 |

    | min.insync.replicas | 2-3 | 1-2 | 2 |

    | retries | 5-10 | 0-3 | 3 |

    | max.in.flight.requests | 1 | 5 | 3 |

    5.2 性能与可靠性的平衡策略

    在Kafka系统设计中,性能和可靠性往往是相互制约的两个方面。如何在保证可靠性的同时获得良好的性能表现,是系统设计的关键挑战。以下是一些平衡策略:

    分区策略优化

    合理的分区策略可以平衡负载并提高可靠性:

  • 增加分区数量提高吞吐量,但注意不要过多导致元数据开销增加。
  • 均衡分区分布,避免某些Broker负载过重。
  • 关键业务使用较少分区,提高可靠性;非关键业务使用较多分区,提高吞吐量。
  • #publish-mermaid-1788278822672-9{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-1788278822672-9 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278822672-9 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278822672-9 .error-icon{fill:#552222;}#publish-mermaid-1788278822672-9 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1788278822672-9 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1788278822672-9 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1788278822672-9 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1788278822672-9 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1788278822672-9 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1788278822672-9 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1788278822672-9 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1788278822672-9 .marker.cross{stroke:#333333;}#publish-mermaid-1788278822672-9 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1788278822672-9 p{margin:0;}#publish-mermaid-1788278822672-9 .pieCircle{stroke:black;stroke-width:2px;opacity:0.7;}#publish-mermaid-1788278822672-9 .pieCircle.highlighted{scale:1.05;opacity:1;}#publish-mermaid-1788278822672-9 .pieCircle.highlightedOnHover:hover{transition-duration:250ms;scale:1.05;opacity:1;}#publish-mermaid-1788278822672-9 .pieOuterCircle{stroke:black;stroke-width:2px;fill:none;}#publish-mermaid-1788278822672-9 .pieTitleText{text-anchor:middle;font-size:25px;fill:black;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}#publish-mermaid-1788278822672-9 .slice{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;fill:#333;font-size:17px;}#publish-mermaid-1788278822672-9 .legend text{fill:black;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:17px;}#publish-mermaid-1788278822672-9 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1788278822672-9 [data-look=\”neo\”].node rect,#publish-mermaid-1788278822672-9 [data-look=\”neo\”].cluster rect,#publish-mermaid-1788278822672-9 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822672-9 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1788278822672-9 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788278822672-9 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822672-9 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1788278822672-9 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822672-9 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1788278822672-9 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822672-9 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822672-9 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}35%45%20%分区数量对可靠性和性能的影响少量分区适量分区大量分区

    批处理与压缩优化

    批处理和压缩技术可以在不影响可靠性的情况下提高性能:

  • 增大batch.size参数,提高批量发送效率,但会增加内存使用。
  • 启用消息压缩,减少网络传输量和存储空间:
  • ```properties

    compression.type=snappy

    ```

  • 根据业务特点选择合适的压缩算法,平衡CPU使用和压缩率。
  • 磁盘IO优化

    磁盘IO是影响Kafka性能的重要因素,可以通过以下策略优化:

  • 使用SSD替代HDD,提高读写性能。
  • 将日志数据与操作系统文件系统分离,避免相互干扰。
  • 适当调整log.flush.interval.messages和log.flush.interval.ms参数,平衡数据持久化和性能:
  • ```properties

    log.flush.interval.messages=10000

    log.flush.interval.ms=1000

    ```

    网络优化

    网络因素对Kafka性能和可靠性都有重要影响:

  • 使用低延迟网络连接,尤其是Broker集群内部。
  • 合理设置socket.send.buffer.bytes和socket.receive.buffer.bytes参数:
  • ```properties

    socket.send.buffer.bytes=102400

    socket.receive.buffer.bytes=102400

    ```

  • 适当调整num.network.threads和num.io.threads参数,平衡CPU和IO负载:
  • ```properties

    num.network.threads=8

    num.io.threads=8

    ```

    下面是一个综合优化建议表,从不同维度提供优化策略:

    | 优化维度 | 可靠性优先策略 | 性能优先策略 | 平衡策略 |

    |———-|—————-|————–|———-|

    | 分区设置 | 较少分区,较多副本 | 较多分区,较少副本 | 适中分区数,3副本 |

    | 批处理 | 较小批次,频繁提交 | 较大批次,延迟提交 | 中等批次大小 |

    | 消息压缩 | 无压缩或低压缩比 | 高压缩比,启用压缩 | 中等压缩比 |

    | ISR维护 | 较长滞后时间,更多副本 | 较短滞后时间,较少副本 | 适中滞后时间 |

    | 磁盘IO | 频繁刷盘,SSD | 批量刷盘,HDD | 适中刷盘频率,SSD |

    5.3 常见问题与解决方案

    在Kafka的实际应用中,即使正确配置了各种可靠性机制,仍可能会遇到各种问题。以下列出了一些常见问题及其解决方案。

    问题一:生产者消息丢失

    现象:生产者发送的消息在某些情况下未能成功到达Broker。

    可能原因:

  • 生产者配置了acks=0,且Broker在确认前崩溃
  • 生产者重试次数不足
  • 网络分区导致消息无法到达
  • 解决方案:

  • 对于关键业务,配置acks=all并设置适当重试次数:
  • ```properties

    acks=all

    retries=3

    ```

  • 启用幂等生产者,避免网络分区导致的重复消息:
  • ```properties

    enable.idempotence=true

    ```

  • 监控生产者的发送成功率和延迟,及时发现异常情况
  • 问题二:ISR频繁变化

    现象:ISR集合中的副本频繁变化,影响系统稳定性。

    可能原因:

  • Broker间网络不稳定
  • 磁盘IO压力大,Follower无法及时同步
  • Broker资源配置不足
  • 解决方案:

  • 优化网络环境,减少网络抖动
  • 增加Broker资源,特别是磁盘性能
  • 调整replica.lag.time.max.ms参数,给Follower更多同步时间:
  • ```properties

    replica.lag.time.max.ms=60000

    ```

    问题三:Leader选举导致服务中断

    现象:当Leader副本故障时,系统需要时间选举新Leader,导致服务短暂中断。

    可能原因:

  • ISR中副本数量不足,无法及时选举新Leader
  • Controller节点负载过高,选举过程延迟
  • 网络分区影响选举过程
  • 解决方案:

  • 适当增加副本数量,确保每个分区有足够的ISR副本:
  • ```properties

    replication.factor=3

    min.insync.replicas=2

    ```

  • 优化Controller节点的资源配置
  • 确保Broker集群网络稳定,减少网络分区可能性
  • 问题四:消费者重复消费

    现象:消费者在某些情况下重复消费已处理的消息。

    可能原因:

  • 消费者自动提交偏移量时机不当
  • 消费者处理消息失败后偏移量已被提交
  • 消费者重平衡导致偏移量重置
  • 解决方案:

  • 使用手动提交偏移量,确保消息处理成功后才提交:
  • ```java

    // 处理消息完成后

    consumer.commitSync();

    ```

  • 实现幂等消费逻辑,处理重复消息不影响业务
  • 合理设置消费者重平衡策略,避免频繁重平衡:
  • #publish-mermaid-1788278822691-10{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-1788278822691-10 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278822691-10 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1788278822691-10 .error-icon{fill:#552222;}#publish-mermaid-1788278822691-10 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1788278822691-10 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1788278822691-10 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1788278822691-10 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1788278822691-10 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1788278822691-10 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1788278822691-10 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1788278822691-10 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1788278822691-10 .marker.cross{stroke:#333333;}#publish-mermaid-1788278822691-10 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1788278822691-10 p{margin:0;}#publish-mermaid-1788278822691-10 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1788278822691-10 .cluster-label text{fill:#333;}#publish-mermaid-1788278822691-10 .cluster-label span{color:#333;}#publish-mermaid-1788278822691-10 .cluster-label span p{background-color:transparent;}#publish-mermaid-1788278822691-10 .label text,#publish-mermaid-1788278822691-10 span{fill:#333;color:#333;}#publish-mermaid-1788278822691-10 .node rect,#publish-mermaid-1788278822691-10 .node circle,#publish-mermaid-1788278822691-10 .node ellipse,#publish-mermaid-1788278822691-10 .node polygon,#publish-mermaid-1788278822691-10 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788278822691-10 .rough-node .label text,#publish-mermaid-1788278822691-10 .node .label text,#publish-mermaid-1788278822691-10 .image-shape .label,#publish-mermaid-1788278822691-10 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1788278822691-10 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1788278822691-10 .rough-node .label,#publish-mermaid-1788278822691-10 .node .label,#publish-mermaid-1788278822691-10 .image-shape .label,#publish-mermaid-1788278822691-10 .icon-shape .label{text-align:center;}#publish-mermaid-1788278822691-10 .node.clickable{cursor:pointer;}#publish-mermaid-1788278822691-10 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1788278822691-10 .arrowheadPath{fill:#333333;}#publish-mermaid-1788278822691-10 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1788278822691-10 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1788278822691-10 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1788278822691-10 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822691-10 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822691-10 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1788278822691-10 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1788278822691-10 .cluster text{fill:#333;}#publish-mermaid-1788278822691-10 .cluster span{color:#333;}#publish-mermaid-1788278822691-10 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#publish-mermaid-1788278822691-10 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1788278822691-10 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1788278822691-10 .icon-shape,#publish-mermaid-1788278822691-10 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1788278822691-10 .icon-shape p,#publish-mermaid-1788278822691-10 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1788278822691-10 .icon-shape .label rect,#publish-mermaid-1788278822691-10 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1788278822691-10 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1788278822691-10 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1788278822691-10 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1788278822691-10 [data-look=\”neo\”].node rect,#publish-mermaid-1788278822691-10 [data-look=\”neo\”].cluster rect,#publish-mermaid-1788278822691-10 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822691-10 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1788278822691-10 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788278822691-10 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822691-10 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1788278822691-10 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822691-10 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1788278822691-10 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822691-10 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788278822691-10 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}是否

    消息处理开始

    获取消息

    处理消息

    处理是否成功

    提交偏移量

    重试或记录错误

    消息处理结束

    下面是一个综合问题诊断和解决方案表:

    | 问题类型 | 诊断方法 | 解决方案 | 预防措施 |

    |———-|———-|———-|———-|

    | 消息丢失 | 检查生产者配置、Broker日志、监控指标 | 配置适当ACK级别、增加重试次数、启用幂等生产者 | 监控关键指标、完善告警机制 |

    | ISR变化 | 监控ISR大小变化、网络状态、磁盘IO | 优化网络、增加资源、调整参数 | 确保资源充足、网络稳定 |

    | Leader选举 | 监控Leader变化频率、选举耗时、ISR状态 | 增加副本数量、优化资源配置 | 合理规划副本分布、监控系统负载 |

    | 重复消费 | 检查消费者提交偏移量时机、处理结果 | 手动提交偏移量、实现幂等消费 | 设计合理的消费逻辑、监控消费行为 |

    赞(0)
    未经允许不得转载:171主机测评 » Kafka消息不丢的底层保障:ACK机制、ISR同步与Leader Epoch详解
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址