cqrs + event sourcing

84
CQRS + ES DDD

Upload: mbild

Post on 14-May-2015

2.308 views

Category:

Technology


3 download

DESCRIPTION

CQRS + Event Sourcing @Frankfurt 2013

TRANSCRIPT

Page 1: CQRS + Event Sourcing

CQRS + ESDDD

Page 2: CQRS + Event Sourcing

• Intro!

• CAP

• CQRS

• Event Driven

• Event Sourcing / Event Store

• Appendix

Page 3: CQRS + Event Sourcing

performance trouble?

scalability trouble?

big large data?

high frequency data?

complex business domain?

Page 4: CQRS + Event Sourcing

performance problems

if your system is slow for a single user

Page 5: CQRS + Event Sourcing

scalability problems

if your system is fast for a single user but slow under heavy load

Page 6: CQRS + Event Sourcing

system level performance

maximal throughput

acceptable latency

Page 7: CQRS + Event Sourcing

system level requirements

• fast response time = high availability

• slow response time = low availability

• instant (secure) consistency = ACID (Atomicity, Consistency, Isolation und Durability)

• time window consistency = BASE (Basically Available, Soft state, Eventual consistency)

Page 8: CQRS + Event Sourcing

common mistakes• The network is fail-safe

• The latency time is equal to zero

• The data throughput is infinite

• The network is secure

• The network topology will not change

• There is always only one network administrator

• The cost of data transport can be calculated as zero,

• The network is homogeneous

Page 9: CQRS + Event Sourcing

• Intro

• CAP!

• CQRS

• Event Driven

• Event Sourcing / Event Store

• Appendix

Page 10: CQRS + Event Sourcing

You can only pick 2

At a given point in time!

Consistency

Availability

Partition tolerance

CAP

Page 11: CQRS + Event Sourcing

– Brewer

„…a distributed system cannot satisfy all three of these guarantees at the same time…“

Page 12: CQRS + Event Sourcing

Centralized SystemsIn a centralized system (RDBMS etc.) we don’t have network partitions e.g. P in CAP

you GET BOTH:

Consistency

Availability

Page 13: CQRS + Event Sourcing

Distributed SystemsIn a distributed system we have network partitions e.g. P in CAP

You can get to ONLY PICK ONE:

Consistency

Availability

Page 14: CQRS + Event Sourcing

CAP in practice• … there are only 2 types of systems

• CP!

• AP!

• … there is only one choice. In case of existing network partitions, what do you prefer?

1. Consistency

2. Availability

Page 15: CQRS + Event Sourcing

BASE Eventual consistency

Consistency of data is seen as a state which is eventually attained.

Page 16: CQRS + Event Sourcing

CA CP

AP

Where are you?

Page 17: CQRS + Event Sourcing

SOFTWARE SYSTEM

valid correct solid

data driveneasy & fast

flexible modern

task driven

different needssystem level

Page 18: CQRS + Event Sourcing

different needs

UI usability

Data-Driven Task-based

Page 19: CQRS + Event Sourcing

• Reporting / Search? • State Changes? • Business behaviors and rules? • Algorithms / Calculations? • ORM + rich domain model is a

anti pattern! • The reading of an object ended

very often in charge of the entire database.

Data Modeldifferent needs

Page 20: CQRS + Event Sourcing

Storage

• When do you need ACID? • When Eventually

Consistent better fit? • Should Reporting / Search

strictly consistent?

Scaling out READS to a RDBMS is hard (sharing/replication) Scaling out WRITES to a RDBMS is impossible!

different needs

Page 21: CQRS + Event Sourcing

–Greg Young 2008

„A single model cannot be appropriate for reporting, searching and transactional

behavior.“

So… one fits all?

Page 22: CQRS + Event Sourcing

CA CP

AP

needsmultiple systems Data Administration Bank Service / ATM NoSQL

Web-/Mobile End-Users DNS Web Caching

single system RDBMS Data Analytics

Page 23: CQRS + Event Sourcing

CA

CPAP

Page 24: CQRS + Event Sourcing

Sometimes!

Do you really need a RDBMS?

But many times we don’t!

Page 25: CQRS + Event Sourcing

application building (today)

ACID <vs.> EVENTUAL CONSISTENCY

CUD <vs.> BEHAVIORAL DOMAIN

READ <vs.> SEARCH / REPORTS

