cqrs
/cqrs
Pomaga stosować CQRS: handlery komend, projekcje odczytu, denormalizację zdarzeń i asymetryczne skalowanie, także w parze z Event Sourcing.
- Autor
- G1Joshi ↗
- Repozytorium
- Agent-Skills ↗
- Licencja
- MIT
- Ostatnia zmiana repozytorium
- 01.10.2026
Ocena i bezpieczeństwo
- Pliki w paczce
- 1
- Skrypty
- 0
- Komentarze HTML
- 0
- Adresy URL w SKILL.md
- 2
Brak sygnałów. Repozytorium zmienione 01.10.2026, bez odwołań do wycofanych modeli i API.
MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.
Co robi i kiedy
Pomaga stosować CQRS: handlery komend, projekcje odczytu, denormalizację zdarzeń i asymetryczne skalowanie, także w parze z Event Sourcing.
Oryginalny opis (EN)
Expert CQRS (Command Query Responsibility Segregation) assistance covering command handlers, read projections, event denormalization, and asymmetric scaling. Use when separating read and write models, optimizing read performance, or pairing with Event Sourcing.
Jak działa
- When to Use
- Quick Start
- Core Concepts
- Common Patterns
- Best Practices
- Troubleshooting
- References
Podgląd SKILL.md
---
name: cqrs
description: Expert CQRS (Command Query Responsibility Segregation) assistance covering command handlers, read projections, event denormalization, and asymmetric scaling. Use when separating read and write models, optimizing read performance, or pairing with Event Sourcing.
---
# CQRS (Command Query Responsibility Segregation)
CQRS is a pattern that separates read and update operations for a data store. Instead of using a single model for both, you have a **Command Model** (Write) and a **Query Model** (Read).
## When to Use
- **Asymmetric Read/Write Workloads**: Systems where reads outnumber writes by 100:1 or 1000:1 and require independent horizontal scaling.
- **Complex Reporting & Search Views**: Aggregating data across multiple aggregates into denormalized read stores (Elasticsearch, Read Replicas).
- **Collaboration & High Concurrency**: Optimizing write performance and transactional consistency while delivering instant query responses.
- **Pairing with Event Sourcing**: When state changes are captured as events and read models are asynchronously projected from event streams.
## Quick Start
```typescript
// Command Side (Write) - Optimized for Integrity
class CreateOrderCommand { ... }
class OrderAggregate {
create(cmd: CreateOrderCommand) {
// Validate business rules
// Save to DB (Normalized / Event Store)
}
}
// Query Side (Read) - Optimized for Speed
class GetOrderSummaryQuery { ... }
class OrderSummaryProjector {
// Listens to "OrderCreated" event
on(event) {
// Update a flat "Read DB" (e.g., ElasticSearch, Redis, Document DB)
// optimized for the specific UI view
}
}
```
## Core Concepts
### Separation of Command and Query Models
Commands represent intent to mutate state (void return or ID); Queries request data without side effects:
```text
Client
├── Command: POST /orders (CreateOrderCommand) ──→ Command Handler ──→ Write DB (Postgres)
│ │ (CDC / Events)
└── Query: GET /orders/summary (OrderSummary) ←── Read Handler ←── Read DB (Elastic/Redis)
```
### Command Handlers with Explicit Domain Mutex
Validates permissions and business invariants before committing writes:
```typescript
// commands/cancel-order.command.ts
export interface CancelOrderCommand {
orderId: string;
reason: string;
requestedByUserId: string;
}
export class CancelOrderCommandHandler {
constructor(private readonly orderRepo: OrderRepositoryPort) {}
async handle(cmd: CancelOrderCommand): Promise<void> {
const order = await this.orderRepo.getById(cmd.orderId);
if (!order) throw new Error("Order not found");
order.cancel(cmd.reason, cmd.requestedByUserId);
await this.orderRepo.save(order);
}
}
```
### Materialized Read Projections
Updates optimized denormalized read views in response to domain events:
```typescript
// projections/order-dashboard.projection.ts
export class OrderDashboardProjection {
async onOrderCancelled(event: OrderCancelledEvent): Promise<void> {
await redis.hset(`dashboard:order:${event.orderId}`, {
status: "CANCELLED",
cancelledAt: event.timestamp,
cancellationReason: event.reason,
});
}
}
```
## Common Patterns
### Read-Model Projection with Event Handlers
**Problem**: Queries require aggregated data from multiple write models, causing slow and complex database joins.
**Solution**:
Use an asynchronous projector to update a de-normalized, query-optimized document or key-value store:
```typescript
interface OrderCreatedEvent {
orderId: string;
customerId: string;
total: number;
createdAt: string;
}
class OrderReadModelProjector {
async handleOrderCreated(event: OrderCreatedEvent): Promise<void> {
await searchIndex.upsert("orders_summary", event.orderId, {
id: event.orderId,
customerId: event.customerId,
totalAmount: event.total,
placedAt: event.createdAt,
status: "PLACED",
});
}
}
```
## Best Practices
**Do**:
- Make Queries Completely Side-Effect Free: Ensure queries never mutate database state or trigger transactional locks.
- Embrace Eventual Consistency: Design client user experiences to anticipate small projection delays (optimistic UI updates).
- Optimize Read Models for the UI: Structure read tables/documents to match the exact view model needed by frontend clients.
- Use Dedicated Read Replicas: Direct query handlers to read replicas or fast key-value caches to protect the write primary.
**Don't**:
- Apply CQRS globally to an entire system: Apply CQRS only to bounded contexts with complex requirements; simple CRUD benefits from standard MVC.
- Share entities between Command and Query pipelines: Keep query handlers completely independent of domain aggregate classes.
- Let read projections write back to the write store: Keep the data pipeline unidirectional.
## Troubleshooting
| Error | Cause | Solution |
| :----------- | :---------------------------------------------- | :-------------------------------------------------------------------- |
| `Stale Data` | Lag between Command execution and Query update. | UI design updates (spinners/optimistic updates); Check projector lag. |
| `Complexity` | Over-engineering. | Revert to simple CRUD if the domain doesn't warrant separation. |
## References
- [MSDN CQRS Guide](https://learn.microsoft.com/en-us/azure/architecture/patterns/cqrs)
- [Greg Young - CQRS Documents](https://cqrs.files.wordpress.com/2010/11/cqrs_documents.pdf)
Wersje
| Repozytorium | Wynik | Licencja |
|---|---|---|
| Ta strona · G1Joshi/Agent-Skills | 100 | MIT |
| TheBeardedBearSAS/claude-craft | 100 | MIT |
| Amey-Thakur/AI-SKILLS | 90 | MIT |
Migawka bazy z 13.11.2026 · wersja paczki 258cb6044e8e (skrót SHA-256 całej paczki)
Pobrania w ostatnich 30 dniach: 0 · ranking
Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.
Podobne skille
- 90cqrs-implementation
Wdraża CQRS: rozdziela modele odczytu i zapisu, optymalizuje zapytania i buduje systemy oparte na zdarzeniach. - 95cqrs-implementation
Implementuje Command Query Responsibility Segregation: rozdzielenie modeli odczytu i zapisu, optymalizacja zapytań i systemy oparte na zdarzeniach. - 90cqrs-implementation
Wdraża Command Query Responsibility Segregation w skalowalnych architekturach: rozdzielanie modeli odczytu i zapisu, optymalizacja zapytań i systemy oparte na zdarzeniach. - 100cqrs-implementation
Prowadzi wdrożenie CQRS w skalowalnych architekturach: rozdzielenie modeli odczytu i zapisu, optymalizacja zapytań i systemy oparte na zdarzeniach. - 90backend-cqrs-patterns
Rozdziela modele zapisu i odczytu w stylu CQRS: walidację komend, zoptymalizowany model odczytu i spójność ostateczną. - 95event-store
Projektuje i implementuje magazyny zdarzeń dla systemów event sourcing: trwałość zdarzeń, projekcje, snapshoty i wzorzec CQRS.