Kafka ISR与AR深度解析:副本同步机制核心概念
-
- 一、核心概念定义
-
- [1.1 AR:分区的所有副本](#1.1 AR:分区的所有副本)
- [1.2 ISR:与Leader保持同步的副本](#1.2 ISR:与Leader保持同步的副本)
- [1.3 官方定义](#1.3 官方定义)
- 二、ISR的判定标准
-
- [2.1 同步的判断条件](#2.1 同步的判断条件)
- [2.2 关键参数](#2.2 关键参数)
- [2.3 ISR的动态变化](#2.3 ISR的动态变化)
- 三、ISR的核心作用
-
- [3.1 作用一:Leader选举的选民池](#3.1 作用一:Leader选举的选民池)
- [3.2 作用二:消息确认的基准](#3.2 作用二:消息确认的基准)
- [3.3 作用三:保证数据一致性](#3.3 作用三:保证数据一致性)
- 四、AR与ISR的关系图解
-
- [4.1 集合关系图](#4.1 集合关系图)
- [4.2 公式表示](#4.2 公式表示)
- [4.3 动态变化示例](#4.3 动态变化示例)
- 五、ISR相关的关键配置
-
- [5.1 Broker级别配置](#5.1 Broker级别配置)
- [5.2 Topic级别配置](#5.2 Topic级别配置)
- [5.3 Producer级别配置](#5.3 Producer级别配置)
- 六、ISR的实际应用场景
-
- [6.1 场景一:Leader选举](#6.1 场景一:Leader选举)
- [6.2 场景二:生产者的可靠性保证](#6.2 场景二:生产者的可靠性保证)
- [6.3 场景三:监控ISR变化](#6.3 场景三:监控ISR变化)
- 七、面试高频问题
-
- Q1:ISR和AR分别代表什么?
- Q2:副本被踢出ISR的条件是什么?
- Q3:为什么Leader选举只能从ISR中选?
- Q4:min.insync.replicas的作用是什么?
- [Q5:什么是unclean leader election?有什么风险?](#Q5:什么是unclean leader election?有什么风险?)
- 八、总结
-
- [8.1 核心概念关系](#8.1 核心概念关系)
- [8.2 关键公式](#8.2 关键公式)
- [8.3 一句话总结](#8.3 一句话总结)
|-----------------------------|
| 🌺The Begin🌺点点关注,收藏不迷路🌺 |
关键词:Kafka、ISR、AR、副本同步、Leader选举、高可用、数据一致性
在Kafka的分布式架构中,副本机制是保证高可用和数据一致性的基石。而**ISR(In-Sync Replicas)和AR(Assigned Replicas)**则是理解副本同步机制的两个核心概念。
今天,我们将深入剖析这两个概念的本质、作用以及它们在Kafka高可用架构中的关键地位。
一、核心概念定义
1.1 AR:分区的所有副本
AR(Assigned Replicas):一个分区分配的所有副本集合,包括Leader和所有Follower。
#mermaid-svg-mbCVJAsUHs27jED8{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;}}#mermaid-svg-mbCVJAsUHs27jED8 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-mbCVJAsUHs27jED8 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-mbCVJAsUHs27jED8 .error-icon{fill:#552222;}#mermaid-svg-mbCVJAsUHs27jED8 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-mbCVJAsUHs27jED8 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-mbCVJAsUHs27jED8 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-mbCVJAsUHs27jED8 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-mbCVJAsUHs27jED8 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-mbCVJAsUHs27jED8 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-mbCVJAsUHs27jED8 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-mbCVJAsUHs27jED8 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-mbCVJAsUHs27jED8 .marker.cross{stroke:#333333;}#mermaid-svg-mbCVJAsUHs27jED8 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-mbCVJAsUHs27jED8 p{margin:0;}#mermaid-svg-mbCVJAsUHs27jED8 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-mbCVJAsUHs27jED8 .cluster-label text{fill:#333;}#mermaid-svg-mbCVJAsUHs27jED8 .cluster-label span{color:#333;}#mermaid-svg-mbCVJAsUHs27jED8 .cluster-label span p{background-color:transparent;}#mermaid-svg-mbCVJAsUHs27jED8 .label text,#mermaid-svg-mbCVJAsUHs27jED8 span{fill:#333;color:#333;}#mermaid-svg-mbCVJAsUHs27jED8 .node rect,#mermaid-svg-mbCVJAsUHs27jED8 .node circle,#mermaid-svg-mbCVJAsUHs27jED8 .node ellipse,#mermaid-svg-mbCVJAsUHs27jED8 .node polygon,#mermaid-svg-mbCVJAsUHs27jED8 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-mbCVJAsUHs27jED8 .rough-node .label text,#mermaid-svg-mbCVJAsUHs27jED8 .node .label text,#mermaid-svg-mbCVJAsUHs27jED8 .image-shape .label,#mermaid-svg-mbCVJAsUHs27jED8 .icon-shape .label{text-anchor:middle;}#mermaid-svg-mbCVJAsUHs27jED8 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-mbCVJAsUHs27jED8 .rough-node .label,#mermaid-svg-mbCVJAsUHs27jED8 .node .label,#mermaid-svg-mbCVJAsUHs27jED8 .image-shape .label,#mermaid-svg-mbCVJAsUHs27jED8 .icon-shape .label{text-align:center;}#mermaid-svg-mbCVJAsUHs27jED8 .node.clickable{cursor:pointer;}#mermaid-svg-mbCVJAsUHs27jED8 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-mbCVJAsUHs27jED8 .arrowheadPath{fill:#333333;}#mermaid-svg-mbCVJAsUHs27jED8 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-mbCVJAsUHs27jED8 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-mbCVJAsUHs27jED8 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-mbCVJAsUHs27jED8 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-mbCVJAsUHs27jED8 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-mbCVJAsUHs27jED8 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-mbCVJAsUHs27jED8 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-mbCVJAsUHs27jED8 .cluster text{fill:#333;}#mermaid-svg-mbCVJAsUHs27jED8 .cluster span{color:#333;}#mermaid-svg-mbCVJAsUHs27jED8 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;}#mermaid-svg-mbCVJAsUHs27jED8 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-mbCVJAsUHs27jED8 rect.text{fill:none;stroke-width:0;}#mermaid-svg-mbCVJAsUHs27jED8 .icon-shape,#mermaid-svg-mbCVJAsUHs27jED8 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-mbCVJAsUHs27jED8 .icon-shape p,#mermaid-svg-mbCVJAsUHs27jED8 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-mbCVJAsUHs27jED8 .icon-shape rect,#mermaid-svg-mbCVJAsUHs27jED8 .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-mbCVJAsUHs27jED8 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-mbCVJAsUHs27jED8 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-mbCVJAsUHs27jED8 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 一个分区的AR
Leader副本
负责读写
AR集合
Follower副本1
同步数据
Follower副本2
同步数据
AR = Leader + 所有Follower
1.2 ISR:与Leader保持同步的副本
ISR(In-Sync Replicas) :与Leader保持同步的Follower集合,包括Leader自身。只有ISR中的副本才有资格被选举为新的Leader。
#mermaid-svg-hWT6wIQ4lPzc6WLk{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;}}#mermaid-svg-hWT6wIQ4lPzc6WLk .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-hWT6wIQ4lPzc6WLk .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-hWT6wIQ4lPzc6WLk .error-icon{fill:#552222;}#mermaid-svg-hWT6wIQ4lPzc6WLk .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-hWT6wIQ4lPzc6WLk .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-hWT6wIQ4lPzc6WLk .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-hWT6wIQ4lPzc6WLk .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-hWT6wIQ4lPzc6WLk .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-hWT6wIQ4lPzc6WLk .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-hWT6wIQ4lPzc6WLk .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-hWT6wIQ4lPzc6WLk .marker{fill:#333333;stroke:#333333;}#mermaid-svg-hWT6wIQ4lPzc6WLk .marker.cross{stroke:#333333;}#mermaid-svg-hWT6wIQ4lPzc6WLk svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-hWT6wIQ4lPzc6WLk p{margin:0;}#mermaid-svg-hWT6wIQ4lPzc6WLk .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-hWT6wIQ4lPzc6WLk .cluster-label text{fill:#333;}#mermaid-svg-hWT6wIQ4lPzc6WLk .cluster-label span{color:#333;}#mermaid-svg-hWT6wIQ4lPzc6WLk .cluster-label span p{background-color:transparent;}#mermaid-svg-hWT6wIQ4lPzc6WLk .label text,#mermaid-svg-hWT6wIQ4lPzc6WLk span{fill:#333;color:#333;}#mermaid-svg-hWT6wIQ4lPzc6WLk .node rect,#mermaid-svg-hWT6wIQ4lPzc6WLk .node circle,#mermaid-svg-hWT6wIQ4lPzc6WLk .node ellipse,#mermaid-svg-hWT6wIQ4lPzc6WLk .node polygon,#mermaid-svg-hWT6wIQ4lPzc6WLk .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-hWT6wIQ4lPzc6WLk .rough-node .label text,#mermaid-svg-hWT6wIQ4lPzc6WLk .node .label text,#mermaid-svg-hWT6wIQ4lPzc6WLk .image-shape .label,#mermaid-svg-hWT6wIQ4lPzc6WLk .icon-shape .label{text-anchor:middle;}#mermaid-svg-hWT6wIQ4lPzc6WLk .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-hWT6wIQ4lPzc6WLk .rough-node .label,#mermaid-svg-hWT6wIQ4lPzc6WLk .node .label,#mermaid-svg-hWT6wIQ4lPzc6WLk .image-shape .label,#mermaid-svg-hWT6wIQ4lPzc6WLk .icon-shape .label{text-align:center;}#mermaid-svg-hWT6wIQ4lPzc6WLk .node.clickable{cursor:pointer;}#mermaid-svg-hWT6wIQ4lPzc6WLk .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-hWT6wIQ4lPzc6WLk .arrowheadPath{fill:#333333;}#mermaid-svg-hWT6wIQ4lPzc6WLk .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-hWT6wIQ4lPzc6WLk .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-hWT6wIQ4lPzc6WLk .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-hWT6wIQ4lPzc6WLk .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-hWT6wIQ4lPzc6WLk .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-hWT6wIQ4lPzc6WLk .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-hWT6wIQ4lPzc6WLk .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-hWT6wIQ4lPzc6WLk .cluster text{fill:#333;}#mermaid-svg-hWT6wIQ4lPzc6WLk .cluster span{color:#333;}#mermaid-svg-hWT6wIQ4lPzc6WLk 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;}#mermaid-svg-hWT6wIQ4lPzc6WLk .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-hWT6wIQ4lPzc6WLk rect.text{fill:none;stroke-width:0;}#mermaid-svg-hWT6wIQ4lPzc6WLk .icon-shape,#mermaid-svg-hWT6wIQ4lPzc6WLk .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-hWT6wIQ4lPzc6WLk .icon-shape p,#mermaid-svg-hWT6wIQ4lPzc6WLk .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-hWT6wIQ4lPzc6WLk .icon-shape rect,#mermaid-svg-hWT6wIQ4lPzc6WLk .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-hWT6wIQ4lPzc6WLk .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-hWT6wIQ4lPzc6WLk .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-hWT6wIQ4lPzc6WLk :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} ISR与AR的关系
ISR动态集合
ISR: 副本0,1,2,3
与Leader同步的4个副本
主题分区
AR: 副本0,1,2,3,4
分配的所有5个副本
Leader 0
始终在ISR中
Follower 1
同步正常
Follower 2
同步正常
Follower 3
同步正常
Follower 4
同步落后
不在ISR中
1.3 官方定义
| 概念 | 全称 | 定义 | 包含 |
|---|---|---|---|
| AR | Assigned Replicas | 分区分配的所有副本 | Leader + 所有Follower |
| ISR | In-Sync Replicas | 与Leader保持同步的副本 | Leader + 同步的Follower |
二、ISR的判定标准
2.1 同步的判断条件
java
// Kafka源码中的判断逻辑(概念性)
class ReplicaManager {
def isInSync(replica: Replica): Boolean = {
// 1. 必须是Follower(或Leader)
// 2. 必须与Leader保持同步
// 主要判断依据:
// - Follower的LEO(Log End Offset)要接近Leader的LEO
// - Follower的拉取请求要及时
// - 在replica.lag.time.max.ms时间内有拉取请求
val maxLagMs = config.replicaLagTimeMaxMs
val lastCaughtUpTimeMs = replica.lastCaughtUpTimeMs
val currentTimeMs = time.milliseconds()
// 如果最后一次追上Leader的时间在允许范围内
(currentTimeMs - lastCaughtUpTimeMs) <= maxLagMs
}
}
2.2 关键参数
properties
# Kafka Broker配置
# 1. 副本落后最大时间阈值
replica.lag.time.max.ms=30000 # 默认30秒
# 如果Follower超过30秒没有拉取消息,就会被踢出ISR
# 2. 最小同步副本数
min.insync.replicas=2 # Topic级别或Broker级别配置
# 生产者在acks=all时需要ISR中至少有这么多个副本
2.3 ISR的动态变化
#mermaid-svg-VcEUQsaGmKdXoV6m{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;}}#mermaid-svg-VcEUQsaGmKdXoV6m .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-VcEUQsaGmKdXoV6m .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-VcEUQsaGmKdXoV6m .error-icon{fill:#552222;}#mermaid-svg-VcEUQsaGmKdXoV6m .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-VcEUQsaGmKdXoV6m .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-VcEUQsaGmKdXoV6m .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-VcEUQsaGmKdXoV6m .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-VcEUQsaGmKdXoV6m .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-VcEUQsaGmKdXoV6m .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-VcEUQsaGmKdXoV6m .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-VcEUQsaGmKdXoV6m .marker{fill:#333333;stroke:#333333;}#mermaid-svg-VcEUQsaGmKdXoV6m .marker.cross{stroke:#333333;}#mermaid-svg-VcEUQsaGmKdXoV6m svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-VcEUQsaGmKdXoV6m p{margin:0;}#mermaid-svg-VcEUQsaGmKdXoV6m .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-VcEUQsaGmKdXoV6m .cluster-label text{fill:#333;}#mermaid-svg-VcEUQsaGmKdXoV6m .cluster-label span{color:#333;}#mermaid-svg-VcEUQsaGmKdXoV6m .cluster-label span p{background-color:transparent;}#mermaid-svg-VcEUQsaGmKdXoV6m .label text,#mermaid-svg-VcEUQsaGmKdXoV6m span{fill:#333;color:#333;}#mermaid-svg-VcEUQsaGmKdXoV6m .node rect,#mermaid-svg-VcEUQsaGmKdXoV6m .node circle,#mermaid-svg-VcEUQsaGmKdXoV6m .node ellipse,#mermaid-svg-VcEUQsaGmKdXoV6m .node polygon,#mermaid-svg-VcEUQsaGmKdXoV6m .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-VcEUQsaGmKdXoV6m .rough-node .label text,#mermaid-svg-VcEUQsaGmKdXoV6m .node .label text,#mermaid-svg-VcEUQsaGmKdXoV6m .image-shape .label,#mermaid-svg-VcEUQsaGmKdXoV6m .icon-shape .label{text-anchor:middle;}#mermaid-svg-VcEUQsaGmKdXoV6m .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-VcEUQsaGmKdXoV6m .rough-node .label,#mermaid-svg-VcEUQsaGmKdXoV6m .node .label,#mermaid-svg-VcEUQsaGmKdXoV6m .image-shape .label,#mermaid-svg-VcEUQsaGmKdXoV6m .icon-shape .label{text-align:center;}#mermaid-svg-VcEUQsaGmKdXoV6m .node.clickable{cursor:pointer;}#mermaid-svg-VcEUQsaGmKdXoV6m .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-VcEUQsaGmKdXoV6m .arrowheadPath{fill:#333333;}#mermaid-svg-VcEUQsaGmKdXoV6m .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-VcEUQsaGmKdXoV6m .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-VcEUQsaGmKdXoV6m .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-VcEUQsaGmKdXoV6m .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-VcEUQsaGmKdXoV6m .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-VcEUQsaGmKdXoV6m .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-VcEUQsaGmKdXoV6m .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-VcEUQsaGmKdXoV6m .cluster text{fill:#333;}#mermaid-svg-VcEUQsaGmKdXoV6m .cluster span{color:#333;}#mermaid-svg-VcEUQsaGmKdXoV6m 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;}#mermaid-svg-VcEUQsaGmKdXoV6m .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-VcEUQsaGmKdXoV6m rect.text{fill:none;stroke-width:0;}#mermaid-svg-VcEUQsaGmKdXoV6m .icon-shape,#mermaid-svg-VcEUQsaGmKdXoV6m .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-VcEUQsaGmKdXoV6m .icon-shape p,#mermaid-svg-VcEUQsaGmKdXoV6m .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-VcEUQsaGmKdXoV6m .icon-shape rect,#mermaid-svg-VcEUQsaGmKdXoV6m .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-VcEUQsaGmKdXoV6m .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-VcEUQsaGmKdXoV6m .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-VcEUQsaGmKdXoV6m :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Kafka ISR (In-Sync Replicas) 动态变化
ISR缩减
正常状态
Follower恢复
Follower4恢复
重新开始同步
追上Leader的offset
重新加入ISR
ISR: 0,1,2,3,4
恢复完整
ISR作用:
• 确保数据一致性
• 决定可用性
• 影响acks配置
Leader: 0
ISR: 0,1,2,3,4
参数配置:
• min.insync.replicas
ISR: 0,1,2,3
Follower4被踢出
剩余副本
不同步
Leader 0
Follower 1
Follower 2
Follower 3
Follower4
(非ISR)
Follower变慢
同步请求
超过阈值
Follower4
网络延迟/GC/负载高
Leader 0
Follower4
(默认30秒)
从ISR中移除
副本状态
Leader 0
(接收读写)
Follower 1
(同步中)
Follower 2
(同步中)
Follower 3
(同步中)
Follower 4
(同步中)
ISR: 与Leader保持同步的副本集合
三、ISR的核心作用
3.1 作用一:Leader选举的选民池
#mermaid-svg-JjZgIXw0KU9UwT3m{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;}}#mermaid-svg-JjZgIXw0KU9UwT3m .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-JjZgIXw0KU9UwT3m .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-JjZgIXw0KU9UwT3m .error-icon{fill:#552222;}#mermaid-svg-JjZgIXw0KU9UwT3m .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-JjZgIXw0KU9UwT3m .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-JjZgIXw0KU9UwT3m .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-JjZgIXw0KU9UwT3m .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-JjZgIXw0KU9UwT3m .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-JjZgIXw0KU9UwT3m .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-JjZgIXw0KU9UwT3m .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-JjZgIXw0KU9UwT3m .marker{fill:#333333;stroke:#333333;}#mermaid-svg-JjZgIXw0KU9UwT3m .marker.cross{stroke:#333333;}#mermaid-svg-JjZgIXw0KU9UwT3m svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-JjZgIXw0KU9UwT3m p{margin:0;}#mermaid-svg-JjZgIXw0KU9UwT3m .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-JjZgIXw0KU9UwT3m .cluster-label text{fill:#333;}#mermaid-svg-JjZgIXw0KU9UwT3m .cluster-label span{color:#333;}#mermaid-svg-JjZgIXw0KU9UwT3m .cluster-label span p{background-color:transparent;}#mermaid-svg-JjZgIXw0KU9UwT3m .label text,#mermaid-svg-JjZgIXw0KU9UwT3m span{fill:#333;color:#333;}#mermaid-svg-JjZgIXw0KU9UwT3m .node rect,#mermaid-svg-JjZgIXw0KU9UwT3m .node circle,#mermaid-svg-JjZgIXw0KU9UwT3m .node ellipse,#mermaid-svg-JjZgIXw0KU9UwT3m .node polygon,#mermaid-svg-JjZgIXw0KU9UwT3m .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-JjZgIXw0KU9UwT3m .rough-node .label text,#mermaid-svg-JjZgIXw0KU9UwT3m .node .label text,#mermaid-svg-JjZgIXw0KU9UwT3m .image-shape .label,#mermaid-svg-JjZgIXw0KU9UwT3m .icon-shape .label{text-anchor:middle;}#mermaid-svg-JjZgIXw0KU9UwT3m .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-JjZgIXw0KU9UwT3m .rough-node .label,#mermaid-svg-JjZgIXw0KU9UwT3m .node .label,#mermaid-svg-JjZgIXw0KU9UwT3m .image-shape .label,#mermaid-svg-JjZgIXw0KU9UwT3m .icon-shape .label{text-align:center;}#mermaid-svg-JjZgIXw0KU9UwT3m .node.clickable{cursor:pointer;}#mermaid-svg-JjZgIXw0KU9UwT3m .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-JjZgIXw0KU9UwT3m .arrowheadPath{fill:#333333;}#mermaid-svg-JjZgIXw0KU9UwT3m .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-JjZgIXw0KU9UwT3m .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-JjZgIXw0KU9UwT3m .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-JjZgIXw0KU9UwT3m .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-JjZgIXw0KU9UwT3m .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-JjZgIXw0KU9UwT3m .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-JjZgIXw0KU9UwT3m .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-JjZgIXw0KU9UwT3m .cluster text{fill:#333;}#mermaid-svg-JjZgIXw0KU9UwT3m .cluster span{color:#333;}#mermaid-svg-JjZgIXw0KU9UwT3m 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;}#mermaid-svg-JjZgIXw0KU9UwT3m .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-JjZgIXw0KU9UwT3m rect.text{fill:none;stroke-width:0;}#mermaid-svg-JjZgIXw0KU9UwT3m .icon-shape,#mermaid-svg-JjZgIXw0KU9UwT3m .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-JjZgIXw0KU9UwT3m .icon-shape p,#mermaid-svg-JjZgIXw0KU9UwT3m .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-JjZgIXw0KU9UwT3m .icon-shape rect,#mermaid-svg-JjZgIXw0KU9UwT3m .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-JjZgIXw0KU9UwT3m .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-JjZgIXw0KU9UwT3m .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-JjZgIXw0KU9UwT3m :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Kafka Leader选举流程
选出新Leader
选举过程
故障发生
初始状态
候选人对比
副本状态
数据落后
❌ 宕机
被选为
新Leader 1
Leader 0
(当前Leader)
Follower 1
(ISR中)
Follower 2
(ISR中)
Follower 3
(ISR中)
Follower 4
(不在ISR)
Leader 0 不可用
Controller检测到
Leader故障
开始选举
检查ISR列表
当前ISR: 1,2,3
Follower 1
✅ ISR中
数据最新
Follower 2
✅ ISR中
数据最新
Follower 3
✅ ISR中
数据最新
Follower 4
❌ 不在ISR
数据落后
更新ISR: 1,2,3
选举完成
选举规则:
• 优先从ISR中选择
• ISR中所有副本数据一致
• 保证数据不丢失
特殊情况:
• ISR为空时启用脏选举
• 可能丢失数据
• 配合unclean.leader.election
Topic: test
分区: 0
副本: 0,1,2,3,4
基于ISR的Leader选举机制
为什么只能用ISR选举?
- ISR中的副本数据与Leader基本一致
- 选它们做Leader不会丢失数据
- 不在ISR中的副本数据落后,当选会导致数据丢失
3.2 作用二:消息确认的基准
java
// Producer的acks参数
Properties props = new Properties();
// acks=0:不等待确认
props.put("acks", "0"); // 可能丢数据,性能最高
// acks=1:Leader确认即可
props.put("acks", "1"); // Leader写入成功就返回
// acks=all:ISR全部确认
props.put("acks", "all"); // 最安全,性能最低
// 或 acks=-1(等同于all)
acks=all的工作原理:
Follower3 (非ISR) Follower2 (ISR) Follower1 (ISR) Leader Producer Follower3 (非ISR) Follower2 (ISR) Follower1 (ISR) Leader Producer #mermaid-svg-FaK1JrCznY00BREX{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;}}#mermaid-svg-FaK1JrCznY00BREX .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-FaK1JrCznY00BREX .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-FaK1JrCznY00BREX .error-icon{fill:#552222;}#mermaid-svg-FaK1JrCznY00BREX .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-FaK1JrCznY00BREX .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-FaK1JrCznY00BREX .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-FaK1JrCznY00BREX .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-FaK1JrCznY00BREX .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-FaK1JrCznY00BREX .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-FaK1JrCznY00BREX .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-FaK1JrCznY00BREX .marker{fill:#333333;stroke:#333333;}#mermaid-svg-FaK1JrCznY00BREX .marker.cross{stroke:#333333;}#mermaid-svg-FaK1JrCznY00BREX svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-FaK1JrCznY00BREX p{margin:0;}#mermaid-svg-FaK1JrCznY00BREX .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-FaK1JrCznY00BREX text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-FaK1JrCznY00BREX .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-FaK1JrCznY00BREX .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-FaK1JrCznY00BREX .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-FaK1JrCznY00BREX .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-FaK1JrCznY00BREX #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-FaK1JrCznY00BREX .sequenceNumber{fill:white;}#mermaid-svg-FaK1JrCznY00BREX #sequencenumber{fill:#333;}#mermaid-svg-FaK1JrCznY00BREX #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-FaK1JrCznY00BREX .messageText{fill:#333;stroke:none;}#mermaid-svg-FaK1JrCznY00BREX .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-FaK1JrCznY00BREX .labelText,#mermaid-svg-FaK1JrCznY00BREX .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-FaK1JrCznY00BREX .loopText,#mermaid-svg-FaK1JrCznY00BREX .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-FaK1JrCznY00BREX .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-FaK1JrCznY00BREX .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-FaK1JrCznY00BREX .noteText,#mermaid-svg-FaK1JrCznY00BREX .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-FaK1JrCznY00BREX .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-FaK1JrCznY00BREX .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-FaK1JrCznY00BREX .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-FaK1JrCznY00BREX .actorPopupMenu{position:absolute;}#mermaid-svg-FaK1JrCznY00BREX .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));}#mermaid-svg-FaK1JrCznY00BREX .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-FaK1JrCznY00BREX .actor-man circle,#mermaid-svg-FaK1JrCznY00BREX line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-FaK1JrCznY00BREX :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} min.insync.replicas=2 所以2个ISR确认就够了 发送消息 写入本地 同步 同步 同步(慢,可能超时) ack ack 返回成功(所有ISR已确认)
3.3 作用三:保证数据一致性
java
// 结合min.insync.replicas保证最小同步数
// Topic配置
bin/kafka-topics.sh \
--bootstrap-server localhost:9092 \
--alter \
--topic my-topic \
--config min.insync.replicas=2
// Producer配置
props.put("acks", "all"); // 要求所有ISR确认
// 当ISR数量小于min.insync.replicas时
// 比如:副本数=3,ISR=[0,1](2个),min.insync.replicas=2 ✅ 可用
// 如果ISR=[0](1个),min.insync.replicas=2 ❌ 不可用,Producer会收到异常
四、AR与ISR的关系图解
4.1 集合关系图
#mermaid-svg-p0w6xGqVHb9WMRDD{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;}}#mermaid-svg-p0w6xGqVHb9WMRDD .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-p0w6xGqVHb9WMRDD .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-p0w6xGqVHb9WMRDD .error-icon{fill:#552222;}#mermaid-svg-p0w6xGqVHb9WMRDD .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-p0w6xGqVHb9WMRDD .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-p0w6xGqVHb9WMRDD .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-p0w6xGqVHb9WMRDD .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-p0w6xGqVHb9WMRDD .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-p0w6xGqVHb9WMRDD .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-p0w6xGqVHb9WMRDD .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-p0w6xGqVHb9WMRDD .marker{fill:#333333;stroke:#333333;}#mermaid-svg-p0w6xGqVHb9WMRDD .marker.cross{stroke:#333333;}#mermaid-svg-p0w6xGqVHb9WMRDD svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-p0w6xGqVHb9WMRDD p{margin:0;}#mermaid-svg-p0w6xGqVHb9WMRDD .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-p0w6xGqVHb9WMRDD .cluster-label text{fill:#333;}#mermaid-svg-p0w6xGqVHb9WMRDD .cluster-label span{color:#333;}#mermaid-svg-p0w6xGqVHb9WMRDD .cluster-label span p{background-color:transparent;}#mermaid-svg-p0w6xGqVHb9WMRDD .label text,#mermaid-svg-p0w6xGqVHb9WMRDD span{fill:#333;color:#333;}#mermaid-svg-p0w6xGqVHb9WMRDD .node rect,#mermaid-svg-p0w6xGqVHb9WMRDD .node circle,#mermaid-svg-p0w6xGqVHb9WMRDD .node ellipse,#mermaid-svg-p0w6xGqVHb9WMRDD .node polygon,#mermaid-svg-p0w6xGqVHb9WMRDD .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-p0w6xGqVHb9WMRDD .rough-node .label text,#mermaid-svg-p0w6xGqVHb9WMRDD .node .label text,#mermaid-svg-p0w6xGqVHb9WMRDD .image-shape .label,#mermaid-svg-p0w6xGqVHb9WMRDD .icon-shape .label{text-anchor:middle;}#mermaid-svg-p0w6xGqVHb9WMRDD .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-p0w6xGqVHb9WMRDD .rough-node .label,#mermaid-svg-p0w6xGqVHb9WMRDD .node .label,#mermaid-svg-p0w6xGqVHb9WMRDD .image-shape .label,#mermaid-svg-p0w6xGqVHb9WMRDD .icon-shape .label{text-align:center;}#mermaid-svg-p0w6xGqVHb9WMRDD .node.clickable{cursor:pointer;}#mermaid-svg-p0w6xGqVHb9WMRDD .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-p0w6xGqVHb9WMRDD .arrowheadPath{fill:#333333;}#mermaid-svg-p0w6xGqVHb9WMRDD .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-p0w6xGqVHb9WMRDD .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-p0w6xGqVHb9WMRDD .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-p0w6xGqVHb9WMRDD .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-p0w6xGqVHb9WMRDD .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-p0w6xGqVHb9WMRDD .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-p0w6xGqVHb9WMRDD .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-p0w6xGqVHb9WMRDD .cluster text{fill:#333;}#mermaid-svg-p0w6xGqVHb9WMRDD .cluster span{color:#333;}#mermaid-svg-p0w6xGqVHb9WMRDD 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;}#mermaid-svg-p0w6xGqVHb9WMRDD .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-p0w6xGqVHb9WMRDD rect.text{fill:none;stroke-width:0;}#mermaid-svg-p0w6xGqVHb9WMRDD .icon-shape,#mermaid-svg-p0w6xGqVHb9WMRDD .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-p0w6xGqVHb9WMRDD .icon-shape p,#mermaid-svg-p0w6xGqVHb9WMRDD .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-p0w6xGqVHb9WMRDD .icon-shape rect,#mermaid-svg-p0w6xGqVHb9WMRDD .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-p0w6xGqVHb9WMRDD .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-p0w6xGqVHb9WMRDD .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-p0w6xGqVHb9WMRDD :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 副本集合关系
ISR: 同步副本集合
Leader
Follower 1
Follower 2
AR: 所有副本
Follower 3
同步落后
4.2 公式表示
AR = {Leader, Follower1, Follower2, Follower3, ...}
ISR = {Leader, Follower_i, Follower_j, ...} 其中 Follower 满足同步条件
OSR = AR - ISR # Out-of-Sync Replicas,同步落后的副本
AR = ISR ∪ OSR
ISR ∩ OSR = ∅
4.3 动态变化示例
bash
# 查看副本状态
bin/kafka-topics.sh \
--bootstrap-server localhost:9092 \
--describe \
--topic my-topic
# 输出示例
Topic: my-topic Partition: 0 Leader: 0 Replicas: 0,1,2,3,4 Isr: 0,1,2,3
# AR: [0,1,2,3,4]
# ISR: [0,1,2,3]
# OSR: [4]
五、ISR相关的关键配置
5.1 Broker级别配置
properties
# server.properties
# Follower落后多长时间被踢出ISR(默认30秒)
replica.lag.time.max.ms=30000
# 是否允许不在ISR中的副本被选举为Leader
# 如果设为true,可能会丢数据;设为false,提高数据一致性
unclean.leader.election.enable=false
# 最小同步副本数(也可在Topic级别设置)
min.insync.replicas=1
5.2 Topic级别配置
bash
# 创建Topic时设置
bin/kafka-topics.sh \
--bootstrap-server localhost:9092 \
--create \
--topic my-topic \
--partitions 3 \
--replication-factor 3 \
--config min.insync.replicas=2
# 修改现有Topic
bin/kafka-topics.sh \
--bootstrap-server localhost:9092 \
--alter \
--topic my-topic \
--config min.insync.replicas=2
5.3 Producer级别配置
java
// Producer配置影响ISR的使用
Properties props = new Properties();
// 一致性要求高的场景
props.put("acks", "all"); // 等待所有ISR确认
props.put("retries", "3"); // 重试次数
props.put("max.in.flight.requests.per.connection", "1"); // 保证顺序
// 性能优先的场景
props.put("acks", "1"); // Leader确认即可
props.put("retries", "0"); // 不重试
六、ISR的实际应用场景
6.1 场景一:Leader选举
java
// 当Leader宕机时,Kafka的选举逻辑
public class LeaderElection {
public void electNewLeader(Partition partition) {
// 1. 获取当前ISR列表
List<Replica> isr = partition.getInSyncReplicas();
// 2. 优先从ISR中选举
if (!isr.isEmpty()) {
// 选择ISR中的第一个作为新Leader
Replica newLeader = isr.get(0);
partition.setLeader(newLeader);
} else {
// 如果ISR为空,根据配置决定
if (uncleanLeaderElectionEnabled) {
// 允许从OSR中选举(可能丢数据)
Replica newLeader = chooseFromOSR();
partition.setLeader(newLeader);
} else {
// 不可用
throw new NoReplicaAvailableException();
}
}
}
}
6.2 场景二:生产者的可靠性保证
java
// 需要高可靠性的生产者配置
public class ReliableProducer {
public static void main(String[] args) {
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("acks", "all");
props.put("retries", Integer.MAX_VALUE);
props.put("max.in.flight.requests.per.connection", "1");
props.put("enable.idempotence", "true"); // 开启幂等性
// 还需要Topic配置min.insync.replicas=2
KafkaProducer<String, String> producer = new KafkaProducer<>(props);
// 发送消息
producer.send(new ProducerRecord<>("my-topic", "key", "value"),
(metadata, exception) -> {
if (exception != null) {
// 如果ISR不足,会收到NotEnoughReplicasException
handleException(exception);
}
});
}
}
6.3 场景三:监控ISR变化
bash
# 监控ISR收缩和扩张
bin/kafka-topics.sh \
--bootstrap-server localhost:9092 \
--describe \
--topic my-topic \
--under-replicated-partitions
# 查看所有未完全同步的分区
bin/kafka-topics.sh \
--bootstrap-server localhost:9092 \
--describe \
--under-replicated-partitions
七、面试高频问题
Q1:ISR和AR分别代表什么?
答:
- AR:分区的所有副本集合(包括Leader和所有Follower)
- ISR:与Leader保持同步的副本集合(包括Leader和同步的Follower)
Q2:副本被踢出ISR的条件是什么?
答 :Follower超过replica.lag.time.max.ms(默认30秒)没有向Leader拉取消息,就会被踢出ISR。这表示该Follower已经落后太多,不能保证数据一致性。
Q3:为什么Leader选举只能从ISR中选?
答:因为ISR中的副本数据与Leader基本一致,选它们做Leader不会丢失数据。如果从OSR中选举,选出的Leader可能缺少大量数据,导致数据不一致。
Q4:min.insync.replicas的作用是什么?
答 :当Producer设置acks=all时,消息必须写入ISR中至少min.insync.replicas个副本才算成功。这个参数保证了最小的数据冗余度,防止在ISR数量不足时仍然写入成功导致数据风险。
Q5:什么是unclean leader election?有什么风险?
答 :当ISR为空时,是否允许从OSR(不同步副本)中选举Leader。如果允许(unclean.leader.election.enable=true),可能会选出数据落后的副本作为Leader,导致数据丢失。这是用一致性换可用性。
八、总结
8.1 核心概念关系
#mermaid-svg-9qOCHhGtsJMXVbYQ{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;}}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-9qOCHhGtsJMXVbYQ .error-icon{fill:#552222;}#mermaid-svg-9qOCHhGtsJMXVbYQ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-9qOCHhGtsJMXVbYQ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-9qOCHhGtsJMXVbYQ .marker.cross{stroke:#333333;}#mermaid-svg-9qOCHhGtsJMXVbYQ svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-9qOCHhGtsJMXVbYQ p{margin:0;}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge{stroke-width:3;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section--1 rect,#mermaid-svg-9qOCHhGtsJMXVbYQ .section--1 path,#mermaid-svg-9qOCHhGtsJMXVbYQ .section--1 circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .section--1 polygon,#mermaid-svg-9qOCHhGtsJMXVbYQ .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .section--1 text{fill:#ffffff;}#mermaid-svg-9qOCHhGtsJMXVbYQ .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge-depth--1{stroke-width:17;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:lightgray;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:#efefef;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-0 rect,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-0 path,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-0 circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-0 polygon,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-0 text{fill:black;}#mermaid-svg-9qOCHhGtsJMXVbYQ .node-icon-0{font-size:40px;color:black;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge-depth-0{stroke-width:14;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:lightgray;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:#efefef;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-1 rect,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-1 path,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-1 circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-1 polygon,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-1 text{fill:black;}#mermaid-svg-9qOCHhGtsJMXVbYQ .node-icon-1{font-size:40px;color:black;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge-depth-1{stroke-width:11;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:lightgray;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:#efefef;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-2 rect,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-2 path,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-2 circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-2 polygon,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-2 text{fill:#ffffff;}#mermaid-svg-9qOCHhGtsJMXVbYQ .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge-depth-2{stroke-width:8;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:lightgray;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:#efefef;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-3 rect,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-3 path,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-3 circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-3 polygon,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-3 text{fill:black;}#mermaid-svg-9qOCHhGtsJMXVbYQ .node-icon-3{font-size:40px;color:black;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge-depth-3{stroke-width:5;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:lightgray;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:#efefef;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-4 rect,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-4 path,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-4 circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-4 polygon,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-4 text{fill:black;}#mermaid-svg-9qOCHhGtsJMXVbYQ .node-icon-4{font-size:40px;color:black;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge-depth-4{stroke-width:2;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:lightgray;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:#efefef;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-5 rect,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-5 path,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-5 circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-5 polygon,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-5 text{fill:black;}#mermaid-svg-9qOCHhGtsJMXVbYQ .node-icon-5{font-size:40px;color:black;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge-depth-5{stroke-width:-1;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:lightgray;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:#efefef;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-6 rect,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-6 path,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-6 circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-6 polygon,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-6 text{fill:black;}#mermaid-svg-9qOCHhGtsJMXVbYQ .node-icon-6{font-size:40px;color:black;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge-depth-6{stroke-width:-4;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:lightgray;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:#efefef;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-7 rect,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-7 path,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-7 circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-7 polygon,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-7 text{fill:black;}#mermaid-svg-9qOCHhGtsJMXVbYQ .node-icon-7{font-size:40px;color:black;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge-depth-7{stroke-width:-7;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:lightgray;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:#efefef;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-8 rect,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-8 path,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-8 circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-8 polygon,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-8 text{fill:black;}#mermaid-svg-9qOCHhGtsJMXVbYQ .node-icon-8{font-size:40px;color:black;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge-depth-8{stroke-width:-10;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:lightgray;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:#efefef;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-9 rect,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-9 path,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-9 circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-9 polygon,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-9 text{fill:black;}#mermaid-svg-9qOCHhGtsJMXVbYQ .node-icon-9{font-size:40px;color:black;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge-depth-9{stroke-width:-13;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:lightgray;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:#efefef;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-10 rect,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-10 path,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-10 circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-10 polygon,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-10 text{fill:black;}#mermaid-svg-9qOCHhGtsJMXVbYQ .node-icon-10{font-size:40px;color:black;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge-depth-10{stroke-width:-16;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:lightgray;}#mermaid-svg-9qOCHhGtsJMXVbYQ .disabled text{fill:#efefef;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-root rect,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-root path,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-root circle,#mermaid-svg-9qOCHhGtsJMXVbYQ .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-root text{fill:#ffffff;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-root span{color:#ffffff;}#mermaid-svg-9qOCHhGtsJMXVbYQ .section-2 span{color:#ffffff;}#mermaid-svg-9qOCHhGtsJMXVbYQ .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-9qOCHhGtsJMXVbYQ .edge{fill:none;}#mermaid-svg-9qOCHhGtsJMXVbYQ .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-9qOCHhGtsJMXVbYQ :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} root(Kafka副本集合)
AR(所有副本)
Leader(读写)
Follower(同步)
ISR(同步副本)
数据一致
可被选举
OSR(落后副本)
数据落后
不能选举
8.2 关键公式
AR = ISR ∪ OSR
ISR ⊆ AR
Leader ∈ ISR
8.3 一句话总结
ISR是Kafka保证数据一致性的核心机制,它圈定了"可用且可靠"的副本集合,是Leader选举和数据确认的基准。
掌握了ISR和AR的概念,你就理解了Kafka高可用架构的精髓!
思考题:在Kafka 2.8+中引入的Raft-based KRaft模式,ISR的概念有什么变化?和ZooKeeper模式下的ISR有什么异同?欢迎在评论区讨论!

|---------------------------|
| 🌺The End🌺点点关注,收藏不迷路🌺 |