Share
Embed
Transcript
55 slides
-
The default architecture for distributed business apps Domain Model User
Interface Remote Facade Application Services DTO DTO ORM DB -
The default architecture for distributed business apps Domain Model User
Interface Remote Facade Application Services DTO DTO ORM DB findCustomers() getCustomer() updateCustomer() <customer> <name>John Doe</name> <address>...</address> ... </customer> -
The default architecture for distributed business apps Domain Model User
Interface Remote Facade Application Services DTO DTO ORM DB GET /customers?filter=... GET /customer/{id} PUT /customer/{id} { "name": “John Doe”, "address": { ... } } -
The default architecture for distributed business apps Domain Model User
Interface Remote Facade Application Services DTO DTO ORM DB GET /customers?filter=... GET /customer/{id} PUT /customer/{id} { "name": “John Doe”, "address": { ... } } Anything wrong with this? -
“The features that characterize a class are divided into commands
and queries. A command serves to modify objects, a query to return information about objects.” Bertrand Meyer ETH Zurich Text cited from: Object-Oriented Software Construction, second edition, 1997 -
Part of the Design-by-Contract methodology First demonstrated in the object-oriented
EIFFEL programming language -
class Foo { void command(); Result query(); } Mutates state
Returns a value without causing side effects -
CQRS = CQS in the large Scope is a single
class Scope is a Bounded Context -
interface CustomerQueryService { CustomerList findCustomers(CustomerQuery); Customer getCustomer(ID); } interface CustomerCommandService
{ void updateCustomer(Customer); void deleteCustomer(ID); } -
The default architecture for distributed business apps Domain Model User
Interface Remote Facade Application Services DTO DTO ORM DB -
The default architecture for distributed business apps Domain Model User
Interface Query Facade Query Services DTO DTO ORM DB Command Facade Command Services CQRS PATTERN APPLIED -
But it allows you to challenge established assumptions and opens
up new architectural options! -
Assumption: Reads and Writes are strongly cohesive, so they must
be part of the same Bounded Context. -
Assumption: Reads and Writes are strongly cohesive, so they must
be part of the same Bounded Context. FALSE -
The default architecture for distributed business apps Domain Model User
Interface Query Facade Query Services DTO DTO ORM DB Command Facade Command Services CQRSified -
The default architecture for distributed business apps CQRSified User Interface
Query Facade Query Services DTO DTO ORM DB Command Facade Command Services Domain Model ORM Domain Model Query Facade Query Services ORM Domain Model Query Facade Query Services ORM Domain Model Command and query parts can scale independently, e.g. to accommodate highly asymmetric load. -
Assumption: Reads and writes use the same data, so they
must be served from and applied to the same domain model. -
Assumption: Reads and writes use the same data, so they
must be served from and applied to the same domain model. FALSE -
The default architecture for distributed business apps CQRSified Queries can
benefit from a specialized query model, optimized for quick data retrieval (de-normalized, pre- aggregated,...) User Interface Query Facade Query Services DTO DTO ORM DB Command Facade Command Services Command Model ORM Domain Model Query Facade Query Services ORM Domain Model Query Facade Query Services ORM Query Model -
The default architecture for distributed business apps CQRSified Queries can
benefit from a specialized query model, optimized for quick data retrieval (de-normalized, pre- aggregated,...) User Interface Query Facade Query Services DTO DTO ORM DB Command Facade Command Services Command Model ORM Domain Model Query Facade Query Services ORM Domain Model Query Facade Query Services ORM Query Model ‣ validate and process commands ‣ keep data consistent ‣ guarantee ACID properties ‣ behaviour of domain model ‣ more difficult to scale ‣ rich query capabilities ‣ short response times ‣ different views on data ‣ potentially denormalized ‣ easier to scale -
Assumption: For queries, we have to use a domain model
to abstract from the underlying data model. -
Assumption: For queries, we have to use a domain model
to abstract from the underlying data model. FALSE -
Query Services Query Services Thin Read Layer User Interface Query
Facade Query Services DTO DTO ORM DB Command Facade Command Services Command Model Query Facade Query Services Query Facade Query Services SELECT … FROM … WHERE ... The default architecture for distributed business apps CQRSified Queries are just dealing with data, not with behavior. Why do we need objects? -
Assumption: We must use the same database for queries and
commands to make sure that data is consistent. -
Assumption: We must use the same database for queries and
commands to make sure that data is consistent. FALSE -
The default architecture for distributed business apps CQRSified In many
cases, eventual consistency is sufficient. The data users are looking at in the UI is always stale to some extent. Query Services Query Services Thin Read Layer User Interface Query Facade Query Services DTO DTO ORM DB Command Facade Command Services Command Model Query Facade Query Services Query Facade Query Services SELECT … FROM … WHERE ... Query DB Query DB Query DB Event Handler Event Handler Event Handler Events Write -
The default architecture for distributed business apps CQRSified In many
cases, users don’t care whether their actions have immediate effect – as long as they eventually get feedback. Query Services Query Services Thin Read Layer User Interface Query Facade Query Services CMD DTO ORM DB Command Facade Command Services Command Model Query Facade Query Services Query Facade Query Services SELECT … FROM … WHERE ... Query DB Query DB Query DB Event Handler Event Handler Event Handler Events Write Command Queue Commands update read model asynchronously -
Query Services Query Services Thin Read Layer User Interface Query
Facade Query Services CMD DTO DB Command Facade Command Services Command Model Query Facade Query Services Query Facade Query Services SELECT … FROM … WHERE ... Query DB Query DB Query DB Event Handler Event Handler Event Handler Events Write Command Queue Commands Processor The default architecture for distributed business apps CQRSified CQRS plays well with an Event Sourcing architecture – you just store events and re-create the state of domain objects as needed. event store -
‣ capture each update to application state in an event
‣ changes to domain objects are the result of applying events ‣ events are immutable ‣ events are a representation of what has happened at a specific time Event Sourcing in a nutshell -
‣ allows you to rebuild application state at any point
in time just by replaying events up to that point in time ‣ allows you to analyse historic data based on detailed events that would otherwise have been lost ‣ gives you an audit log “for free” ‣ you can add new, optimized read models (potentially in-memory) later without migration hassle and such What’s so great about Event Sourcing? -
That’s it. Really. Feel free to ask me anything! @owolf
http://www.flickr.com/photos/kaptainkobold/5170454747/ How do I convince by boss that we’ll have to rewrite our application with CQRS??? -
CQRS is not a silver bullet and doesn’t apply everywhere.
Beware of the added complexity! -
Don’t do CQRS… …if your application is just a simple
CRUD-style app. …if you don’t have scaling issues. …if it doesn’t improve your domain models. -
Consider doing CQRS… …if your write/read load ratio is highly
asymmetrical. …if scaling your application is difficult. …if your domain model is bloated by complex domain logic, making queries inefficient. …if you can benefit from event sourcing. -
Axon Framework (Java) www.axonframework.org Piotr Wyczesany Lokad (.NET) http://lokad.github.com/lokad-cqrs/ Piotr
Wyczesany …and some more Piotr Wyczesany