Thursday, September 23, 2010

Clarified CQRS

@UdiDahan has just clarified CQRS for me:

Why CQRS
  • Collaboration - multiple actors using/modifying the same set of data
  • Staleness - once data has been shown to a user, that same data may have been changed by another actor
Queries
  • Use of an additional data store whose data can be a bit out of sync with the master database (if the data we're showing to the user is stale anyway)
  • Can be structured just like the ViewModel - no need to transform objects
Query Data Storage
  • Does not have to be a relational database!
Scaling Queries
  • Assuming that the data being served does not need to 100% up to date we can easily add more store instances without worrying about that they contain the exact same data
Commands and Validation
  • Clients send commands to the server - not returning errors to the client, but rather confirming/notifying the user of the outcome
  • Validation is different from business rules in that it states a context-independent fact about a command
  • Even though a command may be valid, there may still be reasons to reject it
  • The server still validates all commands, not trusting clients to do the validation
Commands and Autonomy
  • Commands don't need to be processed immediately - they can be queued
  • Shouldn't need to access the query store to process commands - any state that is needed should be managed by the autonomous component
  • Failure messages due to the database being down or hitting a deadlock should not be returned to the client - we can just rollback and try again by processing all messages in the queue
  • Each command could be processed by a different AC, each with its own queue
Service Layers
  • Each command is independent of the other - so why should we allow the objects which handle them (domain objects) to depend on each other? Dependencies are things which should be avoided, unless there is a good reason for them
Persistence for Command Processing
  • Do we really need to have a column for every single domain object property? What if we just serialized the domain entity and put it into a single column and had another column containing the ID i.e. akin to the key-value storage available from various cloud providers
See: http://www.udidahan.com/2009/12/09/clarified-cqrs

No comments: