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.

CQRS introduction


Published on

Published in: Technology
  • Be the first to comment

CQRS introduction

  1. 1. Yuriy Taras
  2. 2.  Conventional architecture and its problems CQRS introduction Bonus – Event Sourcing
  3. 3.  Collaborations – when users are working on same set of data Staleness – when data may expire between read and write
  4. 4. Data is stale anyway. Why don’t admit it?We can create read-optimized database to show data. Remember OLAP and OLTP?SELECTtitle, short_text, publish_date, publisher_name, comment_count FROM posts_listGood scalability, good performanceYou can use direct mapping from tables to UI
  5. 5. 1. Manager loads client info2. He’s editing data a) Address is changed b) Status is changed to ‘preferred’3. In background status is changed to ‘delinquent’4. Manager submits dataWhat would you do? Example by Udi Dahan
  6. 6. Conventional way – here’s updated copy of your data, save it to databaseCQRS (in fact, DDD) way – do this with your entity
  7. 7.  Commands are verbs – RenameUser, PublishPost Most of them will be idempotent – no more double submit problem Include version of your data – will help to resolve conflicts Commands are asynchronous – remember about DDD’s CQS concept? Do command validation before sending it – if you can’t do it without making command synchronous, it’s no longer a validation and we’ll talk later about it
  8. 8. public final class ChangeStatus implements Serializable {// private final fields skipped public ChangeStatus(Status status, EntityKey userId, longversion, String description) { assertNotNull(status); if (status == Status.REQUIRES_ACTION) { assertNotNull(description); } // and so on}}
  9. 9. Usually it’s Domain Model – commands matches perfectly with DDD’s entities andservices.Still you can use Transaction Script or Table Module patterns – CQRS doesn’t care.
  10. 10. Stale data – most of the time you’ll be able to merge changes in different branches withoutconflicts. do you see DCVS terminology here?Example:CustomerStatus(delinquent) + ChangeCustomerAddress = merge automaticallyCustomerStatus(delinquent) + CustomerStatus(preferred) = depends on abusiness logic, but probably make it delinquent and notify managerWhat if billing command would be late?CustomerStatus(preferred) + CustomerStatus(delinquent) = make it delinquentand notify manager
  11. 11.  Database deadlock Some component down Kernel panic somewhere Bug in our software Other infrastructure issuesRetry!
  12. 12. Command processor should emit (publish) events.Commands are DoThis. Events are ThisDone.
  13. 13. Read database is the most obvious subscriber:UPDATE post_lists SET comments_count = comments_count+ 1 WHERE post_id = #{post_id}INSERT INTO comments_list SET … Do you still remember our OLTP/OLAP analogy?
  14. 14.  Send email notification Write to file Notify other system via Web service or ESB Push notification to Web-client with Web sockets Ring the alarm
  15. 15. Pros Better scalability Taking staleness into account Works better in collaboration scenariosCons Harder to understand and implement Not widely known/adopted Requires more setup that conventional architecture In simple cases is overcomplicated
  16. 16.  Complex business domain Users that collaborate on common data Scalability and performance Large or distributed team
  17. 17. Conventional way to store domain objects – database tables.Post(id: 5, title: ‘CQRS+ES’, content: ‘Today I wantto…’)
  18. 18. Other way be storing it as a series of events:1. PostCreated(id: 1)2. PostTitled(‘CQRS+ES’)3. PostContentSet(‘Will add content later’)4. PostContentSet(‘I want to…’)5. PostPublished(Fri, 13)
  19. 19. Pros Audit for free Event centric Better conflict merging Simpler testingCons Even more infrastructure to set up (snapshots, cleanups, etc) Not very known/popular Higher entrance threshold
  20. 20.  CQRS Jorney CQRS Blog Clarified CQRS Fowler’s articles   Other links