- Updated service template structure in `ARCHITECTURE.md` and `README.md` for clarity and usability. - Enhanced bilingual documentation across skills, increasing the number of available skills from 15 to 25. - Added new sections on event-driven architecture, inter-service communication, and performance optimization. - Improved formatting and removed outdated references to streamline the documentation experience.
85 lines
3.7 KiB
Markdown
85 lines
3.7 KiB
Markdown
# Patterns Nhất Quán Dữ Liệu (Data Consistency Patterns)
|
|
|
|
Data consistency patterns for distributed microservices including Saga patterns, distributed transactions, eventual consistency, compensation, and idempotency. Use when handling distributed transactions, implementing eventual consistency, or managing data synchronization across services.
|
|
> Các patterns nhất quán dữ liệu cho distributed microservices bao gồm Saga patterns, distributed transactions, eventual consistency, compensation, và idempotency. Sử dụng khi xử lý distributed transactions, implement eventual consistency, hoặc quản lý đồng bộ dữ liệu giữa các services.
|
|
|
|
## Tổng Quan
|
|
|
|
Data consistency in distributed systems requires different approaches than traditional ACID transactions. This skill covers Saga patterns for distributed transactions, idempotency for retries, optimistic locking for conflicts, and eventual consistency strategies.
|
|
|
|
Nhất quán dữ liệu trong hệ thống phân tán yêu cầu các cách tiếp cận khác với ACID transactions truyền thống. Skill này bao gồm Saga patterns cho distributed transactions, idempotency cho retries, optimistic locking cho conflicts, và các chiến lược eventual consistency.
|
|
|
|
## Khi Nào Sử Dụng
|
|
|
|
Use this skill when:
|
|
- Implementing distributed transactions across multiple services
|
|
- Handling eventual consistency in microservices
|
|
- Implementing Saga patterns for distributed workflows
|
|
- Designing compensation strategies
|
|
|
|
Sử dụng skill này khi:
|
|
- Implement distributed transactions qua nhiều services
|
|
- Xử lý eventual consistency trong microservices
|
|
- Implement Saga patterns cho distributed workflows
|
|
- Thiết kế chiến lược compensation
|
|
|
|
## Khái Niệm Chính
|
|
|
|
### ACID vs BASE
|
|
|
|
**ACID (Truyền Thống)**: Atomicity, Consistency, Isolation, Durability
|
|
|
|
**BASE (Phân Tán)**: Basic Availability, Soft state, Eventual consistency
|
|
|
|
### Các Mô Hình Nhất Quán
|
|
|
|
- **Strong Consistency**: Tất cả nodes thấy cùng dữ liệu cùng lúc
|
|
- **Eventual Consistency**: Hệ thống trở nên nhất quán theo thời gian
|
|
- **Weak Consistency**: Không đảm bảo về thời điểm nhất quán
|
|
|
|
## Các Patterns Chính
|
|
|
|
### Saga Orchestrator Pattern
|
|
|
|
```typescript
|
|
// EN: Centralized orchestrator coordinates steps
|
|
// VI: Orchestrator tập trung điều phối các bước
|
|
const saga = new SagaOrchestrator();
|
|
await saga.execute({
|
|
sagaId: 'saga_123',
|
|
steps: [
|
|
{ name: 'create-order', execute: createOrder, compensate: cancelOrder },
|
|
{ name: 'reserve-inventory', execute: reserveInventory, compensate: releaseInventory },
|
|
],
|
|
});
|
|
```
|
|
|
|
### Idempotency / Idempotency
|
|
|
|
```typescript
|
|
// EN: Execute with idempotency check
|
|
// VI: Thực thi với idempotency check
|
|
await idempotencyHandler.execute(
|
|
idempotencyKey,
|
|
async () => await userService.create(data)
|
|
);
|
|
```
|
|
|
|
## Best Practices / Thực Hành Tốt
|
|
|
|
1. **Design Compensations**: Mỗi step cần compensation / Every step needs compensation
|
|
2. **Idempotent Steps**: Làm cho steps idempotent cho retries / Make steps idempotent for retries
|
|
3. **Conflict Resolution**: Định nghĩa chiến lược giải quyết / Define resolution strategies
|
|
4. **Monitoring**: Theo dõi saga execution / Track saga execution
|
|
|
|
## Skills Liên Quan
|
|
|
|
- [Event-Driven Architecture](./event-driven-architecture.md) - Event patterns / Các patterns event
|
|
- [Error Handling Patterns](./error-handling-patterns.md) - Error handling / Xử lý lỗi
|
|
- [Database & Prisma](./database-prisma.md) - Database patterns / Các patterns database
|
|
|
|
## Tài Nguyên
|
|
|
|
- [Saga Pattern](https://microservices.io/patterns/data/saga.html) - Saga pattern overview
|
|
- Skill Source: `.cursor/skills/data-consistency-patterns/SKILL.md` - Source file đầy đủ
|