Exploring CQRS and Event Sourcing

Post on 14-May-2015

1.294 views 2 download

Tags:

description

A journey into high scalability, availability, and maintainability with Windows Azure

Transcript of Exploring CQRS and Event Sourcing

Christopher BennageMicrosoft

Exploring CQRS and Event SourcingA journey into high scalability, availability, and maintainability with Windows

Azure

Terminology

Command

Query

Responsibility

Segregation

What is CQRS?Separating Reads from Writes

An architectural pattern that separates Commands, that change state, from Queries that only read state.

Simple Example of CQRSSegregating Queries

PresentationValidation

Commands

Domain LogicData persistence

Queries (generate DTOs)

Based on Rob Ashton’s codeofrob.com/entries/cqrs-is-too-complicated.html

Taking it further…Separate Data Stores

PresentationValidation

Commands

Domain LogicData persistence

Queries (generate DTOs)

What is Event Sourcing?An Alternate Way to Represent the Data

Cart Created

Item 1 Added

Item 2 Added

Item 1 Removed

Shipping Information

Added

Relational Model

Event Stream

CQRS / ES

PresentationValidation

Commands

Domain LogicData persistence

Read thin-layer

<<projection>>

Based on Rob Ashton’s codeofrob.com/entries/cqrs-is-too-complicated.html

• Asynchronous is faster, but may yield stale data

• Write Model can be consistent

• Write Model is the source of truth

Eventual ConsistencyThe Trade-Off

a CQRS Journey

“For the things we have to learn before we can do them, we learn by doing them.”

~Aristotle

• Build Consensus

• Focus on Learning

• Leverage Windows Azure

Project Objectives

Decomposing the Domain

Conference Management

Orders & Registrations

Payments

Discounts

demo >> conference management system

• Do multiple users compete for access to the same resources?

• Are the business rules ever changing?

• Is scalability one of the challenges?

• Is the business logic complex?

• Are benefits that CQRS brings clear?

When to Use CQRS?

• Integration with other systems / screens

• Versioning / Evolution

• Team development

• Testing

• Performance can be fine-tuned separately

BenefitsCQRS & Event Sourcing

• Throw away your assumptions

• CQRS space is a little confusing - inconsistency, dissonance

• Not a top-level architecture

• Not everything needs to be asynchronous, nor message-based

• In messaging, tracing matters big time

• Test for performance early

• Find or build a robust infrastructure that fits your needs

• “Sagas” != sagas; Avoid them at first

Some Lessons Learned

Questions?

Personal

•@bennage

• dev.bennage.com

Project

• cqrsjourney.github.com

• aka.ms/cqrs

• aka.ms/cqrspdf

microsoft.com/practices

Resources