Change the Rules



Rethink you architecture with
           CQRS
Change the Rules




Who are those guys?

INTRODUCTION
Change the Rules
Change the Rules
Change the Rules


                  Agenda
•   Introduction
•   Current architectures & problems
•   CQRS
•   Food for th...
Change the Rules
Change the Rules


Huidige architecturen




      bron: .NET Application Architecture Guide 2.0
Change the Rules


Huidige architecturen
       Presentation layer




       Application layer




           Data layer
Change the Rules


multi-tier
 Presentation layer




   Domain layer




     Data layer
Change the Rules

                                                 Update DTO

                                           ...
Change the Rules

                                                 Update DTO

                                           ...
Change the Rules




“A single model cannot be appropriate for
reporting, searching, and transactional
behaviors.” – Greg ...
Change the Rules




“Doordat software processen kan
automatiseren stelt het ons in staat deze
verder op te schalen dan oo...
Change the Rules




“Every method should either be a
command that performs an action, or a
query that returns data to the...
Change the Rules




Presentation layer




  Domain layer




    Data layer
Change the Rules




             Presentation layer




Data layer                        Domain layer
Change the Rules




             Presentation layer




Data layer                        Domain layer
Change the Rules




                          Presentation layer




Read focus                                          ...
Change the Rules




                               Presentation layer



                          Queries            Com...
Change the Rules




                               Presentation layer



                          Queries            Com...
Change the Rules




Here we go!

FROM CONCEPT TO ARCHITECTUUR
Change the Rules




User interface
Change the Rules


                     Queries
•   Requests information
•   Is context dependant
•   Is executed many tim...
Change the Rules


                     Read side




User interface
Change the Rules


                         Read side




                 Read databases




User interface
Change the Rules


                         Read side




                 Read databases




                     Facade
...
Change the Rules


                          Read side




                 Read databases




                     Facade...
Change the Rules


                 Read side
•   Focus on data needs
•   Optimized for reading
•   Can contain multiple m...
Change the Rules




“State transitions are an important part of
our problem space and should be modeled
within our domain...
Change the Rules


                 Commands
• Causes a state transition
• Contains the intend of the user
• Exists in a b...
Change the Rules


         Commands example



Create                                    Remove
         Add item   Add i...
Change the Rules


Write side                                                Read side




                               ...
Change the Rules


Command handler sample
Change the Rules


              The domain
• Validates and handles commands
• Encapsulates behavior
• Build with rich obj...
Change the Rules


Write side                                                      Read side




                         ...
Change the Rules


Write side                                                          Read side




                     ...
Change the Rules


             Domain events
• Result of a command
• Represents a event

• Examples
  – ProductAddedToSho...
Change the Rules


Transitional flow
Change the Rules


Write side                                                          Read side




                     ...
Change the Rules


Write side                                                                                             ...
Change the Rules


Denormalizer sample
Change the Rules


 Write side                                                                                            ...
Change the Rules


                 Event store
•   Contains all domain events
•   Does not need to be relational
•   Cont...
Change the Rules


 Write side                                                                                            ...
Change the Rules


Application flow
Change the Rules


            CQRS Wrapup
• The domain receives commands and
  publishes domain events.
• All state chang...
Change the Rules


                Benefits
• Fully encapsulated domain that only
  exposes behavior
Change the Rules


                Benefits
• Fully encapsulated domain that only
  exposes behavior
• Keep users intent
Change the Rules


                 Benefits
• Fully encapsulated domain that only
  exposes behavior
• Keep users intent
...
Change the Rules


                 Benefits
• Fully encapsulated domain that only
  exposes behavior
• Keep users intent
...
Change the Rules


                 Benefits
• Fully encapsulated domain that only
  exposes behavior
• Keep users intent
...
Change the Rules


                 Benefits
• Fully encapsulated domain that only
  exposes behavior
• Keep users intent
...
Change the Rules


                 Benefits
• Fully encapsulated domain that only
  exposes behavior
• Keep users intent
...
Change the Rules


              Consequences
•   Learning curve
•   Overhead for simple CRUD
•   Data duplication
•   Int...
Change the Rules


          Food for thought
• Should your read model even contains a
  relational database?
• What does ...
Change the Rules


You’ve been a great audience!
Change the Rules


           Reference material
•   Podcast with Greg Young
•   MyShop reference architecture
•   Ncqrs f...
Upcoming SlideShare
Loading in …5
×

Rethink your architecture with CQRS

4,020 views

Published on

Published in: Technology
0 Comments
12 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,020
On SlideShare
0
From Embeds
0
Number of Embeds
54
Actions
Shares
0
Downloads
145
Comments
0
Likes
12
Embeds 0
No embeds

No notes for slide

