Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Practical introduction to DDD, CQRS and Event Sourcing

468 views

Published on

After several in-depth talks about Event Sourcing, I realized that there's a large group of developers that may have heard about Domain-Driven Design, Command-Query Responsibility Segregation and Event Sourcing, but have not really connected the dots yet. So in this talk, I'd like to take a practical example of a simple domain and gradually introduce functional requirements to see how the principles behind DDD affect the way your entities are going to protect the business rules. After that, I'll introduce some real-world non-functional requirements and see if and how Event Sourcing and/or CQRS may or may not help to accomplish those.

Published in: Technology
  • Be the first to comment

Practical introduction to DDD, CQRS and Event Sourcing

  1. 1. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver
  2. 2. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver Mortgage Application Proofing Documents Loan Officer
  3. 3. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver
  4. 4. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver …all proofing documents must have been signed before the mortgage application can be approved
  5. 5. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver Use a foreign key constraint to prevent removing signatures Transaction boundary to prevent tampering with proof concurrently.
  6. 6. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver No way to remove a signature.
  7. 7. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver Aggregate boundary Protects the signatures Uses the Ubiquitous Language Bounded context
  8. 8. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver …reading should be fast and collaboration is low
  9. 9. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver HTTP API Domain OR/M RDBMS Web App Tables (Materialized) Views
  10. 10. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver HTTP API Domain NOSQL Web App Documents Indexes Elastic Search
  11. 11. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver Command Handlers Commands Domain App Query HTTP API Command HTTP API NOSQL DocumentsIndexes Elastic Search Data Access Data Access Use Ubiquitous Language Captures business processes Indexes are typically eventually consistent.
  12. 12. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver …changes have to be audited
  13. 13. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver Command Handlers Commands Domain App Query HTTP API Command HTTP API NOSQL DocumentsIndexes Elastic Search Data Access Data Access Audit Trail
  14. 14. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver …many folks work on the same documents concurrently
  15. 15. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver Command Handlers Commands Domain Model Event Store Events App Query Store Data Access Projectors Events Query HTTP API Projections Events Command HTTP API Great unit of testing Great unit of testing Auditability comes for free Can look at the state in the past Can be scaled independently Autonomous projections Aligns well with Event Storming Forces you to understand your domain thorogoughly No relational table structure anymore Can replay old data against new rules More difficult to envisage domain relationships
  16. 16. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver
  17. 17. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous ImproverDennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver Application Domain NoSQL / RDBMS OR/M / DAL Web UI, HTTP API, etc Lucene Index Document Projector Web UI, HTTP API, etc Web UI, HTTP API, etc Domain Commands Events Event StoreProjections Projectors Uses Event Sourcing Uses traditional CRUD architecture Indexing-based architecture Subcribe to webhooks Coarse- grained HTTP requests. Bus Subscribe Publish coarse- grained event
  18. 18. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver …list of proofing documents with state, mortgage number and last loan officer that signed it.
  19. 19. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver Event Store App Query Store Data Access Mortgage Proof Projector Subscription Query HTTP API Mortgage Proof Projection Loan Officer Projector Loan Officer Projection Joins the tables while querying Load Officers Proofing Documents
  20. 20. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver Event Store App Query Store Data Access Mortgage Proof Projector Events Query HTTP API Mortgage Proof Projection Processes events from the mortgage and loan officer aggregates Proofing Document Projections Contains state, mortgage number and loan officer info Autonomous Can be asynchronous Can be stale. Now what? Can be anything.
  21. 21. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver …a new version requires a schema change?
  22. 22. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver Event Store Projector Application Application Network Load Balancer Event Store Version 1 Version 2 events Projection Projector Projection bring off-line Returns HTTP 503 (Service Unavailable) Returns HTTP 503 (Service Unavailable)
  23. 23. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver Event Store Projector Application Version 2 Owns schema Version 1 X Involves down-time until projections are rebuild
  24. 24. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver …the terminology changes?
  25. 25. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver HTTP Command API Some Command Handler Customer #123 Event Store Customer Created Event Get<Customer>(“123”) Customer Created Event Converter Customer Enrolled Event May split or merge events Can also run as part of migration May change the identity Unaffected Events Dennis Doomen | @ddoomen | The Continuous Improver
  26. 26. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver …there’s a bug in a projection?
  27. 27. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver …rebuilding a projection is slow?
  28. 28. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver Event Store Projector Projector RDBMS Subscribe Subscribe Document DB Projector RDBMS Subscribe Raw SQLNHibernate RavenDB Autonomous & independent Storage technique optimized for projection Use aggressive caching during rebuilds. Caching strategy optimized for projector Stream-by-stream projections during rebuilds.
  29. 29. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver …we have 4 years of data and rebuilding is still too slow?
  30. 30. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver Document #1 Created Event Event Store Graph Projector Document #1 Closed Event (other events) Projector with active projections Archiving Projector Start archiving Document #1 Marked As Archivable Event Mark all events as archivable Tracks dependencies between documents Deletes projections related to Document #1 Can skip all archivable events during next rebuild.
  31. 31. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver Event Store Lucene Projector Document #1 Marked As Archivable Event Allows projectors to clean up Lucene Index Take snapshot Purge events Tombstone $tombstone stream Application Document Projector Search. Tracks deleted streams for future references Stream Tombstoned Event Search
  32. 32. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver …need to capture the original name of a loan officer while signing?
  33. 33. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver Event Store Projector Subscription Projection Subscribes to events from loan officers and documents Loan Officer Lookup
  34. 34. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver Event Store Projector Project loan officer events Subscription Subscription Storage (persistent) Lookup Event Modifier Modifies events with lookup data Receives modified events
  35. 35. Dennis Doomen | @ddoomen | Aviva Solutions | The Continuous Improver …then ping me at @ddoomen …or email me at dennis.doomen@avivasolutions.nl

×