Part 09 · Distributed Systems

CQRS và Event Sourcing khi có requirement

CQRS có thể chỉ tách read/write model; Event Sourcing lưu events làm source of truth. Hai pattern không bắt buộc đi cùng.

CQRS

Hữu ích khi read/write model, scale, security hoặc workflow khác biệt đáng kể. Chi phí: projection lag, duplicate model, sync/rebuild và operational tooling. CRUD thông thường không cần hai stack.

Event Sourcing

Aggregate state fold từ immutable event stream với expected version để optimistic concurrency. Event là lịch sử lâu dài: không sửa/xóa tùy tiện; schema evolution/upcaster, snapshot và privacy deletion cần design từ đầu.

Projection lifecycle

Consumer idempotent cập nhật read model, checkpoint offset/version, rebuild từ event history và swap projection an toàn. Poison event hoặc code mới không đọc event cũ có thể chặn rebuild.

Failure modes

CQRS Pattern · Event Sourcing Pattern