Cả hai đều là hệ thống messaging (hàng đợi tin nhắn), nhưng kiến trúc, mô hình và khả năng mở rộng khác biệt lớn.
Tóm tắt nhanh
- JMS: API tiêu chuẩn Java (javax.jms / jakarta.jms) cho messaging truyền thống. Triển khai bởi broker: ActiveMQ, RabbitMQ, IBM MQ, WebLogic...
- Kafka: Platform event streaming phân tán, high-throughput, log-based. Không tuân thủ JMS spec.
Bảng so sánh chi tiết
| Tiêu chí | Apache Kafka | JMS |
|---|---|---|
| Kiến trúc | Distributed commit log (partitioned, replicated). Broker = cluster. | Broker-based (centralized queue/topic). Thường single broker hoặc HA pair. |
| Mô hình messaging | Publish-Subscribe + Consumer Groups (hybrid queue/topic). | Point-to-Point (Queue) & Pub/Sub (Topic) – tách biệt. |
| Consumer model | Pull: Consumer poll dữ liệu từ broker. | Push: Broker push đến consumer (MessageListener). |
| Message Retention | Policy-based: thời gian (7 ngày), dung lượng, compacted. Persistent mặc định. | Ack-based: xóa sau khi consumer ack. Không lưu lâu (trừ config đặc biệt). |
| Ordering | Đảm bảo ordering trong partition. Key quyết định partition. | Không đảm bảo ordering toàn cục (trừ single consumer + single session). |
| Scalability | Horizontal scaling tự nhiên: thêm partition, thêm broker, rebalance auto. | Khó scale horizontally. Queue: competing consumers. Topic: broadcast. |
| Rebalancing | Tự động khi consumer join/leave group (Consumer Group Protocol). | Không có auto-rebalance tiêu chuẩn. |
| Throughput | Rất cao (millions msg/sec) nhờ sequential disk I/O, zero-copy, batching. | Thấp hơn (thousands-hundreds thousands msg/sec). |
| Replay/Reprocessing | Dễ dàng: reset offset → đọc lại từ đầu. | Khó: message đã ack bị xóa. Cần dead-letter queue hoặc backup. |
| Protocol | TCP custom protocol (binary, efficient). | JMS API (vendor-specific wire protocol: OpenWire, AMQP, MQTT...). |
| Use case điển hình | Event streaming, log aggregation, metrics, audit trail, CDC, real-time analytics. | Enterprise integration, task queue, command messaging, legacy systems. |
Mô hình Consumer Group của Kafka (Điểm khác biệt then chốt)
Kafka kết hợp ưu điểm Queue và Topic:
- Một topic có nhiều partition.
- Một Consumer Group chứa nhiều consumer.
- Mỗi partition chỉ được assign cho một consumer trong group → load balancing như Queue.
- Nhiều Consumer Group khác nhau đọc cùng topic → broadcast như Topic.
- Rebalance tự động khi scale consumer.
Khi nào chọn Kafka?
- High throughput, low latency, big data.
- Cần replay, event sourcing, audit log.
- Microservices event-driven architecture.
- Log aggregation, metrics, clickstream.
Khi nào chọn JMS?
- Enterprise legacy integration (ESB, mainframe).
- Cần JTA/XA transaction (distributed transaction).
- Đơn giản: task queue, command pattern, low volume.
- Team đã am hiểu JMS, không muốn vận hành cluster Kafka.
Lưu ý: Spring Boot / Jakarta EE
- Spring Boot hỗ trợ cả hai:
spring-kafkavàspring-jms(với ActiveMQ/RabbitMQ). - Jakarta EE 9+ dùng namespace
jakarta.jms(thayjavax.jms).