Về trang blog
Series: Kafka từ A-Z · Bài 5/6
Partitions & Replication
Bí mật sau Durability và Scalability của Kafka

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
Rule of thumb
Bắt đầu với 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 ítPartition nhiều
Throughput thấp hơnThroughput cao hơn
Ordering dễ đảm bảo hơnOrdering chỉ trong partition
Ít resource hơn (file, memory)Nhiều resource, leader election chậm hơn
Khó scale horizontal consumerScale consumer dễ dàng hơn
Rebalancing nhanh hơnRebalancing chậm hơn khi nhiều partition
Không thể giảm partition
Kafka chỉ cho phép tăng số partition, không thể giảm (trừ khi xóa và tạo lại topic). Lên kế hoạch trước, bắt đầu với số hợp lý.

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.

Topic: order-events · Partition 0 · Replication Factor = 3
Broker 1 LEADER
P0 (leader)
P1 (follower)
←sync→
Broker 2 ISR
P0 (follower)
P2 (leader)
←sync→
Broker 3 ISR
P0 (follower)
P1 (leader)
Nếu Broker 2 chậm (lag quá lớn): bị kick khỏi ISR → chỉ còn ISR={Broker 1, Broker 3}

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.factormin.insync.replicasTolerate failGhi chú
110 brokerDev/test only
211 brokerHA minimal — vẫn có thể mất data
321 brokerProduction standard
330 brokerKhông tolerate fail, quá strict
Production standard
replication.factor=3 + min.insync.replicas=2 + acks=all là bộ ba đảm bảo durability tốt nhất trong thực tế. Cluster 3 broker có thể mất 1 broker bất kỳ mà không mất data và không ngừng nhận write.

Leader election

Khi leader broker crash:

  1. Kafka Controller (broker đặc biệt quản lý cluster) phát hiện broker down
  2. Chọn một replica trong ISR làm leader mới
  3. Cập nhật metadata, các producer/consumer redirect đến leader mới
  4. 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.

bash — tạo topic với log compaction
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 countTối thiểu 3 cho production. Scale theo throughput và storage.
Partition per brokerKhông quá 4000 partition per broker (Kafka guideline). Tổng cluster nên < 200K partition.
DiskSSD cho low-latency. Tính: throughput × retention_days × replication_factor × 1.2 (buffer)
RAMKafka dùng OS page cache nhiều — RAM càng nhiều càng ít disk I/O. 64GB+ cho production.
JVM heap4-8GB cho Kafka broker JVM. Phần còn lại để cho OS page cache.
Network10Gbps cho cluster lớn. Replications và client traffic cộng lại có thể rất cao.