What is a bounded context in domain-driven design and how does it map to microservices?
Learn what a bounded context is in domain-driven design, how it maps to microservices, ubiquitous language, context mapping and common interview mistakes.
Expected Interview Answer
A bounded context is an explicit boundary within which a specific domain model, its terms, and its rules are consistent and unambiguous; it maps naturally to a microservice because each service owns one context, its data, and its language.
In domain-driven design a large system is split into subdomains, and each subdomain gets a bounded context where a word like 'customer' or 'order' has exactly one agreed meaning. Inside the boundary the model is internally consistent; across boundaries the same word can mean something different, so contexts communicate through well-defined contracts rather than a shared model. When you draw microservice boundaries along bounded contexts, each service owns its model and database, changes stay local, and teams can evolve independently.
- Keeps each service model small and internally consistent
- Prevents a single bloated shared model across the whole system
- Aligns service boundaries with business capabilities
- Enables independent deployment and team ownership
- Makes integration contracts explicit instead of implicit
AI Mentor Explanation
A bounded context is like the laws that apply only inside a single format of cricket. In a T20 match the word 'over', the fielding restrictions, and the powerplay rules mean one precise thing, while a Test match uses the same words with different limits. Nobody tries to force one rulebook onto both formats; each format is its own consistent world, and umpires translate at the edges. A microservice owns its format's rulebook the same way.
Step-by-Step Explanation
Step 1
Explore the domain
Work with domain experts to map the business into subdomains and the language each area actually uses.
Step 2
Draw context boundaries
Group closely related concepts into bounded contexts where every term has one unambiguous meaning.
Step 3
Define the ubiquitous language
Agree on the vocabulary used inside each context and document where the same word means something different elsewhere.
Step 4
Map relationships
Use a context map to describe how contexts integrate — shared kernel, customer-supplier, or anti-corruption layer.
Step 5
Assign services and data
Give each bounded context its own microservice, owning its model and private database, communicating only through contracts.
What Interviewer Expects
- A clear definition tying model consistency to an explicit boundary
- Understanding that the same term can mean different things across contexts
- The link between bounded context and one service owning its own data
- Awareness of context mapping and anti-corruption layers
- Ability to give a concrete business example
Common Mistakes
- Confusing a bounded context with a single database table or class
- Assuming one shared canonical model can serve the whole system
- Drawing service boundaries by technical layer instead of business capability
- Letting two services share the same database, breaking the boundary
- Ignoring the need for translation between contexts
Best Answer (HR Friendly)
“A bounded context is a clear boundary around one part of the business where every term means exactly one thing. In microservices, each service usually owns one such boundary along with its own data, so teams can build and change their part without stepping on everyone else.”
Code Example
// Sales context: an Order is about winning a purchase
namespace Sales {
export interface Order {
orderId: string
customerId: string
total: number
discountCode?: string
}
}
// Fulfillment context: an Order is about shipping goods
namespace Fulfillment {
export interface Order {
orderId: string
warehouseId: string
items: { sku: string; quantity: number }[]
shippingAddress: string
}
}
// Each service owns its own model; they exchange a contract, not internals.
function toFulfillment(o: Sales.Order, address: string): Fulfillment.Order {
return { orderId: o.orderId, warehouseId: 'W1', items: [], shippingAddress: address }
}Follow-up Questions
- What is a context map and what relationship patterns does it capture?
- What is an anti-corruption layer and when do you need one?
- How do bounded contexts relate to subdomains (core, supporting, generic)?
- How do you avoid a distributed monolith when splitting by context?
- How does each service owning its own database follow from bounded contexts?
MCQ Practice
1. What primarily defines a bounded context?
A bounded context is the boundary within which a specific model and its ubiquitous language stay consistent and unambiguous.
2. Why does a bounded context map well to a microservice?
Aligning a service with one bounded context means it owns its model and database, so changes stay local and teams work independently.
3. How should two bounded contexts communicate?
Contexts integrate through explicit contracts (and patterns like anti-corruption layers), never by reaching into each other's internals.
Flash Cards
What is a bounded context? — An explicit boundary where a specific domain model, terms, and rules stay consistent and unambiguous.
What is a ubiquitous language? — The shared, precise vocabulary used by developers and experts inside one bounded context.
What is a context map? — A diagram of how bounded contexts relate and integrate, e.g. customer-supplier or anti-corruption layer.
Context to service rule of thumb? — One bounded context per microservice, each owning its own model and private database.
Continue Learning
Related Interview Questions
What is the Strangler Fig pattern for migrating a monolith to microservices?
medium
What is the CQRS pattern and when should you use it?
medium
Two services call each other dozens of times to serve one request. Is that a performance problem or a boundary problem, and how do you fix it?
medium
You are asked to design a greenfield system as microservices. When would you argue for a modular monolith instead, and how would you defend it?
medium