In the previous posts, we learned about:
- Entities
- Value Objects
These help us model the business world.
But a question still remains:
Why do these objects exist?
The answer is simple:
To enforce business behavior correctly.
And business behavior is usually defined by:
Business Rules and Invariants.
Understanding them is one of the biggest shifts from writing code to designing systems.
What Is a Business Rule?
A Business Rule is:
A rule that defines how the business expects the system to behave.
Examples:
BookMyShow
A seat cannot be sold twice.
Uber
A driver cannot complete a ride that never started.
Amazon
The final order amount must be correct.
Banking
An account cannot withdraw more than its available balance.
These rules do not come from code.
They come from the business.
Software Does Not Invent Rules
Many beginners think:
Business Rules
↓
Code
Reality is:
Business
↓
Business Rules
↓
Code
The software's responsibility is not to create rules.
The software's responsibility is to enforce them.
What Is an Invariant?
An Invariant is:
A business rule that must always remain true.
Think of it as a rule the business cannot afford to violate.
Example
Suppose a movie seat is booked.
Invariant:
One seat
=
One booking
Valid:
Seat A1
→ Booking #101
Invalid:
Seat A1
→ Booking #101
Seat A1
→ Booking #102
The invariant has been violated.
Business Rule vs Invariant
These terms are related but not identical.
Business Rule
Describes expected behavior.
Example:
Customers can cancel orders before shipment.
Invariant
Must never become false.
Example:
A shipped order cannot become unshipped.
Relationship:
Business Rules
↓
Some become critical enough to become
↓
Invariants
Every invariant is a business rule.
Not every business rule is an invariant.
Why Invariants Matter More Than Features
Imagine a payment system with:
- beautiful UI
- excellent APIs
- fast processing
But:
Money randomly disappears.
Would anyone trust it?
No.
Because correctness matters more than features.
A system that is correct but simple is useful.
A system that is feature-rich but incorrect is dangerous.
Every Domain Has Invariants
Ride Sharing
A driver cannot have two active rides simultaneously.
Food Delivery
An order cannot be delivered before it is accepted.
Banking
Balance cannot become negative beyond allowed limits.
E-Commerce
Order total must equal item total plus charges.
BookMyShow
A seat cannot be booked twice.
Different systems.
Same concept.
Invariants Are Hidden Inside Requirements
Requirements rarely say:
Invariant:
...
Instead they appear indirectly.
Requirement:
Users can book seats for a movie.
A beginner sees:
Seat
Movie
Booking
An experienced designer asks:
Can multiple users book the same seat?
They're searching for invariants.
Strong Designers Look for "Never"
A useful trick:
Ask:
What should NEVER happen?
Examples:
A payment should never be processed twice.
A cancelled ride should never become active again.
A delivered order should never return to CREATED state.
These statements often reveal invariants immediately.
Real Example: Order Lifecycle
Consider an order.
CREATED
↓
PAID
↓
SHIPPED
↓
DELIVERED
One invariant might be:
DELIVERED
cannot occur before
SHIPPED
Another might be:
PAID
cannot return to
CREATED
Notice something important.
The important part is not the fields.
The important part is protecting valid business behavior.
Weak LLD Thinking
What classes should I create?
Strong LLD Thinking
What business rules must never break?
This question often leads to far better designs.
Why This Matters Later
As systems become larger, a new question appears:
Who is responsible for protecting these invariants?
That question eventually leads us to:
Aggregates
Consistency Boundaries
Aggregate Roots
We'll explore those in upcoming posts.
For now, focus on identifying invariants first.
The Most Important Insight
Entities describe:
Who exists
Value Objects describe:
What something means
Business Rules describe:
How the business operates
Invariants describe:
What must never be violated
The strongest domain models are built around protecting invariants.
One-Line Takeaway
An invariant is a business rule that must always remain true, and discovering these rules is one of the most important skills in Domain Modeling.
Top comments (0)