Build an Event-Sourced Order Service
Build an Event-Sourced Order Service
Section titled “Build an Event-Sourced Order Service”What You’ll Build: A complete event-sourced order management system where all state changes are captured as immutable events.
Overview
Section titled “Overview”This tutorial teaches event sourcing by building an order management service. You’ll learn to model business logic as aggregates that emit events, project those events into read models, and leverage the power of event replay.
By the end, you’ll have a service that:
- Creates orders as aggregates
- Adds/removes items with business rule validation
- Places and cancels orders
- Projects events into queryable read models
- Supports complete audit trails
- Enables time-travel queries
Learning Objectives
Section titled “Learning Objectives”By the end of this tutorial, you will be able to:
- Design aggregates that enforce business invariants
- Create domain events that capture state changes
- Build event handlers that update read models
- Use the event store for persistence
- Replay events to rebuild state
- Implement event-driven workflows
Prerequisites
Section titled “Prerequisites”Before starting this tutorial, you should:
- Complete Todo Service and User Management tutorials
- Understand domain-driven design basics
- Understand the CQRS pattern
- Have PostgreSQL running (for event store)
What We’re Building
Section titled “What We’re Building”An order management service with:
Aggregates:
Order- Root aggregate managing order lifecycle
Commands:
CreateOrder- Initialize new orderAddItem- Add product to orderRemoveItem- Remove product from orderPlaceOrder- Submit order for processingCancelOrder- Cancel a pending order
Domain Events:
OrderCreatedItemAddedItemRemovedOrderPlacedOrderCancelled
Queries:
GetOrder- Retrieve order detailsListOrders- List orders for customer
Read Models:
OrderReadModel- Projected from events for queries
For the complete tutorial with all implementation details, see the platform example at:
Section titled “For the complete tutorial with all implementation details, see the platform example at:”/docs-new/06-examples/event-sourced-service/README.md
This tutorial will guide you through:
- Setting up event sourcing infrastructure
- Creating domain events
- Building the Order aggregate
- Implementing command handlers with event store
- Creating read model projections
- Testing event sourcing flows
- Using event replay and catchup
The key difference from CRUD services is that instead of directly modifying state, we:
- Load aggregate from event history
- Execute business logic (emits events)
- Append events to event store
- Events automatically update read models
Refer to the detailed event-sourced service example for complete code and explanations.