46
Fighting Bottlenecks with CQRS/ES ResearchGate Berlin Nov 30, 2013 Mathias Verraes @mathiasverraes verraes.net

Fighting Bottlencks with CQRS - ResearchGate

Embed Size (px)

DESCRIPTION

ResearchGate DevDay Berlin, November 30, 2013 How can you sell a gazillion tickets in an hour? This talk will explain how to use CQRS, Event Sourcing, and a strong focus on the problem domain to build a system that deals with peaks, reporting, and occasionally connected devices. More at http://verraes.net/ or http://twitter.com/mathiasverraes

Citation preview

Page 1: Fighting Bottlencks with CQRS - ResearchGate

Fighting Bottlenecks

with CQRS/ES

ResearchGate Berlin

Nov 30, 2013

Mathias Verraes @mathiasverraes

verraes.net

Page 2: Fighting Bottlencks with CQRS - ResearchGate

Student of

Systems

Meddler of

Models

Labourer of

Legacy

Page 3: Fighting Bottlencks with CQRS - ResearchGate

Blog verraes.net

DDDBE domaindriven.be

Podcast elephantintheroom.io

Page 4: Fighting Bottlencks with CQRS - ResearchGate

Business Problem A Simplified Model

Command/Query Responsibility Segregation Event Sourcing

Read Model Projections Eventual Consistency

Page 5: Fighting Bottlencks with CQRS - ResearchGate

Business Problem

Page 6: Fighting Bottlencks with CQRS - ResearchGate

Sell a gazillion concert tickets to desperate fans

… in a hurry!

Page 7: Fighting Bottlencks with CQRS - ResearchGate

Don’t crash during peaks

Don’t annoy our customers

Don’t sell more than we have

Page 8: Fighting Bottlencks with CQRS - ResearchGate

A Simplified Model

Page 9: Fighting Bottlencks with CQRS - ResearchGate

Model invariants as a Presale Aggregate?

Page 10: Fighting Bottlencks with CQRS - ResearchGate

#reservedTickets #soldTickets

#availableTickets

Presale # Tickets

Page 11: Fighting Bottlencks with CQRS - ResearchGate

Model invariants as individual Ticket Aggregates

Page 12: Fighting Bottlencks with CQRS - ResearchGate

reserved sold

available

Individual Ticket status

Page 13: Fighting Bottlencks with CQRS - ResearchGate

Command/Query Responsibility Segregation

Page 14: Fighting Bottlencks with CQRS - ResearchGate

Client

Remote Façade

Application Services

Domain Model

ORM

Database

Page 15: Fighting Bottlencks with CQRS - ResearchGate

Highly cohesive Read + Write

Model?

Page 16: Fighting Bottlencks with CQRS - ResearchGate

Bottleneck

Page 17: Fighting Bottlencks with CQRS - ResearchGate

Client

Database Database

Remote Façade

Application Services

Domain Model

ORMEventually Consistent

Page 18: Fighting Bottlencks with CQRS - ResearchGate

CQRS challenges the assumption that reading and writing are supposed to share the same

abstractions.

Page 19: Fighting Bottlencks with CQRS - ResearchGate

CQRS challenges the assumption that reading and writing are supposed to share the same

models.

Page 20: Fighting Bottlencks with CQRS - ResearchGate

CQRS challenges the assumption that reading and writing are supposed to share the same

databases.

Page 21: Fighting Bottlencks with CQRS - ResearchGate

CQRS challenges the assumption that reading and writing are supposed to share the same

applications.

Page 22: Fighting Bottlencks with CQRS - ResearchGate

Segregation of read and write is a radical form of decoupling.

Page 23: Fighting Bottlencks with CQRS - ResearchGate

Client

Domain Model

Remote Façade Remote Façade

Application Svcs Application Svcs

Domain Model

ORM ORM

Database Database

Page 24: Fighting Bottlencks with CQRS - ResearchGate

Radical!

Page 25: Fighting Bottlencks with CQRS - ResearchGate

Client

Write Model

Read Model

Page 26: Fighting Bottlencks with CQRS - ResearchGate

Client

Write Model

Read Model

DTOCommands

Even

ts

Page 27: Fighting Bottlencks with CQRS - ResearchGate

Interact with State

Guard Invariants

Project State

Page 28: Fighting Bottlencks with CQRS - ResearchGate

Task Based UI

Event Store

Polyglot Persistence

Page 29: Fighting Bottlencks with CQRS - ResearchGate
Page 30: Fighting Bottlencks with CQRS - ResearchGate

Event Sourcing

Page 31: Fighting Bottlencks with CQRS - ResearchGate

TicketWasReserved [ ticketId, forUser, onDate ]

Page 32: Fighting Bottlencks with CQRS - ResearchGate

Ticket state = Ticket history !

TicketWasSold [ toUser ] TicketWasReserved [ forUser, onDate ] ReservationWasReleased [ ] TicketWasReserved [ forUser, onDate ] TicketWasPrinted [ inPresale ]

Page 33: Fighting Bottlencks with CQRS - ResearchGate

Mature domains already work this way !

AccountWasDebited [ 20 ] AccountWasDebited [ 10 ] AccountWasCredited [ 100 ] AccountWasOpened

Page 34: Fighting Bottlencks with CQRS - ResearchGate

BankAccount { debit(amount) { guardThatAccountIsNotOverdraft(amount) recordThat( new AccountWasDebited(amount) ) } }

Page 35: Fighting Bottlencks with CQRS - ResearchGate

BankAccount { apply(AccountWasDebited event) { amount = amount - event.amount } }

Page 36: Fighting Bottlencks with CQRS - ResearchGate

BankAccount { getRecordedEvents() {} reconstituteFromHistory(eventHistory) {} }

Page 37: Fighting Bottlencks with CQRS - ResearchGate

Read Model Projections

Page 38: Fighting Bottlencks with CQRS - ResearchGate

TicketWasSold TicketWasReserved

ReservationWasReleased TicketWasReserved TicketWasPrinted

Ticket !

!

Page 39: Fighting Bottlencks with CQRS - ResearchGate

TicketWasSold TicketWasReserved

ReservationWasReleased TicketWasReserved TicketWasPrinted

AvailableTicket TotalNumberOfAvailableTickets

AverageNumberOfTicketsPerBuyer AverageNumberOfReleasesPerTicket

Page 40: Fighting Bottlencks with CQRS - ResearchGate

Eventual Consistency

Page 41: Fighting Bottlencks with CQRS - ResearchGate

Writes are immediately consistent Reads are eventually consistent

Page 42: Fighting Bottlencks with CQRS - ResearchGate

Many issues stemming from eventual consistency can be solved in the user interface

Page 43: Fighting Bottlencks with CQRS - ResearchGate

A Ticket can only be sold once. How do we know it’s available?

Page 44: Fighting Bottlencks with CQRS - ResearchGate

Get a batch of x*2 PossiblyAvailableTickets !

ReserveTicket ack/nack

loop while acks < 3

To buy x tickets:

Page 45: Fighting Bottlencks with CQRS - ResearchGate

More? !

Testing Context Mapping Event Storming

Process Managers Occasionally Connected Systems

Reporting Legacy Integrations

Page 46: Fighting Bottlencks with CQRS - ResearchGate

Questions?

verraes.net@mathiasverraes