SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Downloaden Sie, um offline zu lesen
CQRS &
Event Sourcing
@jankronquist
Who am I? @jankronquist
Outline
CQRS!
Event sourcing!
Design & Code!!
Functional programming
CQRS
Consistency
User A
User B
Get
Get
Buy!
Buy!
!
One ticket left!
One ticket left!
User C Get Buy!
!
One ticket left!
User D Get
No tickets!
Consistency
User A
User B
Get
Get
Buy!
Buy!
!
One ticket left!
One ticket left!
User C Get Buy!
!
One ticket left!
User D Get
No tickets!
Eventual Consistency
User A
User B
Get
Get
Buy!
Buy!
!
One ticket left!
One ticket left!
User C Get Buy!
!
One ticket left!
User D Get
No tickets!
Buy!
!
One ticket left!
CQRS
Command Query Responsibility Segregation!
Commands - action that will modify state!
Queries - anything that view state!
The handling of commands and queries are separated
ProjectionProjection
Common CQRS Architecture
API
Aggregate
Message Bus
Projection
Commands Queries
EventsEvents
Not possible to view!
Micro Service Architecture
API Gateway
Service A
Message Bus
Service B
Request Request
MessageMessage
Validation vs Business rules
Validation!
Correct structure!
Ranges, lengths, types!
Rules!
Should we do this?!
What should happen
When creating the
command
When executing the
command
CQRS scalability
Queries!
Add more nodes for linear scalability!!
Commands!
Partitioning!
Process asynchronously
Event sourcing
Event vs Command
Event - Something that has happened!
UserCreatedEvent!
FriendRequestReceivedEvent!
TicketSoldEvent!
Command - Something we want to happened!
CreateUserCommand!
SendFriendRequestCommand!
BuyTicketCommand
Event sourcing
All changes recorded as Events!
Order (time) is preserved!
The event log is THE persistent source
Aggregates
Unit of consistency!
Example: user, meeting, order, product, game
MeetingCreated
AttendeeAdded A
AttendeeAdded B
AttendeeRemoved A
AttendeeAdded C
Attendees:A
Attendees:A & B
Attendees: B
Attendees: B & C
Event	

Stream
Application Service
Aggregate
Command Handler
Event Handler
Inside the aggregate
Command Handler
Command
Old events
Event Handler
New events
Event Store
Event Sourcing Benefits
History of changes!
Reset & replay!
Persisting changes is simple & fast
Example domain
Rock - Paper - Scissors
Opening gambit
http://www.worldrps.com/gambit-play
Opening gambit
http://www.worldrps.com/gambit-play
Lizard - Spock?
http://rps.com
The future Facebook of Rock Paper Scissors!
Millions of users!
Many games per user
Lets design!
Playing the game
Player A
Player B
Server
rock
paper
Player B: paper
Player A: rock
Game 123
Game 123	

winner: Player B	

loser: Player A
CREATED WAITING
GAME WON
GAME TIED
any move
other move	

(victory)
other move	

(tie)
T
CQRS & Event Sourcing Explained with Examples

Weitere ähnliche Inhalte

Ähnlich wie CQRS & Event Sourcing Explained with Examples

Moving beyond request reply - designing smarter APIs
Moving beyond request reply - designing smarter APIsMoving beyond request reply - designing smarter APIs
Moving beyond request reply - designing smarter APIsBernd Ruecker
 
Architecting eCommerce APIs - Gluecon 13
Architecting eCommerce APIs - Gluecon 13Architecting eCommerce APIs - Gluecon 13
Architecting eCommerce APIs - Gluecon 13Saranyan Vigraham
 
How to CQRS in node: Eventually Consistent, Distributed Microservice Systems..
How to CQRS in node: Eventually Consistent, Distributed Microservice Systems..How to CQRS in node: Eventually Consistent, Distributed Microservice Systems..
How to CQRS in node: Eventually Consistent, Distributed Microservice Systems..Matt Walters
 
[Kong summit 2019] Egress Gateway Pattern - Zhuojie Zhou
[Kong summit 2019] Egress Gateway Pattern - Zhuojie Zhou[Kong summit 2019] Egress Gateway Pattern - Zhuojie Zhou
[Kong summit 2019] Egress Gateway Pattern - Zhuojie Zhou00zzj
 
