Files
pos-system/docs/en/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

2.8 KiB

name, description
name description
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.

Data Consistency Patterns

When to Use This Skill

Use this skill when:

  • Implementing distributed transactions across multiple services
  • Handling eventual consistency in microservices
  • Implementing Saga patterns for distributed workflows
  • Designing compensation strategies for failed transactions
  • Implementing idempotent operations
  • Managing data synchronization across services
  • Handling conflict resolution

Core Concepts

ACID vs BASE

ACID (Traditional): Atomicity, Consistency, Isolation, Durability

BASE (Distributed): Basic Availability, Soft state, Eventual consistency

Consistency Models

  • Strong Consistency: All nodes see same data at same time
  • Eventual Consistency: System becomes consistent over time
  • Weak Consistency: No guarantees about when consistency occurs

Key Patterns

Saga Orchestrator Pattern

// Centralized orchestrator coordinates steps
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 },
    { name: 'process-payment', execute: chargePayment, compensate: refundPayment },
  ],
  data: {},
  status: 'pending',
});

Saga Choreography Pattern

// Services react to events
eventConsumer.on('order.created', async (event) => {
  await inventoryService.reserve(event.data.items);
  await eventPublisher.publish('inventory.reserved', {...});
});

Idempotency

// Execute operation with idempotency check
await idempotencyHandler.execute(
  idempotencyKey,
  async () => await userService.create(data)
);

Optimistic Locking

// Update with version check
await optimisticLockService.updateWithLock(
  repository,
  id,
  (current) => ({ ...current, name: newName })
);

Best Practices

  1. Design Compensations: Every step needs compensation
  2. Idempotent Steps: Make steps idempotent for retries
  3. Conflict Resolution: Define resolution strategies
  4. Monitoring: Track saga execution and consistency lag
  5. Read Models: Use separate read models for queries (CQRS)

Resources