3. What’s going on around the world
Connected via Internet
Social networks
Mobile Devices
News
Forums & Blogs
e-commerce
Search Engines
3
5. Facebook
How many friends do you have?
Thousand?
Hundred?
A few?
Do you like to
Post new status?
Like?
Comment?
Share?
There types of people:
Social people
Quite normal
Look around
5
Activity
8. Problem?
In traditional systems, both commands (updates to data) and queries
(request for data) are executed against a same set of entities of a
single data repository.
Mismatch between read and write entity
Create a lot of read-only model that presents the result from joining multi-
tables
The system will be slow down if handle a large of requests since the
read/write action be handled in the same services.
The number of reads and writes maybe very different in the enterprise
system
Hard to manage security and permission because each entity is subject
to both read and write operations
8
9. Quote
A single model cannot be appropriate for
reporting, searching and transactional behavior.
9
Greg Young, 2008
10. What?
CQRS
CQRS: Command & Query Responsibility Segregation is simply the creation
of two objects where there was previously only one. The separation occurs
based upon whether the methods are a command or a query. Greg
Young
CQRS enables us to optimize the read and write side independently
CQRS is an architectural style that is often enabling of DDD
Implementation of the model exists on the write side; it encapsulates the
complex business logic
DDD divides large, complex systems into more manageable units known as
bounded context
10
11. Why? 11
Scalability: by separating the read side and the write side into separate
models, we have ability to scale each side independently
Reduced complexity: designing and implementing objects that are
responsible for both reading and writing data can exacerbate the
complexity. It becomes harder to deal with difficult issues such as multiple
users, shared data, performance, transactions, consistency, and stale
data with the same model.
Flexibility: easier to make changes on both sides that doesn’t impact the
other
Focus on the business: separating the different concerns into the read
side and the write side is a solution that is more adaptable in the face of
changing business requirements
Facilitates building task-based UIs: design the UI to send the command to
the domain instead of CRUD operations
12. When? 12
Collaborative domains: multiple operations are performed in parallel on
the same data
Performance: when the read/write ratio is very high, and when horizontal
scaling is required
Business rule change regularly: updating the model of a side doesn’t
affect to the other
Work independently: one team of developers can focus on the complex
domain model that is part of the write model, and another less
experienced team can focus on the read model and the user interfaces
Use with task-based UI: with complex domain models, and for teams
already familiar with DDD techniques.
13. When NOT? 13
Business rules are simple
Simple, static bounded context: less likely to warrant the up-front
investment in detailed analysis, modeling, and complex implementation
Simple CRUD-style user interface and the related data access operations
are sufficient
14. Related Patterns 14
Event Sourcing Pattern: simplify tasks in complex domains; improve
performance, scalability, and responsiveness; provide consistency for
transactional data; and maintain full audit trails and history that may
enable compensating actions
Materialized View Pattern: The read model of a CQRS implementation
may contain materialized views of the write model data, or the read
model may be used to generate materialized views
15. CQRS & Event Sourcing 15
Event Sourcing: Event sourcing is a way of persisting your application's
state by storing the history that determines the current state of your
application
Audit trails
Integration with other sub-systems
Product troubleshot
Fixing errors
Testing
ES is a great pattern to use to implement the link between the thing that
writes and the thing that reads
18. Enterprise Service Bus 18
ESB: Enterprise Service Bus is an infrastructure piece of software than a
pattern itself
Implementing different message patterns:
Publish / Subscribe
Request / Reply
Dead after channel
Peer to peer
Broad cast to all
19. Sherpa Project
An enterprise social network
Recruitment
Resource planning
Survey system
Training
Library
Events
19
Case Study
20. Resource planning
Member Availability
Member Time-off
Public Holiday
Cost Center
Shift Position
Roster & Shift
Timesheet
Payroll Engine
20
Case Study
21. Member Time-off
A member can belong to multi projects
Member can view their list of time-off by time range
Any manager can view list of all member’s time off by time range in
a specific project/group
Manager can view only time-off with pending status
Manager can approve/reject their time-off
21
Requirements
22. Member Time-off
A member can belong to multi
projects/Group
Manager can view list in any
projects/group
22
Use case diagram
uc Member TimeOff
Member Time-off
View my list
View detail
Create
Update
View list in group
Approve/Reject
Send Notification
Member
Manager
«invokes»
24. Member Time-off 24
Query needed
Query results contain only data – no logic
Give me all my leaves this year
Give me all my leaves this month
Give me all team member’s leaves for project A
Give me all team member’s leaves for project B for only this week
Give me all leaves of project C that are in pending
Give me all pending leaves of project D in next two weeks
Give me all member M’s leaves of project E last month
25. NoSQL Storage 25
Azure storage
Have no relationship
Index by Partition Key & Row Key
Searching priority from left to right as string
1
11
111
2
No support paging (limitation)
26. Member Time-off
My Time-Off By pending By Groups
PartitionKey NetworkId_ AzureQueryType NetworkId_ AzureQueryType NetworkId_ AzureQueryType
RowKey MemberId_Date_MemberTimeOffId GroupId_Pending_Date_MemberTimeOffId GroupId_Date_MemberId_MemberTimeOffId
MemberTimeOffId int int int
MemberId int int int
StartDate DateTime DateTime DateTime
EndDate DateTime DateTime DateTime
TimeZoneId string string string
TimeOffType string string string
Status string string string
Username string string string
DisplayName string string string
Comment string string string
26
Entity Diagram – Read part
27. Member Time-off 27
Query Example
My Time-Off: MemberId_Date_MemberTimeOffId
19433_20151021_1
19433_20151022_2
19433_20151023_3
19433_20151024_4
19433_20151025_5
19433_20151026_6
19433_20151027_7
19433_20151028_8
19434_20151020_9
19433_20151021_1
19433_20151022_2
19433_20151023_3
19433_20151024_4
19433_20151025_5
19433_20151026_6
19433_20151027_7
19433_20151028_8
19433_20151022_2
19433_20151023_3
19433_20151024_4
19433_20151025_5
By member id: 19433 From date range: 22 Oct – 25 Oct
Row key: 19433_ Row key: >= 19433_20151022 && <= 19433_20151025
28. Member Time-off 28
Sequence diagram: Create
sd TimeOff.Create new
Member
SherpaWeb Sherpa APIV2 MemberTimeOffService MemberTimeOffRepository Database Sherpa.Application.Agents
memberTimeOff Id()
CreateMemberTimeOff()
«success»
Publish CreateMemberTimeOffMessage()
InsertMemberTimeOff()
Create Time-Off()
«success»
Insert(memberTimeOff)
POST: api/memberTimeOffs()
memberTimeOff Id()
new memberTimeOff()
Member Create Time-off
35. Lesion learn 35
Developer works independently
Requirements changed or added not affected much to design and code
Design easily: What we want to get is a read model
Big company in the world used, are using it
Just a few company in Vietnam applied it
Proud to be the first one in TMA design it & implemented it
This day last year I had a presentation about Modern Web
Hope that this day next year, we will see more people understanding, more project applying this
Usually talking about definition at the second slide
But today
We’re living in the world that everyone is connected to Internet
When saying “connected” , thinking about social network
And mobiles help us to connect easily (show some funny numbers)
Besides, this is daily activity that I believe we’re using most
There is plenty of social network platform out there but I’ll show you some of famous ones.
Some information about social networks. Then pick facebook at one example
I believe
Number of friends, then number of feeds
Talk about the habit of people when using facebook
How they react with other friends or other news:
How many views to like? To comment?
Number of friends, then number of feeds
Talk about the habit of people when using facebook
How they react with other friends or other news:
How many views to like? To comment?
There is less than 5% people react on a feed
We read more than we write
Quick develop by scaffolding
Hard to scale out database, to distribute system
Quick develop by scaffolding
Hard to scale out database, to distribute system
A bounded context is map with the business components
adding a new query to support a new report screen in the UI, when you can be confident that you won't have any impact on the behavior of the business logic. On the write side, having a model that concerns itself solely with the core business logic in the domain means that you have a simpler model to deal with than a model that includes read logic as well.
The write model has a full command-processing stack with business logic, input validation, and business validation to ensure that everything is always consistent for each of the aggregates (each cluster of associated objects that are treated as a unit for the purpose of data changes) in the write model. The read model has no business logic or validation stack and just returns a DTO for use in a view model. The read model is eventually consistent with the write model.
Decouple every component we have
\
\
\
Let make this to be our advantage by off-loading from database using read-model
\
No need to check permission of time-off and don’t need to get list of member of projetcs