| Users / Requests | What Changes? |
|---|---|
| 100 users | Single instance per microservice; aggregates handle entity consistency locally; simple database transactions. |
| 10,000 users | Multiple instances of microservices; aggregates partitioned by business domain; database connection pooling; caching introduced. |
| 1,000,000 users | Microservices scaled horizontally with load balancers; aggregates sharded by entity ID ranges; eventual consistency patterns; asynchronous messaging between services. |
| 100,000,000 users | Global distribution of microservices; aggregates split further; CQRS and event sourcing used; cross-region replication; advanced caching and CDN for read-heavy data. |
Aggregates and entities in Microservices - Scalability & System Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
The database managing aggregates becomes the first bottleneck. Aggregates enforce consistency boundaries, so heavy write/read loads on entities cause contention and slow transactions.
- Horizontal Scaling: Add more microservice instances behind load balancers to distribute traffic.
- Sharding Aggregates: Partition aggregates by entity ID or business domain to reduce contention.
- Caching: Use in-memory caches for read-heavy entity data to reduce database load.
- Eventual Consistency: Use asynchronous messaging and event sourcing to decouple aggregates and improve scalability.
- Database Replication: Use read replicas to scale read queries.
- CQRS Pattern: Separate read and write models to optimize performance.
- At 1M users, assuming 10 requests per user per minute = ~166,000 requests/sec.
- Each microservice instance handles ~3,000 concurrent connections; need ~60 instances.
- Database handles ~5,000 QPS; sharding needed to distribute load.
- Storage depends on entity size; for 1M entities at 1KB each = ~1GB; grows with history if event sourcing used.
- Network bandwidth must support request and event message traffic; estimate 1 Gbps or more.
Start by explaining aggregates as consistency boundaries in microservices. Discuss how entities inside aggregates are managed. Then describe scaling challenges as user load grows. Finally, propose solutions like sharding, caching, and asynchronous communication, linking each to the bottleneck it solves.
Your database handles 1000 QPS. Traffic grows 10x. What do you do first?
Answer: Introduce read replicas and caching to reduce load on the primary database. Also consider sharding aggregates to distribute write load.
Practice
Solution
Step 1: Understand aggregate root responsibility
The aggregate root is the main entity that manages all changes inside its aggregate to ensure consistency.Step 2: Eliminate unrelated options
Options A, B, and D describe roles unrelated to aggregate roots in microservices.Final Answer:
It controls all changes within the aggregate to keep data consistent. -> Option DQuick Check:
Aggregate root controls changes = C [OK]
- Confusing aggregate root with database or UI component
- Thinking aggregate root stores unrelated data
- Assuming aggregate root handles external service data
Solution
Step 1: Identify the aggregate root
In an order system, the Order is the root entity controlling related entities like OrderItems and PaymentDetails.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.Final Answer:
Order (root) -> OrderItems (entities) -> PaymentDetails (entity) -> Option AQuick Check:
Root entity is Order controlling others = A [OK]
- Assigning wrong entity as root
- Treating all entities as roots
- Ignoring aggregate boundaries
Customer with entities Address and Order, which operation should only be performed through Customer?Solution
Step 1: Understand aggregate root control
The aggregate rootCustomercontrols all changes to its entities likeAddressandOrderto maintain consistency.Step 2: Identify allowed operations
Adding a newAddressshould go throughCustomer. Direct updates or deletes bypassing root break consistency.Final Answer:
Adding a newAddressvia theCustomeraggregate root -> Option CQuick Check:
Changes go through root entity = A [OK]
- Updating entities directly without root
- Deleting entities independently
- Confusing querying with updating
Invoice and entities LineItem. The code allows direct modification of LineItem without Invoice. What is the main problem?Solution
Step 1: Identify aggregate root role in consistency
The aggregate rootInvoiceensures all changes toLineItemare consistent and valid.Step 2: Analyze direct modification impact
Directly modifyingLineItembypassesInvoice, risking inconsistent or invalid data.Final Answer:
Data consistency may break because changes bypass the aggregate root. -> Option BQuick Check:
Bypassing root risks consistency = B [OK]
- Assuming direct access improves design
- Ignoring consistency importance
- Confusing performance with correctness
Solution
Step 1: Apply aggregate root principle for consistency
Cart as aggregate root should control all changes to CartItem and Discount to keep data consistent.Step 2: Consider scalability and design best practices
Centralizing changes through Cart allows easier management and scaling of the microservice without data conflicts.Step 3: Evaluate other options
Options A and C risk inconsistency; B ignores aggregate design and can cause complexity.Final Answer:
Make Cart the aggregate root controlling all CartItem and Discount changes. -> Option AQuick Check:
Aggregate root controls changes for consistency and scale = D [OK]
- Allowing independent updates breaking consistency
- Splitting tightly coupled entities into separate services
- Ignoring aggregate design principles
