Partition count — bao nhiêu là đủ?
Partition là đơn vị song song hóa. Số partition quyết định:
- Max consumer parallelism: không thể có nhiều consumer active hơn số partition
- Write throughput: mỗi partition có thể nhận vài trăm MB/s, nhiều partition = nhiều throughput
- Resource consumption: mỗi partition = một file trên disk, memory cho buffer, open file descriptor
max(throughput_MB/s / 10, max_consumers). Ví dụ:
throughput 100 MB/s và cần 20 consumer → partition = max(10, 20) = 20 partitions.
Không cần partition nhiều hơn số consumer thực tế.
| Partition ít | Partition nhiều |
|---|---|
| Throughput thấp hơn | Throughput cao hơn |
| Ordering dễ đảm bảo hơn | Ordering chỉ trong partition |
| Ít resource hơn (file, memory) | Nhiều resource, leader election chậm hơn |
| Khó scale horizontal consumer | Scale consumer dễ dàng hơn |
| Rebalancing nhanh hơn | Rebalancing chậm hơn khi nhiều partition |
Replication — Leader, Follower, ISR
Mỗi partition có một leader và nhiều follower (replica). Producer/consumer luôn làm việc với leader. Follower đồng bộ dữ liệu từ leader liên tục.
P1 (follower)
P2 (leader)
P1 (leader)
ISR (In-Sync Replicas): tập hợp replica đang sync theo kịp leader.
Một replica bị remove khỏi ISR nếu lag quá lớn (vượt replica.lag.time.max.ms, mặc định 30s).
min.insync.replicas
Khi dùng acks=all, broker chờ đủ min.insync.replicas replica trong ISR ghi xong mới ack.
Đây là cơ chế đảm bảo data không bị mất khi có broker fail.
| replication.factor | min.insync.replicas | Tolerate fail | Ghi chú |
|---|---|---|---|
| 1 | 1 | 0 broker | Dev/test only |
| 2 | 1 | 1 broker | HA minimal — vẫn có thể mất data |
| 3 | 2 | 1 broker | Production standard |
| 3 | 3 | 0 broker | Không tolerate fail, quá strict |
Leader election
Khi leader broker crash:
- Kafka Controller (broker đặc biệt quản lý cluster) phát hiện broker down
- Chọn một replica trong ISR làm leader mới
- Cập nhật metadata, các producer/consumer redirect đến leader mới
- Quá trình này mất khoảng 30 giây mặc định (tunable)
Nếu không có ISR nào còn alive, Kafka có thể chọn unclean leader election
(replica out-of-sync làm leader) — data loss nhưng cluster tiếp tục hoạt động.
Default là off (unclean.leader.election.enable=false) — nên giữ nguyên.
Retention vs Log Compaction
Kafka có hai cách quản lý vòng đời dữ liệu:
Time-based retention (mặc định)
# Giữ 7 ngày (mặc định)
retention.ms=604800000
# Giữ theo kích thước — giữ tối đa 10GB per partition
retention.bytes=10737418240
Log Compaction
Thay vì xóa theo thời gian, log compaction giữ bản ghi cuối cùng của mỗi key. Phù hợp cho use case "state store" — ví dụ: user profile, product catalog.
docker exec kafka /opt/kafka/bin/kafka-topics.sh \
--bootstrap-server localhost:9092 \
--create \
--topic user-profiles \
--partitions 3 \
--replication-factor 1 \
--config cleanup.policy=compact \
--config min.compaction.lag.ms=0
Sizing guidelines thực tế
| Yếu tố | Recommendation |
|---|---|
| Broker count | Tối thiểu 3 cho production. Scale theo throughput và storage. |
| Partition per broker | Không quá 4000 partition per broker (Kafka guideline). Tổng cluster nên < 200K partition. |
| Disk | SSD cho low-latency. Tính: throughput × retention_days × replication_factor × 1.2 (buffer) |
| RAM | Kafka dùng OS page cache nhiều — RAM càng nhiều càng ít disk I/O. 64GB+ cho production. |
| JVM heap | 4-8GB cho Kafka broker JVM. Phần còn lại để cho OS page cache. |
| Network | 10Gbps cho cluster lớn. Replications và client traffic cộng lại có thể rất cao. |
- Bài 1: Kafka là gì? Core concepts & architecture
- Bài 2: Cài đặt với Docker — Kafka UI trong 15 phút
- Bài 3: Producer deep dive — acks, batching, ordering
- Bài 4: Consumer & Consumer Groups — offset, rebalancing
- Bài 5: Partitions & Replication — durability đảm bảo thế nào
- Bài 6: Kafka trong Production — monitoring, tuning, common issues