在 Ubuntu 上优化 Kafka 延迟(latency),通常要从 系统层、Kafka 配置、生产/消费端、磁盘和网络 几个方面入手。下面按“最常见 → 进阶”给你一个实用清单。
先分清是:
常用工具:
# 消费滞后
kafka-consumer-groups.sh --bootstrap-server localhost:9092 \
--describe --group your-group
# 生产耗时
producer 加 metrics / 日志
# server.properties
log.flush.interval.messages=10000
log.flush.interval.ms=1000
⚠️ 默认 Kafka 依赖 OS page cache,不要频繁 flush,否则延迟飙升。
如果你追求低延迟而非绝对持久:
log.flush.scheduler.interval.ms=1000
acks=1 # producer 端
min.insync.replicas=1
acks=all 延迟明显更高acks=1num.network.threads=8
num.io.threads=16
(根据 CPU 核数调整)
linger.ms=0
batch.size=16384
linger.ms>0 会主动等批,提高吞吐但增加延迟compression.type=lz4
producer.send(record); // 不 await
fetch.min.bytes=1
fetch.max.wait.ms=0
fetch.max.wait.ms=500,会等批✅ 用 SSD / NVMe ❌ 避免 HDD + 高吞吐
挂载选项:
noatime,nodiratime
ulimit -n 100000
/etc/security/limits.conf
* soft nofile 100000
* hard nofile 100000
sysctl -w net.core.rmem_max=16777216
sysctl -w net.core.wmem_max=16777216
sysctl -w net.ipv4.tcp_rmem="4096 87380 16777216"
sysctl -w net.ipv4.tcp_wmem="4096 65536 16777216"
Producer
acks=1
linger.ms=0
compression.type=lz4
batch.size=16384
Broker
num.network.threads=8
num.io.threads=16
log.flush.interval.ms=1000
Consumer
fetch.max.wait.ms=0
fetch.min.bytes=1
如果你愿意,可以告诉我:
我可以给你一套针对你场景的精确参数。