Bird
Raised Fist0
Microservicessystem_design~25 mins

Bounded context mapping in Microservices - System Design Exercise

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Design: Bounded Context Mapping for Microservices
Focus on designing the bounded context boundaries, communication patterns, and data ownership. Out of scope: detailed implementation of each microservice business logic.
Functional Requirements
FR1: Identify clear boundaries between different business domains in a microservices architecture
FR2: Define how different bounded contexts communicate and share data
FR3: Support independent development and deployment of services
FR4: Ensure data consistency within each bounded context
FR5: Handle integration between contexts with minimal coupling
Non-Functional Requirements
NFR1: System must support up to 100 microservices
NFR2: Latency for inter-service communication should be under 100ms p99
NFR3: Availability target of 99.9% uptime for critical services
NFR4: Data consistency within bounded contexts must be strong; eventual consistency allowed across contexts
NFR5: Scalable to handle 10,000 concurrent users
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
API Gateway or Service Mesh for routing and communication
Event Bus or Message Broker for asynchronous integration
Databases owned by each bounded context
Domain Models encapsulated within each context
Anti-Corruption Layers to translate between contexts
Design Patterns
Domain-Driven Design (DDD) for defining bounded contexts
Event-Driven Architecture for integration
Saga Pattern for distributed transactions
API Composition for query aggregation
Anti-Corruption Layer to isolate contexts
Reference Architecture
  +----------------+       +----------------+       +----------------+
  |  Bounded       |       |  Bounded       |       |  Bounded       |
  |  Context A     |       |  Context B     |       |  Context C     |
  |  (Microservice)|       |  (Microservice)|       |  (Microservice)|
  +-------+--------+       +-------+--------+       +-------+--------+
          |                        |                        |
          | REST/gRPC              | Event Bus (Kafka)      | REST/gRPC
          |                        |                        |
  +-------v--------+       +-------v--------+       +-------v--------+
  | Database A     |       | Event Broker   |       | Database C     |
  +----------------+       +----------------+       +----------------+

Legend:
- Each bounded context owns its database
- Communication between contexts uses sync or async methods
- Anti-Corruption Layer (not shown) translates data formats
Components
Bounded Context Microservices
Any microservice framework (Spring Boot, Node.js, etc.)
Encapsulate business logic and data for a specific domain
Databases per Context
Relational or NoSQL databases (PostgreSQL, MongoDB)
Store data owned exclusively by each bounded context
API Gateway / Service Mesh
Kong, Istio, or Envoy
Route requests and manage communication between services
Event Bus / Message Broker
Kafka, RabbitMQ
Enable asynchronous communication and event-driven integration
Anti-Corruption Layer
Adapter or Translator components
Translate and protect bounded contexts from external models
Request Flow
1. Client sends request to API Gateway
2. API Gateway routes request to appropriate bounded context microservice
3. Microservice processes request using its own database
4. If data from another context is needed, microservice calls that context via API or listens to events
5. For asynchronous updates, microservices publish events to Event Bus
6. Other contexts subscribe to relevant events and update their own data accordingly
7. Anti-Corruption Layer translates data formats between contexts to avoid leakage
Database Schema
Entities are defined per bounded context with no shared tables. Example: - Context A: Customer(id, name, contact_info) - Context B: Order(id, customer_id, order_date, status) - Context C: Inventory(id, product_id, quantity) Relationships between contexts are handled via IDs and events, not direct foreign keys.
Scaling Discussion
Bottlenecks
Tight coupling between bounded contexts causing deployment delays
Synchronous calls causing high latency and cascading failures
Event bus overload with high event volume
Database contention within a single context
Data inconsistency across contexts due to eventual consistency
Solutions
Enforce strict bounded context boundaries and use Anti-Corruption Layers
Prefer asynchronous communication with retries and circuit breakers
Partition event topics and scale brokers horizontally
Use database sharding or read replicas within contexts
Implement Saga pattern and idempotent event handlers to maintain consistency
Interview Tips
Time: Spend 10 minutes understanding domain boundaries, 15 minutes designing communication and data ownership, 10 minutes discussing scaling and consistency, 10 minutes for Q&A
Explain importance of clear bounded contexts for team autonomy
Discuss trade-offs between synchronous and asynchronous communication
Highlight data ownership and isolation per context
Describe patterns like Anti-Corruption Layer and Saga for integration
Address scaling challenges and solutions realistically

Practice

(1/5)
1. What is the main purpose of bounded context mapping in microservices architecture?
easy
A. To divide a system into clear, manageable parts with defined boundaries
B. To merge all services into a single large application
C. To increase the number of database tables in a system
D. To remove communication between different teams

