Bird
Raised Fist0
Microservicessystem_design~25 mins

Aggregates and entities 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: Order Management System
Design the domain model focusing on aggregates and entities for order management. Include microservice boundaries and data consistency approach. Out of scope: payment processing, shipping logistics.
Functional Requirements
FR1: Allow customers to create and manage orders
FR2: Each order contains multiple items with quantity and price
FR3: Support updating order status (e.g., pending, shipped, delivered)
FR4: Ensure data consistency within orders and their items
FR5: Allow querying orders by customer and status
Non-Functional Requirements
NFR1: Handle 1000 concurrent order creations per minute
NFR2: API response time p99 < 300ms
NFR3: System availability 99.9%
NFR4: Data consistency within an order must be strong
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
Order Service
Order Item Entity
Customer Service (external)
Database with transaction support
API Gateway
Design Patterns
Aggregate pattern for grouping entities
Domain-Driven Design (DDD) principles
Eventual consistency vs strong consistency
Transactional boundaries within aggregates
Reference Architecture
Client
  |
  v
API Gateway
  |
  v
Order Service (Aggregate Root: Order)
  |-- Order Items (Entities)
  |
Database (Transactional)

External Customer Service (for customer data)
Components
Order Service
Spring Boot microservice with REST API
Manages orders as aggregate roots, handles business logic and consistency
Order Item Entity
JPA Entities within Order Service
Represents individual items within an order, part of the aggregate
Database
PostgreSQL with ACID transactions
Stores orders and items ensuring strong consistency within aggregates
API Gateway
Kong or AWS API Gateway
Routes client requests to appropriate microservices
Customer Service
External microservice
Manages customer data, queried by Order Service as needed
Request Flow
1. Client sends create order request to API Gateway
2. API Gateway forwards request to Order Service
3. Order Service validates request and creates Order aggregate with Order Items
4. Order Service saves aggregate in database within a transaction
5. Order Service returns confirmation to client
6. Client queries order status via API Gateway to Order Service
7. Order Service fetches order and items from database and returns data
Database Schema
Entities: - Order (Aggregate Root): order_id (PK), customer_id, status, created_at, updated_at - OrderItem: item_id (PK), order_id (FK), product_id, quantity, price Relationships: - One Order has many OrderItems (1:N) - Order is aggregate root; OrderItems cannot exist without Order - Customer data referenced by customer_id but stored externally
Scaling Discussion
Bottlenecks
Database write contention on orders during high concurrency
Order Service CPU/memory limits under load
API Gateway throughput limits
Solutions
Use database connection pooling and optimize indexes for order queries
Scale Order Service horizontally with stateless instances behind load balancer
Use API Gateway autoscaling and caching for read-heavy endpoints
Interview Tips
Time: 10 minutes for requirements and clarifying questions, 15 minutes for design and data modeling, 10 minutes for scaling and trade-offs discussion
Explain aggregate concept as consistency boundary
Justify why Order is aggregate root and Order Items are entities inside it
Discuss transactional consistency within aggregate
Mention microservice boundaries and external customer service
Describe scaling strategies and bottleneck mitigation

Practice

(1/5)
1. In microservices, what is the main role of an aggregate root entity?
easy
A. It acts as a database for all microservices.
B. It stores unrelated data from different services.
C. It handles user interface rendering.
D. It controls all changes within the aggregate to keep data consistent.

Solution

  1. Step 1: Understand aggregate root responsibility

    The aggregate root is the main entity that manages all changes inside its aggregate to ensure consistency.
  2. Step 2: Eliminate unrelated options

    Options A, B, and D describe roles unrelated to aggregate roots in microservices.
  3. Final Answer:

    It controls all changes within the aggregate to keep data consistent. -> Option D
  4. Quick Check:

    Aggregate root controls changes = C [OK]
Hint: Aggregate root manages changes inside its group [OK]
Common Mistakes:
  • Confusing aggregate root with database or UI component
  • Thinking aggregate root stores unrelated data
  • Assuming aggregate root handles external service data
