Exploring CQRS and Event Sourcing

21
Christopher Bennage Microsoft

description

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

Transcript of Exploring CQRS and Event Sourcing

Page 1: Exploring CQRS and Event Sourcing

Christopher BennageMicrosoft

Page 2: Exploring CQRS and Event Sourcing

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

Azure

Page 3: Exploring CQRS and Event Sourcing

Terminology

Page 4: Exploring CQRS and Event Sourcing

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.

Page 5: Exploring CQRS and Event Sourcing

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

Page 6: Exploring CQRS and Event Sourcing

Taking it further…Separate Data Stores

PresentationValidation

Commands

Domain LogicData persistence

Queries (generate DTOs)

Page 7: Exploring CQRS and Event Sourcing

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

Page 8: Exploring CQRS and Event Sourcing

CQRS / ES

PresentationValidation

Commands

Domain LogicData persistence

Read thin-layer

<<projection>>

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

Page 9: Exploring CQRS and Event Sourcing

• Asynchronous is faster, but may yield stale data

• Write Model can be consistent

• Write Model is the source of truth

Eventual ConsistencyThe Trade-Off

Page 10: Exploring CQRS and Event Sourcing

a CQRS Journey

Page 11: Exploring CQRS and Event Sourcing

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

~Aristotle

Page 12: Exploring CQRS and Event Sourcing
Page 13: Exploring CQRS and Event Sourcing

• Build Consensus

• Focus on Learning

• Leverage Windows Azure

Project Objectives

Page 14: Exploring CQRS and Event Sourcing

Decomposing the Domain

Conference Management

Orders & Registrations

Payments

Discounts

Page 15: Exploring CQRS and Event Sourcing

demo >> conference management system

Page 16: Exploring CQRS and Event Sourcing

• 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?

Page 17: Exploring CQRS and Event Sourcing

• Integration with other systems / screens

• Versioning / Evolution

• Team development

• Testing

• Performance can be fine-tuned separately

BenefitsCQRS & Event Sourcing

Page 18: Exploring CQRS and 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

Page 19: Exploring CQRS and Event Sourcing

Questions?

Page 20: Exploring CQRS and Event Sourcing

Personal

•@bennage

• dev.bennage.com

Project

• cqrsjourney.github.com

• aka.ms/cqrs

• aka.ms/cqrspdf

microsoft.com/practices

Resources

Page 21: Exploring CQRS and Event Sourcing