# Kafka 101

> Work in progress.

## 基本概念

### 消费者与主题分区

消费者订阅主题进行消费，而主题是一个逻辑上的概念，它还可以细分为多个分区，一个分区只属于单个主题​。

同一主题下的不同分区包含的消息是不同的，分区在存储层面可以看作一个可追加的日志（Log）文件，消息在被追加到分区日志文件的时候都会分配一个特定的偏移量（offset）​。offset 是消息在分区中的唯一标识，Kafka 通过它来保证消息在分区内的顺序性，同时 Kafka 保证的是分区有序而不是主题有序。

分区也解决了单个 Topic 对应一个持久化文件可能导致的 IO 瓶颈。

在消费者拉取消息时，只能消费高水位之前的消息，也就是只能消费已提交的消息。分区 ISR（同步副本） 集合中的每个副本都会维护自身的 LEO，而 ISR 集合中最小的 LEO（LogEndOffset，当前日志中最后一条消息的 offset + 1，也就是该副本当前复制到的位置） 即为分区的高水位。

> Kafka 的复制机制基于动态的 in-sync replicas（ISR）集合，leader 只需等待 ISR 中所有副本确认而非全部副本，从而在避免完全同步复制的高延迟的同时，又通过强制 ISR 确认提供了比单纯异步复制更强的数据可靠性。

### 安装与配置

仓库地址：[L2ncE/Kafka101](https://github.com/L2ncE/kafka101)

克隆仓库后进入到 [docker-compose 文件](https://github.com/L2ncE/kafka101/blob/main/docker-compose.yml) 目录运行 `docker-compose up`  或直接运行 `./quick-start.sh`。

> ⚠️ **注意**：自行下载 Docker 以及 docker-compose 工具。 📌 部署版本为 Kafka2.8.1，该版本使用 KRaft 无需部署 Zookeeper。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gists.lanlance.cn/cssys/kafka-101.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
