- 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.
97 lines
2.8 KiB
Markdown
97 lines
2.8 KiB
Markdown
---
|
|
name: data-consistency-patterns
|
|
description: 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
|
|
|
|
```typescript
|
|
// 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
|
|
|
|
```typescript
|
|
// Services react to events
|
|
eventConsumer.on('order.created', async (event) => {
|
|
await inventoryService.reserve(event.data.items);
|
|
await eventPublisher.publish('inventory.reserved', {...});
|
|
});
|
|
```
|
|
|
|
### Idempotency
|
|
|
|
```typescript
|
|
// Execute operation with idempotency check
|
|
await idempotencyHandler.execute(
|
|
idempotencyKey,
|
|
async () => await userService.create(data)
|
|
);
|
|
```
|
|
|
|
### Optimistic Locking
|
|
|
|
```typescript
|
|
// 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
|
|
|
|
- [Saga Pattern](https://microservices.io/patterns/data/saga.html)
|
|
- [Event-Driven Architecture](./event-driven-architecture.md)
|
|
- [Error Handling Patterns](./error-handling-patterns.md)
|
|
- Skill Source: `.cursor/skills/data-consistency-patterns/SKILL.md`
|