- 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.
2.8 KiB
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
- Design Compensations: Every step needs compensation
- Idempotent Steps: Make steps idempotent for retries
- Conflict Resolution: Define resolution strategies
- Monitoring: Track saga execution and consistency lag
- Read Models: Use separate read models for queries (CQRS)
Resources
- Saga Pattern
- Event-Driven Architecture
- Error Handling Patterns
- Skill Source:
.cursor/skills/data-consistency-patterns/SKILL.md