Lesson 69 — Microservice Patterns

Ba mô phỏng tương tác cho các pattern cốt lõi: (1) tách monolith thành microservices theo business capability, (2) API Gateway gom nhiều service call thành 1 response (fan-out song song), (3) Strangler fig migrate dần monolith → service mới, route được rollback.

1. Monolith → Microservices

Monolith e-commerce gom mọi module trong 1 process, 1 database. Bấm "Tách" để decompose theo business capability — mỗi capability thành 1 service own database riêng.

🏢 Monolith (1 process · 1 DB)
Catalog
Cart
Order
Payment
Inventory
Notification

Trạng thái: monolith — mọi module dùng chung 1 database app_db.

2. API Gateway — Aggregation

Màn "chi tiết đơn hàng" cần dữ liệu từ 3 service. Client gửi 1 request; gateway fan-out song song tới Order/User/Shipping rồi gom thành 1 response. So sánh thời gian tuần tự (115ms) vs song song.

Client Gateway Order 40ms User 35ms Shipping 40ms
Đồng hồ: 0 ms
Bấm nút để chạy. Response gom 3 service sẽ hiện ở đây.

Song song: tổng ≈ chậm nhất (max ~40ms). Tuần tự: cộng dồn 40+35+40 = 115ms. Client luôn chỉ thấy 1 request/1 response.

3. Strangler Fig — Migration dần

Đặt router trước monolith. Migrate từng prefix sang service mới; phần chưa migrate vẫn về Monolith. Có thể rollback 1 prefix về Monolith ngay khi service mới lỗi.

Giai đoạn 0 — chưa migrate gì, mọi route → Monolith
🔀 Strangler Router (đứng trước Monolith)

Mỗi bước nhỏ và rollback được → an toàn hơn big-bang rewrite. Khi Monolith không còn route nào → tắt nó.