CQRS & Event Sourcing Architect
Design systems that separate read/write models and derive state from immutable event logs using EventStoreDB, Marten, or custom stores.
Activation Triggers
Activate on: "CQRS", "event sourcing", "event store", "read model", "projection", "aggregate", "domain event", "command handler", "EventStoreDB", "Marten"
NOT for: Message broker infrastructure → event-driven-architecture-expert | Database query optimization → data-warehouse-optimizer | API design → api-architect
Quick Start
- Model aggregates — identify consistency boundaries in your domain
- Define domain events — past-tense facts:
OrderPlaced, ItemShipped
- Implement command handlers — validate, then emit events
- Build event store — append-only log (EventStoreDB, PostgreSQL, DynamoDB)
- Create projections — materialize read models from event streams
Core Capabilities
| Domain |
Technologies |
| Event Stores |
EventStoreDB 24.x, Marten 7.x, custom PostgreSQL |
| Frameworks |
NestJS CQRS, Axon (JVM), Eventuous, Commanded (Elixir) |
| Projections |
In-memory, PostgreSQL views, Elasticsearch, Redis |
| Serialization |
JSON with versioned schemas, Protobuf |
| Testing |
Given-When-Then event test pattern |
Architecture Patterns
CQRS Separation
Command Side Query Side
───────────── ──────────
POST /orders GET /orders/:id
↓ ↑
Command Handler Read Model (Postgres/ES)
↓ ↑
Aggregate.apply(event) Projection Handler
↓ ↑
Event Store (append) ──────→ Event Subscription
Aggregate with Event Replay
class OrderAggregate {
private status: 'draft' | 'placed' | 'shipped' = 'draft';
private items: OrderItem[] = [];
private uncommitted: DomainEvent[] = [];
place(command: PlaceOrderCommand): void {
if (this.status !== 'draft') throw new Error('Order already placed');
this.apply(new OrderPlaced({ orderId: command.orderId, items: command.items }));
}
private apply(event: DomainEvent): void {
this.when(event); // mutate state
this.uncommitted.push(event); // track for persistence
}
private when(event: DomainEvent): void {
if (event instanceof OrderPlaced) {
this.status = 'placed';
this.items = event.items;
}
}
static rehydrate(events: DomainEvent[]): OrderAggregate {
const agg = new OrderAggregate();
events.forEach(e => agg.when(e));
return agg;
}
}
Projection Rebuild Strategy
1. Stop projection subscription
2. Drop/truncate read model table
3. Replay all events from position 0
4. Resume subscription at current position
Key: projections are disposable — the event log is the source of truth
Anti-Patterns
- CRUD in disguise — if you just save/load entities without events, you have CRUD with extra steps
- Anemic events — events like
OrderUpdated are useless; capture what changed specifically
- Querying the event store — never query raw events for read use cases; build a projection
- Deleting events — event stores are append-only; use compensating events for corrections
- Ignoring eventual consistency — read models lag behind writes; design UIs to handle this gracefully
Quality Checklist
1---2license: Apache-2.03name: cqrs-event-sourcing-architect4description: "CQRS pattern, event stores, projections, eventual consistency. Activate on: CQRS, event sourcing, event store, read model, projection, aggregate, domain event, command handler. NOT for: message broker setup (use event-driven-architecture-expert), database optimization (use data-warehouse-optimizer)."5allowed-tools: Read,Write,Edit,Bash(npm:*,npx:*,docker:*)6category: Backend & Infrastructure7tags:8 - cqrs9 - event-sourcing10 - ddd11 - projections12 - eventual-consistency13pairs-with:14 - skill: event-driven-architecture-expert15 reason: Event sourcing depends on reliable event infrastructure16 - skill: distributed-transaction-manager17 reason: Saga patterns complement CQRS for cross-aggregate consistency18 - skill: schema-evolution-manager19 reason: Event schema evolution is critical for long-lived event stores20metadata:21 source: https://skillmd.com/skills/curiositech/cqrs-event-sourcing-architect22---2324# CQRS & Event Sourcing Architect2526Design systems that separate read/write models and derive state from immutable event logs using EventStoreDB, Marten, or custom stores.2728## Activation Triggers2930**Activate on:** "CQRS", "event sourcing", "event store", "read model", "projection", "aggregate", "domain event", "command handler", "EventStoreDB", "Marten"3132**NOT for:** Message broker infrastructure → `event-driven-architecture-expert` | Database query optimization → `data-warehouse-optimizer` | API design → `api-architect`3334## Quick Start35361. **Model aggregates** — identify consistency boundaries in your domain372. **Define domain events** — past-tense facts: `OrderPlaced`, `ItemShipped`383. **Implement command handlers** — validate, then emit events394. **Build event store** — append-only log (EventStoreDB, PostgreSQL, DynamoDB)405. **Create projections** — materialize read models from event streams4142## Core Capabilities4344| Domain | Technologies |45|--------|-------------|46| **Event Stores** | EventStoreDB 24.x, Marten 7.x, custom PostgreSQL |47| **Frameworks** | NestJS CQRS, Axon (JVM), Eventuous, Commanded (Elixir) |48| **Projections** | In-memory, PostgreSQL views, Elasticsearch, Redis |49| **Serialization** | JSON with versioned schemas, Protobuf |50| **Testing** | Given-When-Then event test pattern |5152## Architecture Patterns5354### CQRS Separation5556```57Command Side Query Side58───────────── ──────────59POST /orders GET /orders/:id60 ↓ ↑61Command Handler Read Model (Postgres/ES)62 ↓ ↑63Aggregate.apply(event) Projection Handler64 ↓ ↑65Event Store (append) ──────→ Event Subscription66```6768### Aggregate with Event Replay6970```typescript71class OrderAggregate {72 private status: 'draft' | 'placed' | 'shipped' = 'draft';73 private items: OrderItem[] = [];74 private uncommitted: DomainEvent[] = [];7576 place(command: PlaceOrderCommand): void {77 if (this.status !== 'draft') throw new Error('Order already placed');78 this.apply(new OrderPlaced({ orderId: command.orderId, items: command.items }));79 }8081 private apply(event: DomainEvent): void {82 this.when(event); // mutate state83 this.uncommitted.push(event); // track for persistence84 }8586 private when(event: DomainEvent): void {87 if (event instanceof OrderPlaced) {88 this.status = 'placed';89 this.items = event.items;90 }91 }9293 static rehydrate(events: DomainEvent[]): OrderAggregate {94 const agg = new OrderAggregate();95 events.forEach(e => agg.when(e));96 return agg;97 }98}99```100101### Projection Rebuild Strategy102103```1041. Stop projection subscription1052. Drop/truncate read model table1063. Replay all events from position 01074. Resume subscription at current position108109Key: projections are disposable — the event log is the source of truth110```111112## Anti-Patterns1131141. **CRUD in disguise** — if you just save/load entities without events, you have CRUD with extra steps1152. **Anemic events** — events like `OrderUpdated` are useless; capture *what changed* specifically1163. **Querying the event store** — never query raw events for read use cases; build a projection1174. **Deleting events** — event stores are append-only; use compensating events for corrections1185. **Ignoring eventual consistency** — read models lag behind writes; design UIs to handle this gracefully119120## Quality Checklist121122- [ ] Aggregates enforce invariants before emitting events123- [ ] Events are immutable, past-tense, and contain all needed data124- [ ] Event store is append-only with optimistic concurrency125- [ ] Projections can be rebuilt from scratch without data loss126- [ ] Event versioning strategy defined (upcasting or schema registry)127- [ ] Given-When-Then tests cover all aggregate state transitions128- [ ] Snapshot strategy defined for aggregates with long event streams (>1000 events)129- [ ] Eventual consistency communicated clearly in API contracts130- [ ] Idempotent projection handlers (replaying events produces same read model)