CQRS, der etwas andere Architekturansatz

Post on 21-Jan-2017

142 views 0 download

Transcript of CQRS, der etwas andere Architekturansatz

C Q R SDer etwas andere Architekturansatz

Lars RöwekampCIO New Technologies

@mobileLarson@_openknowledge

#WISSENTEILEN

Branchenneutrale Softwareentwicklung und IT-Beratung

ÜBER OPEN KNOWLEDGE

#WISSENTEILEN

Lars Röwekamp (a.k.a. @mobileLarson)

ÜBER MICH

LR

#WISSENTEILEN

Wer bin ich - und wen ja, wie viele?

• CIO New Technologies • Enterprise & Mobile • Autor, Speaker, Coach & Mentor

• Snowboard & MTB Enthusiast• mehrfacher Vater, einfacher Ehemann

Es könnte alles so einfach sein!

#WISSENTEILEN

CQRS

#WISSENTEILEN

CQRS

#WISSENTEILEN

CQRS

Greetingsfrom

Eric

#WISSENTEILEN

CQRS

Greetingsfrom

Eric

#WISSENTEILEN

CQRS

Greetingsfrom

Eric

In sich konsistentes Modell viaAggregates, Entities, Value Objects, Builder, Services, Repositories, ...

Validierung, Relationen, Mapping, Rules ...

Isses aber nich ...

#WISSENTEILEN

CQRS

Greetingsfrom

Eric

In sich konsistentes Modell viaAggregates, Entities, Value Objects, Builder, Services, Repositories, ...

Validierung, Relationen, Mapping, Rules ...

#WISSENTEILEN

CQRS Write

#WISSENTEILEN

CQRS Read

#WISSENTEILEN

CQRS

#WISSENTEILEN

CQRS ReadWrite

#WISSENTEILEN

CQRS

#WISSENTEILEN

CQRS

#WISSENTEILEN

CQRS

#WISSENTEILEN

CQRS

Verletzung des SRP auf Architekturebene

#WISSENTEILEN

CQRS

Benutzerinteraktion vs. Datenbereitstellung

#WISSENTEILEN

CQRS

Domänen-Modell als Kompromiss für „Read“ & „Write“

CQRS

#WISSENTEILEN

Domänen-Modell als Kompromiss für R & W

• Business vs. Technologie? • Entities, DTOs, VOs?• Architektur Over-Engineering• Optimistic Locking• Caching

CQRS

#WISSENTEILEN

Domänen-Modell als Kompromiss für R & W

• Read vs. Write Performance• Read vs. Write Skalierung• Read vs. Write Optimierung

CQRS

#WISSENTEILEN

Klassische Ein-Domänen Architektur

Die Komplexität der Änderungen & Erweiterungen steigt mit der Komplexität des Domänenmodells

„A single model cannot beappropriate for reporting,

searching, and transactionalbehaviors.” Greg Young

„Every method should either bea command, that performs an action or a query that returns

data to the caller, but not both.”Bertrand Meyer

„Every method should either bea command, that performs an action or a query that returns

data to the caller, but not both.”Bertrand Meyer

Bertrand Meyer

C ommandQ ueryR ?S ?

Bertrand Meyer

C ommandQ ueryR esponsibilityS egregation

#WISSENTEILEN

CQRS

#WISSENTEILEN

CQRS

#WISSENTEILEN

CQRS

#WISSENTEILEN

CQRS

CreateCutomer

CutomerCreated

Find all Customers with ...

#WISSENTEILEN

CQRS// BEFORE: CRUD based interface with no DDD usage

interface CustomerService {

Customer createCustomer(Customer cust); void updateCustomer(Customer cust); void deleteCustomer(String id);

Customer getCustomer(String id); List<Customer> findCustomer(String query);

}

#WISSENTEILEN

CQRS// AFTER: CQRS based interfaces with DDD usage

interface CustomerQueryService {

Customer findCustomerWith(Name uniqueName);List<Customer> findAllCustomerMatching(SearchQuery sq);

}

interface CustomerCommandService {

Customer moveTo(Address newAddress);Customer marry(Name newName, Partner partner);

}

Und was bedeutet das für die

Architektur?

#WISSENTEILEN

CQRS

ReadSide...

• optimiertfürAbfragen• kurzeAntwortzeiten• unterschiedliche„Sichten“• einfachzuskalieren• möglichstdenormalisiert

WriteSide...

• „Kommandos“ausführen• Validierung• Datenkonsistenz• ACID• VerhaltenTeilderDomäne

#WISSENTEILEN

CQRS

#WISSENTEILEN

CQRS

#WISSENTEILEN

CQRS

#WISSENTEILEN

CQRS

#WISSENTEILEN

CQRS

#WISSENTEILEN

CQRS

#WISSENTEILEN

CQRS

#WISSENTEILEN

FAZIT

„Starbucksdoes notuseTwo-PhaseCommit“