Solution

  1. Step 1: Understand bounded context concept

    Bounded context means splitting a system into parts that have clear boundaries and responsibilities.
  2. Step 2: Identify the main goal of mapping

    Mapping helps teams work independently and reduces complexity by defining these boundaries.
  3. Final Answer:

    To divide a system into clear, manageable parts with defined boundaries -> Option A
  4. Quick Check:

    Bounded context = clear system parts [OK]
Hint: Bounded context means clear boundaries in system parts [OK]
Common Mistakes:
  • Thinking bounded context merges services
  • Confusing bounded context with database design
  • Assuming it removes team communication
2. Which of the following correctly represents a relationship type in bounded context mapping?
easy
A. Customer/Supplier means contexts never communicate
B. Shared Kernel means two contexts share a small part of their domain model
C. Open Host Service means one context copies all data from another context
D. Conformist means contexts ignore each other's models completely

Solution

  1. Step 1: Review relationship types in bounded context mapping

    Shared Kernel means two contexts share a small, common part of their domain model to stay consistent.
  2. Step 2: Check other options for correctness

    Open Host Service is about providing a stable interface, not copying all data. Customer/Supplier implies communication. Conformist means one context adapts to another's model, not ignoring it.
  3. Final Answer:

    Shared Kernel means two contexts share a small part of their domain model -> Option B
  4. Quick Check:

    Shared Kernel = shared small domain part [OK]
Hint: Shared Kernel means sharing a small model part [OK]
Common Mistakes:
  • Confusing Open Host Service with data copying
  • Thinking Customer/Supplier means no communication
  • Believing Conformist ignores other models
3. Given two bounded contexts A and B where A is the Customer and B is the Supplier, what is the expected interaction pattern?
medium
A. Context B provides services that Context A consumes
B. Context A adapts to B's model without changes
C. Contexts A and B share the same database schema
D. Contexts A and B never exchange data or messages

Solution

  1. Step 1: Understand Customer/Supplier relationship

    In this pattern, the Supplier context offers services or data that the Customer context uses.
  2. Step 2: Analyze options

    Context A adapts to B's model without changes describes Conformist, not Customer/Supplier. Contexts A and B share the same database schema is incorrect because sharing the same database schema breaks bounded context boundaries. Contexts A and B never exchange data or messages contradicts the relationship.
  3. Final Answer:

    Context B provides services that Context A consumes -> Option A
  4. Quick Check:

    Customer/Supplier = Supplier provides services [OK]
Hint: Supplier provides, Customer consumes services [OK]
Common Mistakes:
  • Mixing Customer/Supplier with Conformist
  • Assuming shared database schema
  • Thinking no data exchange happens
4. You have two bounded contexts with a Conformist relationship, but the Customer context is modifying the Supplier's domain model directly. What is the problem?
medium
A. The Conformist pattern requires sharing the same database schema
B. The Supplier context must always copy the Customer's model
C. Both contexts should merge into one to avoid conflicts
D. The Customer context should not change the Supplier's model; it should adapt to it

Solution

  1. Step 1: Understand Conformist relationship rules

    In Conformist, the Customer context adapts to the Supplier's model but does not modify it directly.
  2. Step 2: Identify the error in modifying Supplier's model

    Modifying the Supplier's model breaks the boundary and can cause inconsistencies.
  3. Final Answer:

    The Customer context should not change the Supplier's model; it should adapt to it -> Option D
  4. Quick Check:

    Conformist means adapt, not modify [OK]
Hint: Customer adapts Supplier model, does not modify it [OK]
Common Mistakes:
  • Thinking Supplier copies Customer model
  • Merging contexts unnecessarily
  • Assuming shared database schema is required
5. You are designing a large e-commerce system with multiple teams. How should you apply bounded context mapping to ensure scalability and team independence?
hard
A. Ignore context boundaries and let teams decide data sharing ad hoc
B. Combine all domains into one large context to simplify communication
C. Define clear bounded contexts for domains like Orders, Payments, and Inventory, and map their relationships explicitly
D. Allow teams to share a single database schema to avoid data duplication

Solution

  1. Step 1: Identify the need for clear domain boundaries

    Large systems benefit from dividing domains like Orders, Payments, and Inventory into separate bounded contexts.
  2. Step 2: Map relationships explicitly for team independence

    Explicit mapping helps teams understand dependencies and communicate properly without tight coupling.
  3. Step 3: Avoid combining domains or sharing schemas

    Combining domains or sharing schemas increases complexity and reduces scalability.
  4. Final Answer:

    Define clear bounded contexts for domains like Orders, Payments, and Inventory, and map their relationships explicitly -> Option C
  5. Quick Check:

    Clear contexts + explicit mapping = scalable teams [OK]
Hint: Clear contexts and explicit maps enable scalable teams [OK]
Common Mistakes:
  • Merging all domains into one context
  • Sharing a single database schema
  • Ignoring boundaries and ad hoc sharing