1. Understand the Goal: The user wants a "document event modeling primer" that clearly distinguishes between "command," "domain," and "integration" events. The tone should be educational and clear (a "primer").
2. Target Audience: Software developers, architects, and system designers who are getting into Event Sourcing, Domain-Driven Design (DDD), or Event-Driven Architecture (EDA). They likely know the basics but might be confused by the nuances of these specific terms.
3. Core Concepts to Define & Distinguish:
-
Command:
- Intent: "Please do this."
- Direction: Outside-in (UI/API -> Application/Domain).
- Outcome: Can be rejected. If accepted, triggers an Event.
- Tense: Imperative (Present/Future).
-
Domain Event:
- Fact: "This happened."
- Scope: Internal to the Bounded Context.
- Granularity: Fine-grained, technical, state change.
- Tense: Past tense.
- Usage: Event sourcing, state reconstitution.
-
Integration Event:
- Broadcast: "Hey world, this business milestone happened."
- Scope: Cross-boundary (between microservices/bounded contexts).
- Granularity: Coarser, focused on business facts useful to others.
- Tense: Past tense.
- Usage: Decoupling systems.
4. Structure of the Document:
* Title: Professional and clear.
* Introduction: Briefly set the stage (why this matters).
* The Core Concept: A visual or conceptual analogy (Intent vs. Fact vs. Broadcast).
* Section 1: Commands (The Request): Definition, Characteristics, Example.
* Section 2: Domain Events (The Reality): Definition, Characteristics, Example.
* Section 3: Integration Events (The Announcement): Definition, Characteristics, Example.
* Comparison Table: A quick reference guide is essential for a "primer."
* The Flow (Lifecycle): How they connect (Command -> Validation -> Domain Event -> Translation -> Integration Event).
* Best Practices/Common Pitfalls: Value-add advice.
* Conclusion: Wrap up.
5. Drafting Content - Specific Examples:
* Scenario: An e-commerce system (easy to understand).
* Command: PlaceOrder, CreateOrder? Let's use AddItemToCart or SubmitOrder. Let's go with SubmitOrder.
* Domain Event: OrderSubmitted, OrderStatusChangedToSubmitted.
* Integration Event: OrderConfirmationPublished or OrderCreated. Let's use OrderCreated (or OrderPlaced) for external consumption.
*Refining the distinctions:*
* Command: "Submit this order." (Might fail if out of stock).
* Domain Event: "Order state updated to submitted." (Technical, internal).
* Integration Event: "Order Placed" (contains summary data needed by Shipping/Billing).
6. Refining the "Story":
* The "Restaurant" Analogy works wonders here.
* Command = Customer saying "I'd like a burger."
* Domain Event = The Chef drops a ticket on the rail (internal "Cook burger" trigger).
* Integration Event = The waiter yelling "Order up!" or the receipt printed for the customer (public notification).
*Decision:* I will use a "Financial Transaction" or "E-commerce" example as the primary technical example because it's standard, but keep the language crisp.
7. Drafting the Sections (Mental Sandbox):
- Commands: Focus on Validation. A command isn't a fact yet. It's a wish.
- Domain Events: Focus on State. It's the source of truth. It must be immutable. It belongs to the Aggregate.
- Integration Events: Focus on Decoupling. It needs to be versioned carefully because external systems depend on it. It often aggregates several domain events.
8. Formatting:
* Use Markdown headers.
* Use bolding for key terms.
* Use code blocks for JSON/Class examples.
* Use a table for the summary.
9. Review against User Request:
* Distinguish Command, Domain, Integration? Yes.
* Primer format? Yes, starts simple, gets detailed.
* Clarity? The distinction between "Internal State Change" vs "External Contract" is the hardest part to explain clearly. I need to emphasize that Domain Events are about the state of the specific object, while Integration Events are about the business process across systems.
10. Final Polish: (Self-Correction during drafting)
* Correction: Make sure not to confuse Domain Events (DDD) with Integration Events (EDA) too much. They look similar but serve different masters. A Domain Event is part of the model. An Integration Event is a message between models.
* Emphasis: Mention that one Domain Event might trigger zero or multiple Integration Events (or vice versa, though less common, usually 1:1 or Many:1).
11. Output Generation: (Proceeding to generate the response based on these steps.)
Event Modeling Primer: The Triad of System Changes
In modern software architecture—specifically Event Sourcing, Domain-Driven Design (DDD), and Microservices—systems communicate through changes in state. However, not all changes are created equal.
To design a robust system, you must distinguish between Commands (intent), Domain Events (facts), and Integration Events (notifications).
This primer defines these three concepts and illustrates their lifecycle using a single workflow: A Customer Placing an Order.
1. The Command (The Request)
"I want something to happen."
A Command represents an intent to change the system state. It is a message sent to the system (or a specific Aggregate) requesting an action. It is imperative and oriented toward the future.
Key Characteristics
- Direction: Outside-in (User → Application).
- Tense: Imperative (e.g.,
AddItem, ShipOrder).
- Outcome: It can be rejected. If the validation fails, nothing happens.
- Consumer: The Domain Model (specifically, an Aggregate Root).
Example: PlaceOrder Command
{
"commandId": "cmd-123",
"type": "PlaceOrder",
"payload": {
"customerId": "cust-99",
"items": [{ "sku": "ABC-1", "qty": 2 }],
"currency": "USD"
}
}
If the customer has insufficient funds, this Command is rejected, and no further action is taken.
2. The Domain Event (The Fact)
"Something happened inside this boundary."
A Domain Event represents a state change that has successfully occurred within the domain model. It is the source of truth. It is immutable and represents the history of the system.
Key Characteristics
- Direction: Inside-out (Domain → Persistence/Projection).
- Tense: Past Tense (e.g.,
ItemAdded, OrderShipped).
- Outcome: It is a fact. It cannot be rejected; it has already happened.
- Granularity: Fine-grained. It describes exactly how the state changed.
Example: OrderPlaced Event
{
"eventId": "evt-987",
"type": "OrderPlaced",
"timestamp": "2023-10-27T10:00:00Z",
"data": {
"orderId": "order-500",
"customerId": "cust-99",
"totalAmount": 50.00,
"status": "PENDING_PAYMENT"
}
}
This event is stored in the database (Event Store) as the permanent record of the transaction.
3. The Integration Event (The Announcement)
"Let the outside world know."
An Integration Event is used to communicate state changes across boundaries (e.g., between Microservices). While a Domain Event is about strict consistency within a model, an Integration Event is about eventual consistency and interoperability.
Key Characteristics
- Direction: Boundary-to-Boundary (Service A → Service B).
- Tense: Past Tense.
- Content: Often flattened or summarized. It contains only what external systems need to know, hiding internal implementation details.
- Protocol: Often uses message brokers (Kafka, RabbitMQ, Azure Service Bus).
Example: OrderConfirmed Integration Event
Note: We do not send the internal OrderPlaced event. Instead, we send a simplified version for external consumption.
{
"eventId": "int-456",
"type": "OrderConfirmed",
"source": "OrderService",
"specversion": "1.0",
"data": {
"orderId": "order-500",
"userEmail": "jane@example.com",
"shippingPreference": "EXPRESS"
}
}
The Shipping Service listens for this event to start the delivery process.
Comparative Summary
| Feature | Command | Domain Event | Integration Event |
|---|
| Definition | An instruction to do something. | A record of something that happened. | A notification to other systems. |
| Tone | Imperative ("Do X") | Indicative ("X happened") | Informative ("FYI, X happened") |
| Timing | Before the state change. | After the state change. | After the transaction commits. |
| Validation | Can fail/be rejected. | Is immutable truth. | Must be handled (retries possible). |
| Audience | The Aggregate/Domain. | The Event Store / Read Models. | Other Bounded Contexts / Services. |
| Coupling | Coupled to the API interface. | Coupled to the internal domain logic. | Decoupled (Public Contract). |
The Lifecycle Flow
To visualize how these three interact, let’s trace the path of a user buying a product.
Step 1: Intent (Command)
The user clicks "Buy." The UI sends a PlaceOrder command to the OrderService.
- Result: The command enters the system.
Step 2: Validation & State Change (Domain Event)
The OrderService (Aggregate) checks if the item is in stock.
- If No: An error is returned. Stop.
- If Yes: The aggregate creates an
OrderPlaced domain event and persists it to the database.
- Result: The state of the Order is now officially "Placed."
Step 3: Propagation (Integration Event)
A background process (or a transactional hook) detects the new OrderPlaced domain event. It translates this internal fact into a public OrderConfirmed integration event and publishes it to a Message Bus.
- Result: The Inventory Service and Email Service receive the notification.
Common Pitfalls
-
Mixing Domain and Integration Events:
- Mistake: Publishing your raw Domain Events directly to Kafka.
- Consequence: You couple your internal database schema to external systems. If you rename a field in your database, you break the downstream services.
- Solution: Always translate Domain Events into Integration Events (DTOs) at the boundary.
-
Business Logic in Commands:
- Mistake: Putting validation logic in the Command handler before it reaches the Aggregate.
- Consequence: Anemic domain model.
- Solution: The Command should simply fetch the Aggregate and tell it to act. The Aggregate (Domain) should enforce the rules and emit the Event.
-
Ignoring Idempotency:
- Mistake: Treating Integration Events as "fire and forget" without unique IDs.
- Consequence: If a message broker redelivers a message, you might charge a customer twice.
- Solution: Ensure Integration Events have unique IDs so consumers can check if they have already processed the event.