Event Sourcing and CQRS as DDD implementation patterns — when to use, aggregate event streams, projections, snapshots, sagas, upcasting, conflict resolution
npx skills add https://github.com/nWave-ai/nWave --skill nw-ddd-eventsourcing
Implementation patterns for DDD. Event Sourcing stores every state change as an immutable event and derives current state from replay. CQRS separates write and read models. These are tools, not mandatory architecture -- apply selectively to domains where they add value.
ES adds value when:
ES is overkill when:
Key question: "Does knowing the history of how we got here provide business value?" Yes -> consider ES. No -> traditional CRUD is simpler.
Events are immutable records of things that happened. Past tense: OrderPlaced, PaymentReceived, ItemShipped.
Event structure: Include all data needed to understand what happened (self-contained) | Use domain language | One event per meaningful business fact | Events carry data only, no behavior
Granularity: Too coarse (OrderUpdated { order: {...} }) loses what changed. Too fine (OrderFieldChanged { field, old, new }) is generic audit log. Just right (OrderConfirmed { orderId, confirmedBy, confirmedAt }) has clear business meaning.
Single source of truth. Core operations:
append(streamId, expectedVersion, events) -> success or concurrency conflictread(streamId) -> ordered list of eventssubscribe(filter) -> push notification for new eventsProperties: Append-only (never modify/delete) | Ordered within stream | Immutable | Versioned (each event has position)
Stream identity: Each aggregate instance owns a stream: Order-123, Customer-789. The stream ID is the aggregate ID.
Loading (rehydration): Read all events from stream -> create empty aggregate -> apply each event in order -> aggregate in current state, ready for commands.
| Aspect | Write Side (Commands) | Read Side (Queries) |
|--------|----------------------|---------------------|
| Purpose | Validate + change state | Serve information |
| Model | Aggregates (normalized) | Projections (denormalized) |
| Optimization | Consistency, correctness | Query speed, UX |
| Scale | Lower throughput typically | Higher throughput |
| Technology | Event store | Any DB (SQL, NoSQL, search, graph) |
Data flow: Command -> Aggregate -> Event Store -> Projection -> Read Model -> Query
Projections transform events into query-optimized views. Properties: disposable (rebuild from event store anytime) | eventually consistent (lag between event and update) | purpose-specific (one per use case) | multiple (same events feed many views)
Types: Live (real-time as events arrive) | Catch-up (replay from beginning, then switch to live) | One-off (replay once for analytics/migration)
Rebuilding: Delete read model -> replay all events from position 0 -> switch to live processing. This is a superpower: add new views retroactively from complete history.
For aggregates with thousands of events, periodically save state snapshot. On load: load latest snapshot + replay only events after snapshot. Fewer events to process.
When: After every N events (e.g., 100) or when load time exceeds threshold. Don't snapshot prematurely -- most aggregates have <100 events and load in milliseconds.
Business processes spanning multiple aggregates. A saga listens for events and issues commands to coordinate multi-step processes.
Key principles: React to events, issue commands (no business logic in saga) | Maintain process state (which step completed) | Handle compensation (rollback on failure) | Keep simple -- complex saga = design smell
Todo Pattern (alternative): Model process as a "todo list" aggregate tracking what needs to be done. All state visible in one place, progress queryable, easy to add/remove steps.
Events are immutable, but schemas evolve. Transform old formats to new during loading.
Weak schema (start here): Flexible format (JSON), handle missing fields with defaults.
Explicit upcaster: Transform V1 -> V2 in a pipeline during event loading, before reaching aggregate/projection.
Upcaster chain: V1 -> V2 -> V3. Each handles one version jump. Test all paths.
Two concurrent commands on same aggregate cause version conflict. Strategies: Retry (default, reload and re-execute) | Merge (domain-specific, both changes apply) | Reject (inform user, safest for critical operations). Start with retry -- most conflicts resolve automatically.
Read side lags write side. Typical: microseconds (same process) to low seconds (across network).
Mitigation: Read-your-own-writes (return command result directly, not from read model) | Optimistic UI (apply expected change on client immediately) | Causal consistency (version token with queries, wait until projection catches up)
Reservation Pattern: Enforce uniqueness constraints (unique email) using dedicated reservation aggregate or DB constraint. Uniqueness in ES is harder than CRUD -- choose strictness based on criticality.
Outbox Pattern: Atomically update event store AND publish to message broker by writing to outbox table in same transaction. Relay process publishes to broker asynchronously.
Decider Pattern: Purely functional aggregate: decide(Command, State) -> List<Event>, evolve(State, Event) -> State, initialState() -> State. Trivially testable, framework-agnostic.
Tombstone Events: Handle deletion in append-only store by emitting a tombstone event. Aggregate refuses commands after tombstone. For GDPR: crypto-shredding or event replacement.
Event-Carried State Transfer: Include relevant data in events even if duplicated from other aggregates. Self-contained events eliminate callback lookups for projections.
Temporal Queries: Replay events up to a timestamp to reconstruct historical state. Impossible with traditional state-based storage. Use for: compliance, debugging, analytics, dispute resolution.
| Technology | Platform | Notes |
|-----------|----------|-------|
| EventStoreDB | Any (gRPC) | Purpose-built, native projections, by Greg Young |
| Axon Server/Framework | Java/Kotlin | Full CQRS/ES framework with command/query bus |
| Marten | .NET (C#) | PostgreSQL as event store + document DB |
| PostgreSQL (manual) | Any | Append-only table with indexing -- simple but manual |
| Eventuous | .NET (C#) | Lightweight modern .NET ES framework |
Given/When/Then is the primary pattern:
GIVEN: [prior events that set up state]
WHEN: [command being issued]
THEN: [resulting events OR error]
Testing pyramid: Value Objects (base) -> Aggregate G/W/T (bulk) -> Projection tests -> Saga tests -> E2E (few)
Implementation maps directly from Event Model specifications to test code. The specification IS the test.
Integration with protocols.io API for managing scientific protocols. This skill should be used when working with protocols.io to search, create, update, or publish protocols; manage protocol steps and materials; handle discussions and comments; organize workspaces; upload and manage files; or integrate protocols.io functionality into workflows. Applicable for protocol discovery, collaborative protocol development, experiment tracking, lab protocol management, and scientific documentation.
Analyzes job descriptions and generates tailored resumes that highlight relevant experience, skills, and achievements to maximize interview chances
Generate Excalidraw diagrams from natural language descriptions. Use when asked to "create a diagram", "make a flowchart", "visualize a process", "draw a system architecture", "create a mind map", or "generate an Excalidraw file". Supports flowcharts, relationship diagrams, mind maps, and system architecture diagrams. Outputs .excalidraw JSON files that can be opened directly in Excalidraw.
Build and distribute Expo development clients locally or via TestFlight
Use when you have a written implementation plan to execute in a separate session with review checkpoints
Data structure for annotated matrices in single-cell analysis. Use when working with .h5ad files or integrating with the scverse ecosystem. This is the data format skill—for analysis workflows use scanpy; for probabilistic models use scvi-tools; for population-scale queries use cellxgene-census.
Benchling R&D platform integration. Access registry (DNA, proteins), inventory, ELN entries, workflows via API, build Benchling Apps, query Data Warehouse, for lab data management automation.
Comprehensive molecular biology toolkit. Use for sequence manipulation, file parsing (FASTA/GenBank/PDB), phylogenetics, and programmatic NCBI/PubMed access (Bio.Entrez). Best for batch processing, custom bioinformatics pipelines, BLAST automation. For quick lookups use gget; for multi-service integration use bioservices.
Take nwave-ai/nw-ddd-eventsourcing from the repository into ~/.claude/skills for personal
use, or into .claude/skills inside a project.
The agent identifies a skill by the name field in its header. Two skills with the
same name cannot sit side by side — one of them will be ignored.