# Domain event

From \[\[Domain-Driven Design]] (\[\[DDD]]).

## Context

A service often needs to publish events when it updates its data. These events might be needed, for example, to update a [CQRS view](https://github.com/ducminhgd/notes/blob/main/computer/microservices/cqrs.md). Alternatively, the service might participate in an choreography-based [saga](https://gia-duong-duc-minh.gitbook.io/notes/computer/microservices/saga), which uses events for coordination.

## Problem

How does a service publish an event when it updates its data?

## Solution

Organize the business logic of a service as a collection of DDD [aggregates](https://gia-duong-duc-minh.gitbook.io/notes/computer/microservices/aggregate) that emit domain events when they created or updated. The service publishes these domain events so that they can be consumed by other services.

## Related patterns

* The \[\[Saga]] and \[\[CQRS]] patterns create the need for this pattern
* The \[\[Aggregate]] pattern is used to structure the business logic
* The \[\[Transactional outbox]] pattern is used to publish events as part of a database transaction
* \[\[Event sourcing]] is sometimes used to publish domain events

## See also

My book [Microservices patterns](https://microservices.io/book) describes this pattern in a lot more detail.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gia-duong-duc-minh.gitbook.io/notes/computer/microservices/domain-event.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