DATA DRIVEN <vs.> TASK BASED

Page 26: CQRS + Event Sourcing

Think about your data: Different data need different guarantees

Page 27: CQRS + Event Sourcing

• Intro

• CAP

• CQRS!

• Event Driven

• Event Sourcing / Event Store

• Appendix

Page 28: CQRS + Event Sourcing

CQRScommand query responsibility segregation

Page 29: CQRS + Event Sourcing

conceptual

strictly segregates the responsibility

independent optimization and specialization

asymmetrical execution of commands & queries

Page 30: CQRS + Event Sourcing

„Do your domain experts speak with only four verbs (Create Read Update Delete)?“

Page 31: CQRS + Event Sourcing

CRUD & OOP

Page 32: CQRS + Event Sourcing

CQSBertrand Meyer's

split interfaces into

state changes Commands

side-effect free Queries

Page 33: CQRS + Event Sourcing

simple pattern

• side-effect-free query/read access

• autonomous commands represent behavioral state changes

Page 34: CQRS + Event Sourcing

–Greg Young 2008

„A single model cannot be appropriate for reporting, searching and transactional behavior.“

That’s it!

Page 35: CQRS + Event Sourcing

CRUD <—> CQRS

Page 36: CQRS + Event Sourcing

CRUD <—> CQRS

Page 37: CQRS + Event Sourcing

conceptual

@Microsoft

Page 38: CQRS + Event Sourcing

CQRS

side-effect free processing of queries

execution of commands represent changes in the state of the software system

Page 39: CQRS + Event Sourcing

scaling benefits

host the two services (read / write) differently

we can host the read service on 25 servers

write service on one server

Page 40: CQRS + Event Sourcing

read side benefits

• top-down driven and optimized view data model

• denormalized schema - no joins

• easy queries (select * from DB)

• no mapping

• no ORM mismatches

Page 41: CQRS + Event Sourcing

write side benefits

• behavioral driven

• exposes only functionality

• no read access

• (event driven) state machine

• forms a consistency boundary

Page 42: CQRS + Event Sourcing

• Intro

• CAP

• CQRS!

• Event Driven !

• Event Sourcing / Event Store

• Appendix

Page 43: CQRS + Event Sourcing

Event-Driven

@Microsoft

Page 44: CQRS + Event Sourcing

–Eric Evans, 2009

„It’s really become clear to me in the last couple of years that we need a new building block and

that is the Domain Events.“

Events

–Greg Young, 2008

„State transitions are an important part of our problem space and should be modeled within

our domain“

Page 45: CQRS + Event Sourcing

*syntax notescommands

events

domain events

business events

notifications

Messages

Page 46: CQRS + Event Sourcing

events

something that has happened in the past

CustomerHandled CargoShipped

Page 47: CQRS + Event Sourcing

commands

something that should do named in imperative

ChangeCustomerLocale MakeCustomerPreferred

Page 48: CQRS + Event Sourcing

storing structure

Page 49: CQRS + Event Sourcing

events & commands• uniquely identifiable • self contained • pure data structure - no behavior • Observable • Time relevant • non-blocking - async execution • conceptual immutable e.g. new, deep copy,

clone • events represent serialized function calls • conceptual duality between function calls and

messages

Page 50: CQRS + Event Sourcing

flow

Page 51: CQRS + Event Sourcing

flow in a nutshell• Write side receive Commands and publish

Events

• All state changes are represented by Business Events

• Read side is updated as a result of the published Events

• All Queries go directly to the Reporting, the Business-Logic is not involved

Page 52: CQRS + Event Sourcing

flow

Page 53: CQRS + Event Sourcing

Common benefits• Focus to Business

• allow to manage business and technology risks separately

• Technology agnostic, platform neutral and easy externals integration

• Fully encapsulated domain that only exposes behavior

• Queries do not use the business model

• No object-relational (ORM) impedance mismatch

• Scalable and Flexible and Testable

• Reduced complexity and simplify system architecture

Page 54: CQRS + Event Sourcing

benefits Event-Driven

• Explicit error/failure modeling

• Easy integration with external systems

• Location Transparency

• Reactive Behavior

• Bullet-proof auditing and historical tracing

Page 55: CQRS + Event Sourcing

Event Aggregator

• publish / subscribe pattern • observe events • dispatch / invoke functions sync or async