2. Which of the following correctly represents an aggregate in a microservice domain model?
easy
A. Order (root) -> OrderItems (entities) -> PaymentDetails (entity)
B. OrderItems (root) -> Order -> PaymentDetails
C. PaymentDetails (root) -> Order -> OrderItems
D. Order -> PaymentDetails -> OrderItems (all roots)

Solution

  1. Step 1: Identify the aggregate root

    In an order system, the Order is the root entity controlling related entities like OrderItems and PaymentDetails.
  2. Step 2: Check the hierarchy correctness

    Order (root) -> OrderItems (entities) -> PaymentDetails (entity) shows Order as root with related entities under it, which is correct. Other options misplace roots or treat all as roots.
  3. Final Answer:

    Order (root) -> OrderItems (entities) -> PaymentDetails (entity) -> Option A
  4. Quick Check:

    Root entity is Order controlling others = A [OK]
Hint: Root entity leads related entities in aggregate [OK]
Common Mistakes:
  • Assigning wrong entity as root
  • Treating all entities as roots
  • Ignoring aggregate boundaries
3. Given the aggregate root Customer with entities Address and Order, which operation should only be performed through Customer?
medium
A. Deleting Order independently from Customer
B. Directly updating an Order without Customer involvement
C. Adding a new Address via the Customer aggregate root
D. Querying Order data directly from the database

Solution

  1. Step 1: Understand aggregate root control

    The aggregate root Customer controls all changes to its entities like Address and Order to maintain consistency.
  2. Step 2: Identify allowed operations

    Adding a new Address should go through Customer. Direct updates or deletes bypassing root break consistency.
  3. Final Answer:

    Adding a new Address via the Customer aggregate root -> Option C
  4. Quick Check:

    Changes go through root entity = A [OK]
Hint: All changes pass through aggregate root only [OK]
Common Mistakes:
  • Updating entities directly without root
  • Deleting entities independently
  • Confusing querying with updating
4. You have a microservice with an aggregate root Invoice and entities LineItem. The code allows direct modification of LineItem without Invoice. What is the main problem?
medium
A. Performance will improve due to direct access.
B. Data consistency may break because changes bypass the aggregate root.
C. It will reduce network calls between services.
D. It simplifies the codebase without side effects.

Solution

  1. Step 1: Identify aggregate root role in consistency

    The aggregate root Invoice ensures all changes to LineItem are consistent and valid.
  2. Step 2: Analyze direct modification impact

    Directly modifying LineItem bypasses Invoice, risking inconsistent or invalid data.
  3. Final Answer:

    Data consistency may break because changes bypass the aggregate root. -> Option B
  4. Quick Check:

    Bypassing root risks consistency = B [OK]
Hint: Bypass root risks data consistency [OK]
Common Mistakes:
  • Assuming direct access improves design
  • Ignoring consistency importance
  • Confusing performance with correctness
5. You design a microservice for a shopping cart system. The cart is an aggregate root with entities like CartItem and Discount. Which design choice best ensures data consistency and scalability?
hard
A. Make Cart the aggregate root controlling all CartItem and Discount changes.
B. Use a single database table for Cart, CartItem, and Discount without aggregates.
C. Store CartItem and Discount in separate microservices with no coordination.
D. Allow CartItem and Discount to be updated independently without Cart involvement.

Solution

  1. Step 1: Apply aggregate root principle for consistency

    Cart as aggregate root should control all changes to CartItem and Discount to keep data consistent.
  2. Step 2: Consider scalability and design best practices

    Centralizing changes through Cart allows easier management and scaling of the microservice without data conflicts.
  3. Step 3: Evaluate other options

    Options A and C risk inconsistency; B ignores aggregate design and can cause complexity.
  4. Final Answer:

    Make Cart the aggregate root controlling all CartItem and Discount changes. -> Option A
  5. Quick Check:

    Aggregate root controls changes for consistency and scale = D [OK]
Hint: Aggregate root controls related entities for consistency and scale [OK]
Common Mistakes:
  • Allowing independent updates breaking consistency
  • Splitting tightly coupled entities into separate services
  • Ignoring aggregate design principles