http://www.enterpriseintegrationpatterns.com/ramblings/18_starbucks.html

#WISSENTEILEN

CQRS

#WISSENTEILEN

CQRS

#WISSENTEILEN

CQRS

#WISSENTEILEN

CQRS

#WISSENTEILEN

CQRS Write Read

Moment, was macht ihr da mit meinen

„Entities“?

#WISSENTEILEN

CQRS & Event Sourcing

... Änderungen „sammeln“ statt State updaten!

State vs. immutable Facts ...

BTW: Immutable!

#WISSENTEILEN

CQRS & Event Sourcing

... Änderungen „sammeln“ statt State updaten!

State vs. immutable Facts ...

BTW: Immutable!

#WISSENTEILEN

CQRS & Event Sourcing

... Änderungen „sammeln“ statt State updaten!

State vs. immutable Facts ...

#WISSENTEILEN

CQRS & Event Sourcing

... Änderungen „sammeln“ statt State updaten!

State vs. immutable Facts ...

CQRS & Event Sourcing

#WISSENTEILEN

Festhalten der Änderungen via Events

• speichern der Events in korrekter Reihenfolge• vollständige Historie implizit• keine „Updates“ oder „Deletes“• kein „Current State“

• „Current State“ ergibt sich durch „Replay“ der Events für einen bestimmten Zeitpunkt!

CQRS & Event Sourcing

#WISSENTEILEN

Festhalten der Änderungen via Events

• „Kein“ Locking bei konkurrierenden Zugriffen (Repräsentation durch zwei Events)

• Evolution des Models problemlos möglich (neueinspielen der Events)

• Tests auf Command- & Eventebene (wenn dieses Command, dann das Ergebnis)

WT#?

Wo bleibt da die Konsistenz?

#WISSENTEILEN

FAZIT

„Starbucksdoes notuseTwo-PhaseCommit“

http://www.enterpriseintegrationpatterns.com/ramblings/18_starbucks.html

Ist das nicht super langsam?

CQRS & Event Sourcing

#WISSENTEILEN

Ist das nicht super langsam?

• In der Regel wenige Events pro Aggregate

• Problem bei langlebenden Entities• Problem bei regelmäßigen Änderungen

• Snapshots als Lösung (Momento Pattern) • ABER: Events niemals ändern/löschen!

#WISSENTEILEN

CQRS & Event Sourcing

#WISSENTEILEN

CQRS & Event Sourcing

#WISSENTEILEN

CQRS & Event Sourcing

#WISSENTEILEN

CQRS & Event Sourcing

Still timefor a Demo?

#WISSENTEILEN

CQRS & Event Sourcing

(siehehttps://blog.jayway.com/2013/03/08/aggregates-event-sourcing-distilled/)

Muss das denn so kompliziert sein?

CQRS & Event Sourcing

#WISSENTEILEN

JA! Denn, ...

• es ist extrem schnell für den Anwender• es ist extrem einfach skalierbar• es ist extrem einfach erweiterbar• das Domänen-Modell bleibt konsistent

• Fehler können rekonstruiert werden• Auditing & History gibt es quasi geschenkt

#WISSENTEILEN

#WISSENTEILEN

FAZIT

#WISSENTEILEN

FAZIT

#WISSENTEILEN

FAZIT

#WISSENTEILEN

FAZIT

FAZIT

#WISSENTEILEN

Die Benefits

• Fachlichkeit im Fokus (auch im Code!)• Optimierung auf „Komponentenebene“• max. Performance für High-Load Szenarien• getrennte Evolution möglich (fachl./tech.)• detaillierte Nachverfolgung aller Vorgänge• Total Cost of Ownership

FAZIT

#WISSENTEILEN

Die Pitfals

• Komplexität gegenüber CRUD Anwendungen• Performance bei Event-Replay• Seiteneffekte durch Event-Replay / Snapshot• Eventual Consistency in Query Views• Concurrent Writes durch Commands• Ramp-Up Zeit

#WISSENTEILEN

Two modelsto rule them all.

FAZIT

#WISSENTEILEN

Focus on Business first!

FAZIT

#WISSENTEILEN

Schrei nach Consistencyist meist das Fehlen eines

fachlichen Plan B!

FAZIT

#WISSENTEILEN

Microservices & JEE?Best Friends Forever!

FAZIT„BFF“

Best Friends Forever!

#WISSENTEILEN

#WISSENTEILEN

? # !

LARS RÖWEKAMPCIO NEW TECHNOLOGIES

lars.roewekamp@openknowledge.de+49 (0) 441 4082 – 0

@mobileLarson@_openknowledge

OFFENKUNDIGGUT

KOTAKT

#WISSENTEILEN

#80: © marekuliasz– shutterstock.com#1, #45, #79: pixabay.com

Icons in this presentation designed by “Freepik”,

BILDNACHWEISE

#WISSENTEILEN