Domain-Driven Design for Microservices
Domain-Driven Design (DDD) is the foundation for properly decomposing systems into microservices. Without DDD, you’ll likely end up with a distributed monolith.Learning Objectives:
- Understand DDD strategic patterns
- Identify bounded contexts in a domain
- Map subdomains to microservices
- Design aggregate boundaries
- Apply context mapping patterns
Why DDD for Microservices?
DDD provides the conceptual framework to:- Identify Service Boundaries - Bounded contexts become services
- Define Data Ownership - Aggregates define what a service owns
- Design Communication - Context maps define how services interact
- Align with Business - Ubiquitous language bridges tech and business
Strategic DDD Patterns
Subdomains
Every business has different types of domains:- Core Domain
- Supporting Domain
- Generic Domain
What makes you competitive - The unique value proposition.
- Invest most resources here
- Custom-built, in-house
- Highest quality code
- Best engineers assigned
- Personalized recommendations
- Dynamic pricing engine
- Customer experience optimization
Bounded Contexts
A bounded context is a boundary within which a particular domain model is defined and applicable.Ubiquitous Language
Each bounded context has its own vocabulary that everyone (developers and business) uses.Context Mapping
Context maps show how bounded contexts relate to each other.Relationship Patterns
E-Commerce Context Map
Tactical DDD Patterns
Aggregates
An aggregate is a cluster of domain objects treated as a single unit.Aggregate Design Rules
Rule 1: Reference by ID
Aggregates reference other aggregates only by their ID, never by direct object reference.
Rule 2: Small Aggregates
Keep aggregates small. Large aggregates cause concurrency issues.
Rule 3: Consistency Boundary
The aggregate is the consistency boundary. Changes within are atomic.
Rule 4: Eventual Consistency
Use domain events for cross-aggregate consistency.
Value Objects
Immutable objects defined by their attributes, not identity.Domain Events
Events that indicate something important happened in the domain.Mapping DDD to Microservices
From Bounded Context to Service
Service Structure Based on DDD
Anti-Corruption Layer Example
When integrating with external systems that have a different model:Event Storming Workshop
Event Storming is a workshop technique to discover bounded contexts.Steps
Event Storming Result for E-Commerce
Interview Questions
Q1: What is a Bounded Context?
Q1: What is a Bounded Context?
Answer:
A bounded context is a conceptual boundary within which a particular domain model is defined and applicable. It’s where a specific ubiquitous language is consistent.Key characteristics:
- Same term can mean different things in different contexts (e.g., “Product” in Catalog vs Sales)
- Each context has its own models, rules, and language
- Maps naturally to microservices boundaries
- Reduces complexity by limiting scope
Q2: What is an Aggregate?
Q2: What is an Aggregate?
Answer:
An aggregate is a cluster of domain objects that can be treated as a single unit, with one entity acting as the root.Rules:
- Reference other aggregates by ID only
- Keep aggregates small
- Changes within aggregate are atomic (consistency boundary)
- Cross-aggregate consistency is eventual (via events)
Q3: What is an Anti-Corruption Layer?
Q3: What is an Anti-Corruption Layer?
Answer:
An ACL is a translation layer between two systems with different models. It prevents external/legacy models from “corrupting” your domain model.Use cases:
- Integrating with third-party APIs (Stripe, SendGrid)
- Communicating with legacy systems
- When upstream model doesn’t fit your domain
Q4: How do you identify service boundaries using DDD?
Q4: How do you identify service boundaries using DDD?
Answer:
- Event Storming: Workshop to discover domain events
- Identify Bounded Contexts: Group related events and concepts
- Define Aggregates: What data changes together?
- Map Context Relationships: Customer-Supplier, ACL, etc.
- Each Bounded Context → Microservice
- High cohesion within the service
- Loose coupling between services
- Clear ownership of data
- Matches team structure
Summary
Key Takeaways
- DDD provides the framework for microservice boundaries
- Bounded contexts map to microservices
- Aggregates define data ownership
- Domain events enable loose coupling
- ACL protects from external model pollution
Next Steps
In the next chapter, we’ll dive into Service Communication Patterns - how microservices talk to each other using REST, gRPC, and messaging.