© 2026 Laravel

CQRS & Event Sourcing – Khi nào nên dùng và khi nào KHÔNG

2 phút đọc 13 lượt xem
#laravel #cqrs #event-sourcing #architecture #system-design

Đây là 2 khái niệm:

Bị lạm dụng nhiều nhất trong system design

Nhưng nếu hiểu đúng:

  • Scale cực mạnh
  • Flexible

Nếu hiểu sai:

  • Complexity tăng x10 💀

1. Problem (Production thật)

System:

  • Read nhiều (dashboard)
  • Write nhiều (orders)

#❌ Issue

  • Query chậm
  • DB overload
  • Conflict read/write

2. CQRS là gì?

Command Query Responsibility Segregation

#Ý tưởng

  • Command → write
  • Query → read

#❌ Truyền thống

User::find(1);
User::update([...]);

Chung model

#✅ CQRS

// Write model
CreateUserCommand

// Read model
UserView

#Insight

  • Tách read/write
  • Optimize riêng

3. Lợi ích CQRS

  • Scale read riêng
  • Optimize query
  • Clear responsibility

4. Trade-off CQRS

  • Code phức tạp hơn
  • Data duplication
  • Eventual consistency

5. Event Sourcing là gì?

Lưu toàn bộ event thay vì state

#❌ Traditional

balance = 1000;

#✅ Event Sourcing

+1000 (deposit)
-200 (withdraw)

#State = replay events

6. Lợi ích Event Sourcing

  • Audit log đầy đủ
  • Debug dễ
  • Time travel

7. Trade-off Event Sourcing

  • Khó implement
  • Storage lớn
  • Replay cost

8. CQRS + Event Sourcing (kết hợp)

#Flow

Command → Event → Update Read Model

#Example

CreateOrderCommand
  OrderCreatedEvent
  UpdateOrderView

9. Real Case (Production)

#Banking system

  • Transaction log
  • Audit

#E-commerce

  • Order history
  • Analytics

10. Khi nào nên dùng?

#✅ Nên

  • System lớn
  • Audit quan trọng
  • Complex domain

#❌ Không nên

  • CRUD đơn giản
  • Startup nhỏ

11. Real Bug 💀

#Scenario

  • Event fail
  • Read model không update

#Issue

  • Data mismatch

#Fix

  • Retry event
  • Rebuild read model

12. Anti-pattern

#❌ Dùng CQRS cho CRUD đơn giản

#❌ Event quá nhiều

#❌ Không handle consistency

13. Tips & Tricks

  • Bắt đầu từ small module
  • Log event
  • Monitor system

14. Mindset Senior

Junior:

“CQRS là best practice”

Senior:

“CQRS chỉ dùng khi cần”

15. Interview Questions

1. CQRS là gì?

Tách read/write

2. Event sourcing là gì?

Lưu event thay vì state

3. Khi nào nên dùng CQRS?

System lớn, read/write khác biệt

4. Trade-off là gì?

Complexity + consistency

5. Làm sao fix data mismatch?

Rebuild read model

Kết luận

CQRS & Event Sourcing rất mạnh.

Nhưng không dành cho mọi hệ thống

Đây là level của architect thực thụ.

#CTA

Bài tiếp theo: System Design Case Study – Thiết kế hệ thống thực tế (end-to-end)

#Internal Link

  • Microservices
  • Event-Driven Architecture