Bounded Context in Microservices: Definition and Usage
bounded context is a clear boundary within a microservices system where a specific domain model applies consistently. It defines the limits where particular terms, rules, and logic have a single, unambiguous meaning to avoid confusion across different parts of the system.How It Works
Imagine a large company with different departments like sales, shipping, and support. Each department uses its own words and rules to do its job. A bounded context is like drawing a fence around each department so everyone inside agrees on what words mean and how things work.
In microservices, this means each service owns its own data and logic, avoiding misunderstandings when different teams or systems talk to each other. This separation helps keep the system organized and easier to change without breaking other parts.
Example
This example shows two bounded contexts: Order Management and Customer Support. Each has its own simple class representing a customer, but with different details and rules.
class OrderManagementCustomer { constructor(id, name) { this.id = id; this.name = name; } getDisplayName() { return `${this.name} (ID: ${this.id})`; } } class CustomerSupportCustomer { constructor(email, issueCount) { this.email = email; this.issueCount = issueCount; } canOpenNewTicket() { return this.issueCount < 5; } } // Usage const orderCustomer = new OrderManagementCustomer(123, 'Alice'); const supportCustomer = new CustomerSupportCustomer('alice@example.com', 2); console.log(orderCustomer.getDisplayName()); console.log(supportCustomer.canOpenNewTicket());
When to Use
Use bounded contexts when your system has different parts that use the same words but mean different things, or when teams work independently on different features. This helps avoid confusion and bugs.
For example, in an e-commerce platform, the Inventory team and the Billing team might both use the term "order," but they handle it differently. Defining bounded contexts lets each team work clearly within their own rules.
Key Points
- A bounded context defines clear boundaries for a domain model.
- It prevents confusion by keeping terms and rules consistent inside the boundary.
- Each microservice usually represents one bounded context.
- Helps teams work independently and reduces bugs.