Microservices Domain Driven Design
Choosing where one service ends and the next begins is the hardest decision in microservices. Domain Driven Design, shortened to DDD, offers a practical method. It places the business at the center and draws service borders around business meaning rather than technical layers.
Layman Explanation
Think about the word "account" in a bank. A loan officer sees an account as a debt schedule. A cashier sees it as a balance and a list of deposits. A marketing analyst sees it as a customer segment. One word carries three meanings. DDD says each group deserves its own model, and each model lives in its own boundary.
Core Vocabulary
Domain
The domain is the business area your software serves, such as retail, banking, or logistics.
Subdomain
A subdomain is one slice of the domain. A retail domain contains subdomains like catalog, ordering, shipping, and billing.
Bounded Context
A bounded context is a border inside which every term has one exact meaning. Most teams map one bounded context to one microservice, or to a small group of closely related services.
Ubiquitous Language
The ubiquitous language is the shared vocabulary that developers and business experts use together. If the business says "shipment," the code says Shipment, not DeliveryRecord.
Diagram: Bounded Contexts in a Shop
+----------------+ +----------------+ +----------------+
| CATALOG | | ORDERING | | SHIPPING |
| | | | | |
| Product: | | Product: | | Product: |
| name, photo, | | id, quantity, | | weight, size, |
| description | | price paid | | fragile flag |
+----------------+ +----------------+ +----------------+
| | |
+---------- share only the product id ----+
Each context holds a different version of "Product." Catalog stores marketing details. Ordering stores the price at the moment of purchase. Shipping stores physical facts. The contexts exchange only the product identifier.
Building Blocks Inside a Context
Entity
An entity has a lasting identity. A customer with id 4521 remains the same customer after changing their address.
Value Object
A value object has no identity and is defined by its values. Two addresses with identical street, city, and code are equal.
Aggregate
An aggregate is a cluster of entities treated as a single unit for changes. An Order aggregate contains order lines. Code changes lines through the Order, which keeps totals correct.
Domain Event
A domain event records something meaningful that happened, such as OrderPlaced or PaymentReceived. Other contexts subscribe to these events.
Finding Boundaries: Event Storming
Event storming is a workshop where business and technical people stick notes on a wall. Follow these steps:
- Write every important business event in past tense on orange notes.
- Arrange the notes in time order from left to right.
- Add the commands and people that trigger each event.
- Circle groups of notes that share vocabulary. Each circle becomes a candidate bounded context.
Context Mapping
Contexts need relationships. Teams describe them with simple labels.
| Relationship | Meaning |
|---|---|
| Upstream / Downstream | The upstream context supplies data and the downstream context consumes it |
| Shared Kernel | Two teams co-own a small common model |
| Anti-Corruption Layer | A translator protects a new context from messy old models |
Common Mistakes
- Splitting services by technical layer, such as one service for all databases.
- Creating one service per database table.
- Skipping conversations with business experts.
Key Takeaways
DDD turns business knowledge into service boundaries. Bounded contexts keep meanings clear, aggregates protect rules, and domain events connect contexts without tight coupling.
