Top Banner
CQRS != Event Sourcing
40
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CQRS is not Event Sourcing

CQRS != Event Sourcing

Page 2: CQRS is not Event Sourcing

Henrik Møller RasmussenFounder and CTO

The digital daycare

Page 3: CQRS is not Event Sourcing

The digital daycare

6 institutions!~1.000 children

Page 4: CQRS is not Event Sourcing

What is CQRS?Command Query Responsibility Segregation

Page 5: CQRS is not Event Sourcing

CQRS

WritingReading

User

Page 6: CQRS is not Event Sourcing

What is important (for most systems)Reading!• Support a lot of concurrent reads • Fast as possible • Combine, aggregate, calculate • Create new views / reports without risk of

introducing bugs in existing code • Scale reading independently of writing

Writing!• Fewer writes than reads • Performance not that important

(< 1s probably ok) • Simple models that are easy to understand

and reason about • Easy to test

Page 7: CQRS is not Event Sourcing

CQRS

WritingReading

User

Page 8: CQRS is not Event Sourcing

What is Event Sourcing?

Page 9: CQRS is not Event Sourcing

Classic approach to persistence

Store snapshots

Page 10: CQRS is not Event Sourcing

Event sourcing

Store every thing that happens - chronologically. Build snapshots “on the fly.

Page 11: CQRS is not Event Sourcing

EventStore

Customer Example

Customer

new Customer(Kermit, Sesame Streeet 1)

CustomerCreatedEvent(…)

Page 12: CQRS is not Event Sourcing

EventStore

Customer Example

Customer

$customer->move(Sesam Street 42)

CustomerCreatedEvent(…)CustomerMovedEvent(…)

Page 13: CQRS is not Event Sourcing

EventStore

Customer Example

Customer

$customer->upgradeTo(Customer::PREMIUM)

CustomerCreatedEvent(…)CustomerMovedEvent(…)CustomerUpgradedTo(…)

Page 14: CQRS is not Event Sourcing

Rehydrating

Page 15: CQRS is not Event Sourcing

Customer Example

Customer { name: Kermit, address: Sesame Street 1, customerType: Customer::REGULAR }

2012-07-12T10:30:01! ! ! CustomerCreatedEvent(Kermit, Sesame Street 1)

Page 16: CQRS is not Event Sourcing

Customer { name: Kermit, address: Sesame Street 42, customerType: Customer::REGULAR }

2013-10-24T08:16:37! ! ! CustomerMovedEvent(Sesame Street 42)

Customer Example

2012-07-12T10:30:01! ! ! CustomerCreatedEvent(Kermit, Sesame Street 1)

Page 17: CQRS is not Event Sourcing

2014-01-05T14:52:04! ! ! CustomerUpgradedTo(Customer::PREMIUM)

Customer { name: Kermit, address: Sesame Street 42, customerType: Customer::PREMIUM }

Customer Example

2012-07-12T10:30:01! ! ! CustomerCreatedEvent(Kermit, Sesame Street 1)2013-10-24T08:16:37! ! ! CustomerMovedEvent(Sesame Street 42)

Page 18: CQRS is not Event Sourcing

What is CQRS + Event Sourcing?

Reading

User

Writing

Events

Page 19: CQRS is not Event Sourcing

CQRS is no silver bullet• Requires a lot of infrastructure

• Not able to edit anything directly in the DB (of course we never do that :-)

• Pretty complex - hard to explain to junior developers

Page 20: CQRS is not Event Sourcing

CQRS != Event Sourcing

Page 21: CQRS is not Event Sourcing

Daily administration Check in/out

Daycare homepage Parent App

Page 22: CQRS is not Event Sourcing

LIVE DEMO FROM FAMLY.DK

Page 23: CQRS is not Event Sourcing
Page 24: CQRS is not Event Sourcing

Security

Email

Social

…Daycare

Bounded Contexts from Famly

Page 25: CQRS is not Event Sourcing

Children and their parents

Checkin / Checkout / Pickup Vacation / Sickness

Modules inside Daycare Context

Institutions and groups

Sleep

Employees

Page 26: CQRS is not Event Sourcing
Page 27: CQRS is not Event Sourcing

REST API

Famly Admin Famly App Famly Checkin Famly Homepage

Writing (Commands)Reading (Query)

Feed … and more

Daycare

MySQL DB

Feed … and more

Daycare

Famly Architecture

Page 28: CQRS is not Event Sourcing

REST API

ChildCheckinService

checkinChild(…)

ChildViewService

OKfindChildrenForGroup(…)

Cus

tom

han

dcra

fted

SQL

Collection of DTOs

Page 29: CQRS is not Event Sourcing
Page 30: CQRS is not Event Sourcing
Page 31: CQRS is not Event Sourcing
Page 32: CQRS is not Event Sourcing
Page 33: CQRS is not Event Sourcing

LIVE DEMO FROM FAMLY.DK

Page 34: CQRS is not Event Sourcing

• Simpler and smaller write-models, that are easy to reason about, because they don’t need any view-logic. (getters etc.)

• High-performing reads, since you fully utilize your underlying database server, and don’t need ORM’s for reading.

• Extreme flexibility since the only things needed to build new view-models are SQL

Benefits of using CQRS this way

Page 35: CQRS is not Event Sourcing

• You couple the modules through their underlying persistence structure!!

• The handwritten SQL is expecting certain columns names etc. So changing the write models will potentially break read models.

• So, have strict rules for where you are allowed to use this

WARNING

Page 36: CQRS is not Event Sourcing

Daycare

ChildDetails ParentDetails

Vacation

Security

Email

Social

Only CQRS between modules

Page 37: CQRS is not Event Sourcing

• If you have views that combines information for a lot of different subsystems

• If you have complex data structures that can benefit from doing data manipulation directly in the database (joins, subselect, counts etc.

When to use

Page 38: CQRS is not Event Sourcing

• Even though the app communicates with a lot of underlying subsystems, it only fetches data for a single child, and performance therefore is not a problem.

CQRS not needed for the app

Page 39: CQRS is not Event Sourcing
Page 40: CQRS is not Event Sourcing

Henrik Møller [email protected] Twitter: @heinodk IRC: Come join us at #ddd

The digital daycare