Your SlideShare is downloading. ×
CQRS introduction
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

CQRS introduction

542
views

Published on

Published in: Technology

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
542
On Slideshare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Yuriy Taras
  • 2.  Conventional architecture and its problems CQRS introduction Bonus – Event Sourcing
  • 3.  Collaborations – when users are working on same set of data Staleness – when data may expire between read and write
  • 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. 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. Conventional way – here’s updated copy of your data, save it to databaseCQRS (in fact, DDD) way – do this with your entity
  • 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. 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. 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. 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.  Database deadlock Some component down Kernel panic somewhere Bug in our software Other infrastructure issuesRetry!
  • 12. Command processor should emit (publish) events.Commands are DoThis. Events are ThisDone.
  • 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.  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. 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.  Complex business domain Users that collaborate on common data Scalability and performance Large or distributed team
  • 17. Conventional way to store domain objects – database tables.Post(id: 5, title: ‘CQRS+ES’, content: ‘Today I wantto…’)
  • 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. 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.  CQRS Jorney http://msdn.microsoft.com/en-us/library/jj554200.aspx CQRS Blog http://cqrs.wordpress.com/ Clarified CQRS http://www.udidahan.com/2009/12/09/clarified-cqrs/ Fowler’s articles  http://martinfowler.com/bliki/CQRS.html  http://martinfowler.com/eaaDev/EventSourcing.html Other links http://www.mindmeister.com/181195534/cqrs-ddd-links