Event sourcing lưu trữ mọi thay đổi state của domain object dưới dạng immutable event (append-only log) thay vì chỉ lưu state hiện tại.
Kafka là storage backend lý tưởng: durable, ordered, replayable.
Pattern hoạt động:
- Mọi action (OrderPlaced, PaymentProcessed, OrderShipped) được publish như event vào Kafka topic;
- Current state được derive bằng cách replay tất cả event từ đầu hoặc từ snapshot gần nhất;
- CQRS thường đi kèm: Command side (write) publish event → Kafka → Event handler rebuild read model cho Query side. Lợi ích: audit trail đầy đủ, time-travel debugging, eventual consistency tự nhiên, dễ scale read side. Nhược điểm: eventual consistency phức tạp hơn strong consistency; query current state cần rebuild từ events (giải quyết bằng snapshot và read model); schema evolution khó (events immutable nên không thể sửa); storage lớn theo thời gian
Dùng log compaction để snapshot state, giảm replay time.