Building event-driven Microservices with Kafka Ecosystem
Building event-driven Microservices with Kafka EcosystemBuilding event-driven Microservices with Kafka Ecosystem
Building event-driven Microservices with Kafka EcosystemGuido Schmutz
 
The ilities of software engineering.pptx
The ilities of software engineering.pptxThe ilities of software engineering.pptx
The ilities of software engineering.pptxMonica Beckwith
 
Realtime collaboration with Clojure - EuroClojure - Barcelona, 2015
Realtime collaboration with Clojure - EuroClojure - Barcelona, 2015Realtime collaboration with Clojure - EuroClojure - Barcelona, 2015
Realtime collaboration with Clojure - EuroClojure - Barcelona, 2015Leonardo Borges
 
(ARC317) Maintaining a Resilient Front Door at Massive Scale | AWS re:Invent ...
(ARC317) Maintaining a Resilient Front Door at Massive Scale | AWS re:Invent ...(ARC317) Maintaining a Resilient Front Door at Massive Scale | AWS re:Invent ...
(ARC317) Maintaining a Resilient Front Door at Massive Scale | AWS re:Invent ...Amazon Web Services
 
Connecting Apps, Devices and Services
Connecting Apps, Devices and ServicesConnecting Apps, Devices and Services
Connecting Apps, Devices and ServicesDamir Dobric
 
API Strategy Austin - App-centric vs Job-centric Microservices
API Strategy Austin - App-centric vs Job-centric MicroservicesAPI Strategy Austin - App-centric vs Job-centric Microservices
API Strategy Austin - App-centric vs Job-centric MicroservicesIvan Dwyer
 
UContracts a DSL to document and validate structural requirements of frameworks
UContracts a DSL to document and validate structural requirements of frameworksUContracts a DSL to document and validate structural requirements of frameworks
UContracts a DSL to document and validate structural requirements of frameworksAngela Lozano
 
Stateless authentication for microservices
Stateless authentication for microservicesStateless authentication for microservices
Stateless authentication for microservicesAlvaro Sanchez-Mariscal
 
Big Data And HTML5 (DevCon TLV 2012)
Big Data And HTML5 (DevCon TLV 2012)Big Data And HTML5 (DevCon TLV 2012)
Big Data And HTML5 (DevCon TLV 2012)Ido Green
 
Broker: adventure through architecture pattern
Broker: adventure through architecture patternBroker: adventure through architecture pattern
Broker: adventure through architecture patternPivorak MeetUp
 
Building Event Driven (Micro)services with Apache Kafka
Building Event Driven (Micro)services with Apache KafkaBuilding Event Driven (Micro)services with Apache Kafka
Building Event Driven (Micro)services with Apache KafkaGuido Schmutz
 
Voxxed Days Microservices Paris 2018 - Keynote
Voxxed Days Microservices Paris 2018 - KeynoteVoxxed Days Microservices Paris 2018 - Keynote
Voxxed Days Microservices Paris 2018 - KeynoteSusanne Kaiser
 
Runbook Automation: Old News or a Key to Unlock Performance? [DOES2020]
Runbook Automation: Old News or a Key to Unlock Performance? [DOES2020]Runbook Automation: Old News or a Key to Unlock Performance? [DOES2020]
Runbook Automation: Old News or a Key to Unlock Performance? [DOES2020]Rundeck
 
DDD Europe 2019: Lost in transaction
DDD Europe 2019: Lost in transactionDDD Europe 2019: Lost in transaction
DDD Europe 2019: Lost in transactionBernd Ruecker
 
MongoDB Mobile
MongoDB Mobile MongoDB Mobile
MongoDB Mobile MongoDB
 

Ähnlich wie CQRS & Event Sourcing Explained with Examples (20)

Moving beyond request reply - designing smarter APIs
Moving beyond request reply - designing smarter APIsMoving beyond request reply - designing smarter APIs
Moving beyond request reply - designing smarter APIs
 
Architecting eCommerce APIs - Gluecon 13
Architecting eCommerce APIs - Gluecon 13Architecting eCommerce APIs - Gluecon 13
Architecting eCommerce APIs - Gluecon 13
 