Page 56: CQRS + Event Sourcing

Command-Handler

• execute functions by command (type) • integration point for cross-cutting concerns (logging,

transactions, validating, authorization)

Page 57: CQRS + Event Sourcing

Event-Handler

• execute functions by event (type) • view or data model generation • schema denormalizer

Page 58: CQRS + Event Sourcing

trade-offs• initially feels complex

• Consistency Boundaries (isn’t a top level architecture)

• Monitoring / Heartbeats

• Stale Data

• Collaborative Domains

• Data Integration

• Modeling with time

• Manage idempotency and event de-duplication

Page 59: CQRS + Event Sourcing

• Intro

• CAP

• CQRS

• Event Driven

• Event Sourcing / Event Store!

• Appendix

Page 60: CQRS + Event Sourcing

Event Sourcing

Page 61: CQRS + Event Sourcing

Event SourcingStoring Deltas

Page 62: CQRS + Event Sourcing

Event Sourcingreplaying events

Page 63: CQRS + Event Sourcing

Event StoreEvent Store

Event Sourcerecord

meta data time stamp

payload identity version

append only log

replay

Produce

Consume #1

#5

#9

Page 64: CQRS + Event Sourcing

Event Sourcing

Every state change is materialized in an Event All Events are sent to an EventProcessor EventProcessor stores all events in an Event Log System can be reset and Event Log replayed Many different EventListeners can be added to EventProcessor (or listen directly on the Event Log) No need for ORM, just persist the Events

Page 65: CQRS + Event Sourcing

Event Sourcing

Page 66: CQRS + Event Sourcing

recover state

1. EventProcessor record & replay all events in the context of a consistency boundary

2. projections - loop over a series of events and aggregate to a state

f(state, event) → state

Page 67: CQRS + Event Sourcing

Event ProcessorRestoring State from Deltas

• Event Processor are tracking events as to what has changed

• Current state is constructed by replaying events

• Data is not persisted in a structure but as a series of transactions

• No ORM is needed

Page 68: CQRS + Event Sourcing

Projections

Query language of Event Streams

Filter, Map, Reduce over Event Streams

Projection is about deriving current state from the stream of events

Given the stream of events, we can transform them to any structural representation

Page 69: CQRS + Event Sourcing
Page 70: CQRS + Event Sourcing
Page 71: CQRS + Event Sourcing
Page 72: CQRS + Event Sourcing

• Recording all state changes • is a business transaction history • Event History + Time Machine • Replay summary of total records is the last state • any regeneration of models • projections as a useful event stream query language • time related analysis and statistics (forecastings)

Event Sourcingbenefits

Page 73: CQRS + Event Sourcing

Event Sourcingbenefits

• No object-relational impedance mismatch • time ordered series of business transactions • Support future ways of looking at data • Performance and scalability • Testability • Reconstruct production scenarios • Long-lived processes, tracking and auditability

Page 74: CQRS + Event Sourcing

• Intro

• CAP

• CQRS

• Event Driven

• Event Sourcing / Event Store

• Appendix

Page 75: CQRS + Event Sourcing

Thanks Questions?

Page 76: CQRS + Event Sourcing

appendix• messaging pattern

• idempotency and de-duplication

Page 77: CQRS + Event Sourcing

Publish / Subscribe

Messaging Pattern

Page 78: CQRS + Event Sourcing

Store and Forward

Messaging Pattern

Page 79: CQRS + Event Sourcing

Request-Reply

Messaging Pattern

Page 80: CQRS + Event Sourcing

Point-to-Point

Messaging Pattern

Page 81: CQRS + Event Sourcing

f(x) = f(f(x))request twice means the same outcome

Idempotency

Page 82: CQRS + Event Sourcing

Idempotency

• natural idempotent operations are mostly easy

• nicely for state machines with alter state without side-effects

• turn the light ON - duplicate calls generate some result - the light is on

• not naturally idempotent operations are hard

• only process ones that I see are new

• Balance += Amount - increases balance again and again

Page 83: CQRS + Event Sourcing

Idempotency

• de-duplicated messages by query a log store • check incoming message by an identifier agains a log store

and reject if a identifier match • check against a correlation identifier (e.g. ETags,

TransactionID) • check against a computed HASH of the complete message

(like GIT commits)

Page 84: CQRS + Event Sourcing

Idempotency

• using a buffer

• time throttling