CQRS way
- Typical architecture pros & cons
- CQRS concept
- Code example
- Pros & cons
- Results
Based on Greg Young's notes and OMD Vision team
experience
Typical architecture
- ORM (NH, EF)
- MS SQL
- DDD (Entity, Repository, Service)
- WebApi CRUD (REST) services
What the problem?
Problems
- You can’t do DDD
- Anemic domain model
- No aggregates
- CRUD
- As result Business logic is blur (server, client, dev mind)
- Lazy loading
- Query optimization is punishment
- One repository method for different reason
- Leads to entity pollution
- Data loss due to storing last state
- Doesn’t scale
- Very expensive horizontal scale, and limited vertical
- Transaction and locks for readers
Why do we use
- Experience, it works at least
- Devs understand it
- Mature
- You can google any problem
- Tooling
- Microsoft, Oracle propagation
- They benefit from this architecture!
- There isn't any other option
Free your mind
- Save state vs history
-
We are lucky,
CQRS started to live in
.NET
-
Greg Young and Jonathan Oliver are fathers
-
CQRS
completely different approach
- With pros and cons, all about trade off
- No silver bullet
-
Time for app to lose weight
How?
CQRS code examples:
- Controller write
- Command
- Command handler
- Aggregate
- Event
- Projection
- Controller read
- Write database
Controller write
Command
Command handler
Aggregate
Event
Projection
Controller read
Write database
OMD Vision results
- MS SQL 2012, ASP.NET MVC 4, ServiceStack.OrmLite 3
- Read request ~ 30ms (prod) ~ 15ms (local)
- Write request
- 100ms (prod) 30ms (local)
- 200ms fat aggregate (prod) 60ms(local)
- 1.3m events
- 0.5m commits
- The most fat aggregates:
- 1st = 14 054 events
- 2nd = 7 778 events
- 3rd = 7 449 events
- 65 projections 110 tables
- Full rebuild duration ~1d
Questions
Links