Kafka broker 写消息的过程

text 复制代码
Producer → Kafka Broker → Replication → Consumer
               |
          Partition chosen (by key or round-robin)
               |
          Message appended to end of log (commit log)

上面的流程是kafka 写操作的大体流程。

kafka 不会特意保留message 在内存中,而是直接写入了disk。

那么消费的时候,如果是最近 produced 的message,即使写入了disk,还是会保留在OS的page cache中。 OS page cache 即使是flush 到了disk,也不会被立刻清除,这个是OS 自己的机制。 所以consumer 读最近生产的消息的时候,还是可以从page cache中读取出来的,而不用从disk 中读取。

  1. Kafka Broker Receives the Message

    决定partition

  2. Message is Appended to a Partition Log

    The selected broker appends the message to the end of the partition log (a sequential write).

    Kafka writes messages to disk efficiently using write-ahead logs (WAL).

  3. Replication (for Fault Tolerance)

    Kafka waits for acknowledgments based on the producer's acks setting:

    acks=0: Producer doesn't wait.

    acks=1: Wait for leader only.

    acks=all: Wait for all replicas to acknowledge.

  4. Message is Made Available to Consumers

    Once the acks is fulfilled, it becomes available for consumers.

    Consumers read sequentially using offsets.

    Kafka keeps messages for a retention period (e.g., 7 days), regardless of whether they've been consumed.

相关推荐
Mr.朱鹏9 小时前
SQL深度分页问题案例实战
java·数据库·spring boot·sql·spring·spring cloud·kafka
脸大是真的好~11 小时前
分布式锁-基于redis实现分布式锁(不推荐)- 改进利用LUA脚本(不推荐)前面都是原理 - Redisson分布式锁
redis·分布式·lua
liuniansilence12 小时前
🚀 高并发场景下的救星:BullMQ如何实现智能流量削峰填谷
前端·分布式·消息队列
Wang's Blog15 小时前
RabbitMQ: 实现高效消息监听之从基础到自动配置
分布式·rabbitmq
Wang's Blog16 小时前
RabbitMQ: 高级特性详解之消息返回机制与消费端确认机制
分布式·rabbitmq
Wang's Blog16 小时前
RabbitMQ: 使用MessageConverter高效处理消息
分布式·rabbitmq
‘胶己人’18 小时前
redis分布式锁
数据库·redis·分布式
山沐与山18 小时前
【MQ】Kafka与RocketMQ深度对比
分布式·kafka·rocketmq
武子康19 小时前
Java-203 RabbitMQ 生产者/消费者工作流程拆解:Connection/Channel、默认交换器、ACK
java·分布式·消息队列·rabbitmq·erlang·ruby·java-rabbitmq