CQRS & Queue unlimited
Transcript of CQRS & Queue unlimited
ISKA 08/2010
CQRS & QUEUEUNLIMITED
Introduction to CQRS pattern
QueueUnlimited concepts and implementation
Demo
WHAT WE ARE GOING TO SEE
“Crack” for architects
CQRS
CommandQueryResponsibility Segregation
CQRS
CQRS – WHY DO WE NEED IT?
BS Customer BS CRM
FE ShopFE
Administration
CQRS – WHY DO WE NEED IT?
BS Customer BS CRM
FE ShopFE
Administration Model View
Controller
Service
Layer
DTO’s
ServicesDomain (business
logic)Data Access Layer
CQRS – WHY DO WE NEED IT?
Model View
Controller
Service
Layer
DTO’s
ServicesDomain (business
logic)Data Access Layer
SOAP / REST schema’s
OO objects Rows
View Model
HTML
“How long would it take to create a screen like this?”
CQRS – WHY DO WE NEED IT?
In a traditional 3 layered SOA: Small changes = relative big impact Performance problems are very hard to solve Mapping, mapping mapping Serialization, Serialization, … Authorization hell All layers are coupled
(I know you don’t want to hear this, but is the truth !)
CQRS – WHY DO WE NEED IT?
<asp:DataList ID="DataList1" runat="server" DataSourceID="LastUsersDataSource"> <ItemTemplate> <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' /> <br /> </ItemTemplate></asp:DataList>
<asp:SqlDataSource ID="LastUsersDataSource" runat="server" ConnectionString="<%$ ConnectionStringsMyConnectionString %>" SelectCommand="SELECT [Name], [InsertDateTime] FROM [LastUsers] ORDER BY [InsertDateTime] DESC "></asp:SqlDataSource>
MS DEMO’S RULE !
Let’s segregate queries and commandsQuery:
Query without transforming or mapping Show stale data whenever we can 2 Tier Data store for each view (persisted views)
Command: Talk in business events Keep all events as a truth Tell, don’t ask Async whenever we can 3 Tier
CQRS - TO THE RESCUE
CQRS - TO THE RESCUE
UI
UI DataStore
Truth (optional)
Business Logic
Commands
Queries
EventBus
Subscribe
Publish
CQRS - TO THE RESCUE
BS Customer BS CRM
FE ShopFE
Administration
Events
Audit tracing Async by design Scalable by design Compensationable logic / conversations Close to the business Feature’s share no data/code = rapid development Best storage for best needs (ex: RDBMS for command
processing, document based for UI datastore) Replayable Performance cannot get killed after adding that one cool
feature Rollout! Easy to unit test:
Given <Previous Events> When <Command> is fired Then <New Events> expected
CQRS - ADVANTAGES
Requires also other view on specification and testing can be considered an advantage
Release management has to be diff erent (per feature style) disadantage because of old style program managers
Very hard to implement correctlyNeeds much disc space Eventually consistency seems scarry to most IT
managers
CQRS - DISADVANTAGES
QueueUnlimited
OpenSource project started by Marc Rexwinkel en Tim Mahy
http://queueunlimited.codeplex.com/
For current customer platform: Sql Server 2008 R2 Sql Server Service Broker .NET 3.5
QUEUE UNLIMITED
A queue like data store that allows asynchronous processing A data store that can distribute it’s messages to multiple
subscribers A data store that can perform a replay of it’s messages (from a
certain period or all messages) A data store that can check retention l ifetime of messages and
remove them when necessary A data store that can optionally send all previous messages to a
subscriber that is added a long time after an message was fi red A data store that is manageable through default tooling
(performance monitoring, intervening when production problems occur, backup …)
A data store that can scale up or scale out A data store that can preserve the ordering of messages inside
groups (functional partitioning) A data store that is fully ACID without a two phase commit (so no
DTC)
QUEUE UNLIMITED
Uses only Stored Procedures Implemented using SSB
QUEUE UNLIMITED
How it works
QUEUE UNLIMITED – NO SUBSCRIBERS
PublishEvent SP
Root Queue
InitiatorService
RootService
QUEUE UNLIMITED – ADDING A SUBSCRIBER
PublishEvent SP
Root Queue
InitiatorService
RootService
RootQueueActivation SP
Activated
Subscriber 1 Queue
Replay 1 Queue
Generated SP
Not activated
Replay 1 InitiatorService
Subscriber InitiatorService
Subscriber ReceiveService Replay 1 ReceiveService
Some app
QUEUE UNLIMITED – ADDING ANOTHER SUBSCRIBER
SendEvent SP
Root Queue
Initiator service
Receive Service
Distribute SP
Activated
Subscriber 1 Queue
Replay 1 Queue
Distribute 3 SP
Initiator service 3
Initiator service 2
Receive Service 2
Receive Service 3
Activated
Subscriber 2 Queue
Replay 2 Queue
Distribute 4 SP
Initiator service 5
Initiator service 4
Receive Service 4Receive Service
5 Not activated
Some app
Some app
QUEUE UNLIMITED – GARBAGE COLLECTION
SendEvent SP
Root Queue
Initiator service
Receive Service
Distribute SP
Activated
Subscriber 1 Queue
Replay 2 Queue
Distribute 4 SP
Initiator service 5
Initiator service 2
Receive Service 2
Receive Service 5
Not activate
d
Subscriber 2 Queue
Initiator service 4
Receive Service 4
Some appSome app
Open for some fresh developers
QUEUE UNLIMITED
Multiplayer Dice websitePart of QueueUnlimited for showing demo possibilities
For more demo’s look at: NCQRS (http://ncqrs.org/) CQRS4J (http://code.google.com/p/cqrs4j/)
For more detailed information look at:
http://skillsmatter.com/podcast/open-source-dot-net/udi-dahan-command-query-responsibility-segregation
http://www.infoq.com/interviews/dahan-cqrs-ddd-nservicebus
DEMO