Rethink your architecture with CQRS

  1. 1. Change the Rules Rethink you architecture with CQRS
  2. 2. Change the Rules Who are those guys? INTRODUCTION
  3. 3. Change the Rules
  4. 4. Change the Rules
  5. 5. Change the Rules Agenda • Introduction • Current architectures & problems • CQRS • Food for thought
  6. 6. Change the Rules
  7. 7. Change the Rules Huidige architecturen bron: .NET Application Architecture Guide 2.0
  8. 8. Change the Rules Huidige architecturen Presentation layer Application layer Data layer
  9. 9. Change the Rules multi-tier Presentation layer Domain layer Data layer
  10. 10. Change the Rules Update DTO Presentation layer Request DTO Map DTO Domain layer Request Request DTO DTO Map DTO Map DTO Data layer Query data Query data Query data Query data
  11. 11. Change the Rules Update DTO Presentation layer Request Send DTO DTO Map DTO Map DTO Domain layer Request Request Send Send DTO DTO DTO DTO Map DTO Map DTO Map DTO Map DTO Data layer Query data Query data Query data Query data Write data Write data Write data Write data
  12. 12. Change the Rules “A single model cannot be appropriate for reporting, searching, and transactional behaviors.” – Greg Young
  13. 13. Change the Rules “Doordat software processen kan automatiseren stelt het ons in staat deze verder op te schalen dan ooit tevoren.“
  14. 14. Change the Rules “Every method should either be a command that performs an action, or a query that returns data to the caller, but not both.” – Bertrand Meyer
  15. 15. Change the Rules Presentation layer Domain layer Data layer
  16. 16. Change the Rules Presentation layer Data layer Domain layer
  17. 17. Change the Rules Presentation layer Data layer Domain layer
  18. 18. Change the Rules Presentation layer Read focus State transition focus Data layer Domain layer
  19. 19. Change the Rules Presentation layer Queries Commands Read focus State transition focus Data layer Domain layer
  20. 20. Change the Rules Presentation layer Queries Commands Read focus State transities focus Read layer Domain layer
  21. 21. Change the Rules Here we go! FROM CONCEPT TO ARCHITECTUUR
  22. 22. Change the Rules User interface
  23. 23. Change the Rules Queries • Requests information • Is context dependant • Is executed many times • Should be simple (select * from x where y) • Examples: – Get all products for user X – Get total price of all orders of month May
  24. 24. Change the Rules Read side User interface
  25. 25. Change the Rules Read side Read databases User interface
  26. 26. Change the Rules Read side Read databases Facade User interface
  27. 27. Change the Rules Read side Read databases Facade DTO’s User interface
  28. 28. Change the Rules Read side • Focus on data needs • Optimized for reading • Can contain multiple models if needed • Can be denormalized • Facades should be minimized
  29. 29. Change the Rules “State transitions are an important part of our problem space and should be modeled within our domain.” – Greg Young
  30. 30. Change the Rules Commands • Causes a state transition • Contains the intend of the user • Exists in a bounded context • Examples: – AddProductToShoppingCart – PurchaseOrder – MoveUserToNewAddress – CorrectAddressForUser
  31. 31. Change the Rules Commands example Create Remove Add item Add item Add item Purchase Cart item
  32. 32. Change the Rules Write side Read side Read databases Facade Commandhandlers commands DTO’s User interface
  33. 33. Change the Rules Command handler sample
  34. 34. Change the Rules The domain • Validates and handles commands • Encapsulates behavior • Build with rich object oriented techniques
  35. 35. Change the Rules Write side Read side Read databases Repository Domein Facade Commandhandlers commands DTO’s User interface
  36. 36. Change the Rules Write side Read side ? Read databases Repository Domein Facade Commandhandlers commands DTO’s User interface
  37. 37. Change the Rules Domain events • Result of a command • Represents a event • Examples – ProductAddedToShoppingCart – OrderPurchased – UserMovedToNewAddress – AddressCorrectedForUser
  38. 38. Change the Rules Transitional flow
  39. 39. Change the Rules Write side Read side ? Read databases Repository Domein Facade Commandhandlers commands DTO’s User interface
  40. 40. Change the Rules Write side Read side Denormalizers Event bus events events events Read databases Repository Domein Facade Commandhandlers commands DTO’s User interface
  41. 41. Change the Rules Denormalizer sample
  42. 42. Change the Rules Write side Read side Denormalizers Event bus events events events events Event store Read databases Repository Domein Facade Commandhandlers commands DTO’s User interface
  43. 43. Change the Rules Event store • Contains all domain events • Does not need to be relational • Contains the audit log • Read models can (re)build themself of it • The domain uses it to get the current state
  44. 44. Change the Rules Write side Read side Denormalizers Event bus events events events events queue Event store Read databases Repository Domein Facade Commandhandlers commands DTO’s User interface
  45. 45. Change the Rules Application flow
  46. 46. Change the Rules CQRS Wrapup • The domain receives commands and publishes domain events. • All state changes are represented by domain Events • The read models are updated as a result of the published Events • All Queries go directly to the read models, the domain model is not involved
  47. 47. Change the Rules Benefits • Fully encapsulated domain that only exposes behavior
  48. 48. Change the Rules Benefits • Fully encapsulated domain that only exposes behavior • Keep users intent
  49. 49. Change the Rules Benefits • Fully encapsulated domain that only exposes behavior • Keep users intent • Bullet-proof auditing and historical tracing
  50. 50. Change the Rules Benefits • Fully encapsulated domain that only exposes behavior • Keep users intent • Bullet-proof auditing and historical tracing • No object-relational impedance mismatch
  51. 51. Change the Rules Benefits • Fully encapsulated domain that only exposes behavior • Keep users intent • Bullet-proof auditing and historical tracing • No object-relational impedance mismatch • Optimized and multiple read models
  52. 52. Change the Rules Benefits • Fully encapsulated domain that only exposes behavior • Keep users intent • Bullet-proof auditing and historical tracing • No object-relational impedance mismatch • Optimized and multiple read models • Testability
  53. 53. Change the Rules Benefits • Fully encapsulated domain that only exposes behavior • Keep users intent • Bullet-proof auditing and historical tracing • No object-relational impedance mismatch • Optimized and multiple read models • Testability • Easy integration with external systems
  54. 54. Change the Rules Consequences • Learning curve • Overhead for simple CRUD • Data duplication • Introducing an new architecture has a big impact
  55. 55. Change the Rules Food for thought • Should your read model even contains a relational database? • What does this mean for requirement engineering? • What does this mean for outsourcing? • What does this mean for DBA?
  56. 56. Change the Rules You’ve been a great audience!
  57. 57. Change the Rules Reference material • Podcast with Greg Young • MyShop reference architecture • Ncqrs framework • InfoQ: Unshackle Your Domain

×