Files
pos-system/docs/vi/skills/data-consistency-patterns.md
Ho Ngoc Hai 478254400a Refactor service documentation and enhance bilingual support
- 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.
2026-01-01 10:06:27 +07:00

3.7 KiB

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

// 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

// 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

Tài Nguyên

  • Saga Pattern - Saga pattern overview
  • Skill Source: .cursor/skills/data-consistency-patterns/SKILL.md - Source file đầy đủ