Apache Kafka là gì?
Apache Kafka là một nền tảng streaming phân tán mã nguồn mở, được phát triển bởi Apache Software Foundation. Kafka cho phép bạn xây dựng các ứng dụng xử lý dữ liệu theo thời gian thực (real-time) với khả năng chịu tải cao, độ trễ thấp và độ tin cậy lớn. Kafka ban đầu được phát triển tại LinkedIn trước khi trở thành dự án Apache top-level.
Kiến trúc của Apache Kafka
Topic và Partition
Topic là đơn vị tổ chức dữ liệu trong Kafka. Mỗi topic được chia thành nhiều partition, cho phép parallel processing và horizontal scaling. Dữ liệu trong partition được sắp xếp theo thứ tự và giữ lại trong một khoảng thời gian configurable.
Producer và Consumer
Producer gửi messages đến Kafka topic. Consumer đọc messages từ topic. Kafka hỗ trợ cả consumer model đồng bộ và bất đồng bộ, cũng như consumer groups để load balancing.
Broker và Cluster
Kafka broker là server chạy Kafka, lưu trữ và phục vụ messages. Một Kafka cluster bao gồm nhiều brokers, đảm bảo high availability thông qua replication factor.
Zookeeper/KRaft
Kafka sử dụng Zookeeper để quản lý cluster metadata và leader election. Từ phiên bản 3.3+, Kafka hỗ trợ KRaft mode cho phép vận hành mà không cần Zookeeper.
Use Cases phổ biến của Kafka
- Event Streaming: Thu thập và xử lý events từ ứng dụng theo thời gian thực.
- Message Queue: Thay thế traditional message brokers như RabbitMQ.
- Log Aggregation: Thu thập logs từ nhiều services để phân tích và giám sát.
- Change Data Capture (CDC): Theo dõi thay đổi trong database và streaming ra Kafka.
- Data Pipelines: Xây dựng data pipelines để di chuyển dữ liệu giữa các hệ thống.
Cài đặt Kafka trên Ubuntu/Debian
# Cài đặt Java JDK (yêu cầu) sudo apt update sudo apt install openjdk-11-jdk -y # Tải Kafka wget https://archive.apache.org/dist/kafka/3.6.1/kafka_2.13-3.6.1.tgz tar -xzf kafka_2.13-3.6.1.tgz mv kafka_2.13-3.6.1 /opt/kafka # Start Zookeeper cd /opt/kafka bin/zookeeper-server-start.sh config/zookeeper.properties & # Start Kafka Broker (terminal mới) bin/kafka-server-start.sh config/server.properties
Các lệnh cơ bản với Kafka
# Tạo topic mới bin/kafka-topics.sh --create --topic my-topic --bootstrap-server localhost:9092 --partitions 3 --replication-factor 1 # Liệt kê topics bin/kafka-topics.sh --list --bootstrap-server localhost:9092 # Mô tả topic bin/kafka-topics.sh --describe --topic my-topic --bootstrap-server localhost:9092 # Gửi message (Producer) bin/kafka-console-producer.sh --topic my-topic --bootstrap-server localhost:9092 # Đọc message (Consumer) bin/kafka-console-consumer.sh --topic my-topic --bootstrap-server localhost:9092 --from-beginning
So sánh Kafka với RabbitMQ
| Tiêu chí | Kafka | RabbitMQ |
|---|---|---|
| Message Retention | Theo thời gian/configurable | Xóa sau khi consume |
| Throughput | Rất cao (triệu messages/s) | Thấp hơn |
| Use Case | Event streaming, log aggregation | Task queue, async processing |
| Protocol | TCP binary (custom) | AMQP, MQTT, STOMP |
| Ordering | Trong partition | Per queue |
Kafka trong Docker
# docker-compose.yml cho Kafka
version: '3'
services:
zookeeper:
image: confluentinc/cp-zookeeper:7.5.0
environment:
ZOOKEEPER_CLIENT_PORT: 2181
kafka:
image: confluentinc/cp-kafka:7.5.0
depends_on:
- zookeeper
ports:
- "9092:9092"
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
FAQ – Câu hỏi thường gặp
- Kafka có phải là Database không? Không, Kafka là message broker, nhưng có thể dùng để lưu trữ tạm thời với configurable retention.
- Làm sao đảm bảo message không bị mất? Cấu hình
acks=allvàretriestrong producer, replication factor >= 3. - Sự khác biệt giữa Kafka và Kafka Streams? Kafka là broker, Kafka Streams là thư viện xử lý stream trong ứng dụng.
- Kafka có phù hợp cho microservices không? Rất phù hợp, Kafka thường được dùng làm backbone cho event-driven microservices.
- Làm thế nào để monitor Kafka? Sử dụng JMX metrics, Prometheus với kafka-exporter, và Grafana dashboards.