Get the FREE Ultimate OpenClaw Setup Guide →

architecture-paradigm-cqrs-es

npx machina-cli add skill athola/claude-night-market/architecture-paradigm-cqrs-es --openclaw
Files (1)
SKILL.md
3.9 KB

The CQRS and Event Sourcing Paradigm

When To Use

  • Designing event-sourced systems with complex domain logic
  • Systems requiring full audit trails of state changes

When NOT To Use

  • Simple CRUD applications without complex domain logic
  • Small projects where event sourcing adds unnecessary complexity

When to Employ This Paradigm

  • When read and write workloads have vastly different performance characteristics or scaling requirements.
  • When all business events must be captured in a durable, immutable history or audit trail.
  • When a business needs to rebuild projections of data or support temporal queries (e.g., "What did the state of this entity look like yesterday?").

Adoption Steps

  1. Identify Aggregates: Following Domain-Driven Design principles, specify the bounded contexts and the business invariants that each command must enforce on an aggregate.
  2. Model Commands and Events: Define the schemas and validation rules for all commands and the events they produce. Document a clear strategy for versioning and schema evolution.
  3. Implement the Write Side (Command Side): Command handlers are responsible for loading an aggregate's event stream, executing business logic, and atomically appending new events to the stream.
  4. Build Projections to the Read Side: Create separate read models (projections) that are fed by subscriptions to the event stream. Implement back-pressure and retry policies for these subscriptions.
  5. validate Full Observability: Implement detailed logging that includes event IDs, sequence numbers, and metrics for tracking the lag time of each projection.

Key Deliverables

  • An Architecture Decision Record (ADR) detailing the aggregates, the chosen event store technology, the projection strategy, and the expected data consistency model (e.g., eventual consistency SLAs).
  • A suite of tests for command handlers that use in-memory event streams, complemented by integration tests for the projections.
  • Operational tooling for replaying events, taking state snapshots for performance, and managing schema migrations.

Risks & Mitigations

  • High Operational Overhead:
    • Mitigation: Bugs related to event ordering and replays can be difficult to diagnose. Invest heavily in automation, Dead-Letter Queues (DLQs) for failed events, and regular "chaos engineering" drills to test resilience.
  • Challenges of Eventual Consistency:
    • Mitigation: Users may be confused by delays between performing an action and seeing the result. Clearly document the SLAs for read model updates and manage user-facing expectations accordingly, for example, by providing immediate feedback on the command side.
  • Schema Drift:
    • Mitigation: An unplanned change to an event schema can break consumers. Enforce the use of a formal schema registry and implement version gates in the CI/CD pipeline to prevent the emission of unvalidated event versions.

Troubleshooting

Common Issues

Command not found Ensure all dependencies are installed and in PATH

Permission errors Check file permissions and run with appropriate privileges

Unexpected behavior Enable verbose logging with --verbose flag

Source

git clone https://github.com/athola/claude-night-market/blob/master/plugins/archetypes/skills/architecture-paradigm-cqrs-es/SKILL.mdView on GitHub

Overview

This paradigm pairs CQRS (separate read and write models) with Event Sourcing to capture every state change as a durable sequence of events. It enables an immutable audit trail, supports temporal queries, and provides read/write scalability through separation. It is ideal for complex domains requiring auditability, but not suitable for simple CRUD without audit needs.

How This Skill Works

Identify aggregates and domain events, then implement a Write Side that loads an aggregate, enforces business invariants, and atomically appends events to the stream. Build Read Models (projections) that subscribe to the event stream, allowing fast queries and back-pressure handling with retry policies. Maintain strong observability with event IDs, sequence numbers, and tooling for replay and schema evolution.

When to Use It

  • When read and write workloads have different performance characteristics and require separate scaling
  • When a durable, immutable audit trail of all state changes is required
  • When you need to rebuild projections or support temporal queries (e.g., state yesterday)
  • When the domain is complex and benefits from domain-driven design with aggregates and events
  • When operating in distributed systems that require reliable event-driven patterns and replay capabilities

Quick Start

  1. Step 1: Identify Aggregates: define bounded contexts and invariants for each aggregate
  2. Step 2: Model Commands and Events: design schemas, validation, and versioning strategy
  3. Step 3: Implement the Write Side and Build Projections: handle commands, append events, and publish to projection streams

Best Practices

  • Identify aggregates and bounded contexts using Domain-Driven Design principles
  • Model commands and events with clear schemas and a strategy for versioning and schema evolution
  • Implement the Write Side to load an aggregate, enforce rules, and append events atomically
  • Create read-model projections fed by event subscriptions, with back-pressure and retries
  • Invest in observability: include event IDs, sequence numbers, lag metrics, ADRs, and test coverage

Example Use Cases

  • E-commerce order processing with a durable event history and read-optimized projections
  • Banking ledger enabling precise audits and the ability to query historical balances
  • Inventory management with event-sourced stock movements and projection-based dashboards
  • Insurance claim processing with replayable events for audits and regulatory compliance
  • CRM data with audit trails and temporal views for analytics and reporting

Frequently Asked Questions

Add this skill to your agents

Related Skills

Sponsor this space

Reach thousands of developers