We use analytics cookies.We use cookies to analyze site traffic and improve your experience.
Loading...
System Design Masterclass · Microservices Architecture
Free preview: the first 2 parts of this 7-part lesson.
Already bought the course? Sign in to read the whole lesson.
this lesson is part of
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design courseread the rest of this lesson in
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design coursein Microservices Architecture
This lesson is part of the full System Design Masterclass. Unlock it along with 769 other lessons for a one-time payment.
One-time payment by card or PayPal
Get Lifetime AccessOne-time payment: UPI, card or netbanking
Get Lifetime Access18 Foundation lessons are free, no payment needed. Try them now
It is easy and good
“It is really nice and good Please join the course”
You're building an e-commerce system. A shopping cart contains items. Each item has a quantity, a price, and a discount. The cart has a total, a tax calculation, and a shipping estimate.
Now someone adds an item. You need to update the item list, recalculate the total, recalculate tax, and recalculate shipping. What if the total update succeeds but the tax recalculation fails? You have an inconsistent cart, the total doesn't match the actual items.
Or worse: two concurrent requests modify the cart. Request A adds an item. Request B removes an item. Both read the cart at the same time, both calculate a new total, both write. One update overwrites the other. Items and total are now out of sync.
The Aggregate pattern prevents this by treating the cart and all its related objects as a single unit. You can't modify items directly. You go through the cart, and the cart ensures everything stays consistent. It's the transactional boundary for a cluster of related objects.
Eric Evans introduced this concept in his 2003 book "Domain-Driven Design." Vaughn Vernon later expanded on it in "Implementing Domain-Driven Design" with detailed sizing guidance. The aggregate is the most misunderstood and most important DDD building block.
An Aggregate is a cluster of domain objects that are treated as a single unit for data changes. Every aggregate has an Aggregate Root, the single entry point through which all modifications must pass.
| Concept | Definition | Shopping Cart Example |
|---|---|---|
| Aggregate | A cluster of related objects treated as one unit | Cart + CartItems + Discounts |
| Aggregate Root | The single entry point for all changes | The Cart object itself |
| Entity | An object with identity that changes over time | CartItem (has an ID, quantity changes) |
| Value Object | An object defined by its attributes, not identity | Money(amount: 29.99, currency: "USD") |
| Invariant | A business rule that must always be true | Total = sum of (item.price * item.quantity) - discounts |
| Consistency Boundary | Changes within an aggregate are immediately consistent | Adding an item recalculates totals atomically |
Rules of aggregates:
The domain-driven design lesson runs rule 3 against a real PostgreSQL database: two clerks race to add a line to one order, and a version column on the root turns a silently broken limit into one visible conflict.