How to CQRS in node: Eventually Consistent, Distributed Microservice Systems..
How to CQRS in node: Eventually Consistent, Distributed Microservice Systems..How to CQRS in node: Eventually Consistent, Distributed Microservice Systems..
How to CQRS in node: Eventually Consistent, Distributed Microservice Systems..
 
[Kong summit 2019] Egress Gateway Pattern - Zhuojie Zhou
[Kong summit 2019] Egress Gateway Pattern - Zhuojie Zhou[Kong summit 2019] Egress Gateway Pattern - Zhuojie Zhou
[Kong summit 2019] Egress Gateway Pattern - Zhuojie Zhou
 
Building event-driven Microservices with Kafka Ecosystem
Building event-driven Microservices with Kafka EcosystemBuilding event-driven Microservices with Kafka Ecosystem
Building event-driven Microservices with Kafka Ecosystem
 
The ilities of software engineering.pptx
The ilities of software engineering.pptxThe ilities of software engineering.pptx
The ilities of software engineering.pptx
 
Realtime collaboration with Clojure - EuroClojure - Barcelona, 2015
Realtime collaboration with Clojure - EuroClojure - Barcelona, 2015Realtime collaboration with Clojure - EuroClojure - Barcelona, 2015
Realtime collaboration with Clojure - EuroClojure - Barcelona, 2015
 
Cqrs & event sourcing
Cqrs & event sourcingCqrs & event sourcing
Cqrs & event sourcing
 
(ARC317) Maintaining a Resilient Front Door at Massive Scale | AWS re:Invent ...
(ARC317) Maintaining a Resilient Front Door at Massive Scale | AWS re:Invent ...(ARC317) Maintaining a Resilient Front Door at Massive Scale | AWS re:Invent ...
(ARC317) Maintaining a Resilient Front Door at Massive Scale | AWS re:Invent ...
 
Connecting Apps, Devices and Services
Connecting Apps, Devices and ServicesConnecting Apps, Devices and Services
Connecting Apps, Devices and Services
 
API Strategy Austin - App-centric vs Job-centric Microservices
API Strategy Austin - App-centric vs Job-centric MicroservicesAPI Strategy Austin - App-centric vs Job-centric Microservices
API Strategy Austin - App-centric vs Job-centric Microservices
 
UContracts a DSL to document and validate structural requirements of frameworks
UContracts a DSL to document and validate structural requirements of frameworksUContracts a DSL to document and validate structural requirements of frameworks
UContracts a DSL to document and validate structural requirements of frameworks
 
Stateless authentication for microservices
Stateless authentication for microservicesStateless authentication for microservices
Stateless authentication for microservices
 
Big Data And HTML5 (DevCon TLV 2012)
Big Data And HTML5 (DevCon TLV 2012)Big Data And HTML5 (DevCon TLV 2012)
Big Data And HTML5 (DevCon TLV 2012)
 
Broker: adventure through architecture pattern
Broker: adventure through architecture patternBroker: adventure through architecture pattern
Broker: adventure through architecture pattern
 
Building Event Driven (Micro)services with Apache Kafka
Building Event Driven (Micro)services with Apache KafkaBuilding Event Driven (Micro)services with Apache Kafka
Building Event Driven (Micro)services with Apache Kafka
 
Voxxed Days Microservices Paris 2018 - Keynote
Voxxed Days Microservices Paris 2018 - KeynoteVoxxed Days Microservices Paris 2018 - Keynote
Voxxed Days Microservices Paris 2018 - Keynote
 
Runbook Automation: Old News or a Key to Unlock Performance? [DOES2020]
Runbook Automation: Old News or a Key to Unlock Performance? [DOES2020]Runbook Automation: Old News or a Key to Unlock Performance? [DOES2020]
Runbook Automation: Old News or a Key to Unlock Performance? [DOES2020]
 
DDD Europe 2019: Lost in transaction
DDD Europe 2019: Lost in transactionDDD Europe 2019: Lost in transaction
DDD Europe 2019: Lost in transaction
 
MongoDB Mobile
MongoDB Mobile MongoDB Mobile
MongoDB Mobile
 

Kürzlich hochgeladen

Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 

Kürzlich hochgeladen (20)

Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 

CQRS & Event Sourcing Explained with Examples