Commands and events. And intent. „Commands are all about intent. - Jonas Bonér @martinschimak Events are intentless.“ Command Retrieve payment Payment retrieved Event
Commands and events. Like two sides of a coin. Payment retrieved Payment retrieval Command Event Execution Something should happen Something happens Something happened Retrieve payment @martinschimak
Commands and events. Flipping the coin ... Payment retrieved Order fulfillment process Event Execution Something happens Something happened @martinschimak Command Something should happen Fetch goods
Events to temporally decouple services ... Order summary Payment retrieved Payment UI raises projected onto Payment Service Order Service ... CQRS with „foreign“ events! @martinschimak
Events to represent history and state of systems ... Payment received Payment Payment received Payment received Payment received Payment retrieved ... Event Sourcing! @martinschimak
Events to enable whole business processes ... ... Event choreographies! Payment received Inform customer listened to by Payment Service Notification Service @martinschimak
Item ordered My context of sophisticated fulfillments Energy traded Order fulfillment Energy trade execution Coverage extension requested Policy issuance @martinschimak
New pair of shoes ordered End-to-end business processes and their phases New pair of shoes ordered Old pair of shoes worn out Order new pair of shoes New pair of shoes delivered Job Execution History Customer-driven Supplier-driven Supplier-exploring Human to Machine Supporting Service Customer-determined Machine to Machine Automated Service @martinschimak
A company‘s „long-running“ order fulfillment service Order new pair of shoes New pair of shoes delivered Command Event “Long-running” service Order fulfillment service Supplier-driven Customer-determined Machine to Machine Automated Service @martinschimak
„Long-running“ services have long-running contracts Order new pair of shoes New pair of shoes delivered Command Event Order fulfillment service “Long-running” service “Long-running” asynchronous contract Hours to weeks @martinschimak
„Long-running“ services may have internal customers Retrieve payment Payment retrieved Payment service “Long-running” service Command Event Seconds to weeks @martinschimak “Long-running” asynchronous contract
Order Service Order placed Payment Service Payment retrieved Inventory Service Goods fetched Shipment Service Goods shipped The choreography principle @martinschimak
This is how the story is often presented … Choreo- graphy Orches- tration Process logic Process control explicit central implicit decentral Service coupling high low @martinschimak
Order Service Order placed Payment Service Payment retrieved Inventory Service Goods fetched Shipment Service Goods shipped Choreographies and their implicit process logic ... @martinschimak
Order Service Payment Service Inventory Service Shipment Service Order placed Goods shipped ... can lead to a hidden form of accidental coupling Payment Service Inventory Service Goods fetched Payment retrieved ... to change implicit process logic ... Change listener 1 ... Change listener 2 ... Change listener 3 ... @martinschimak
Payment Service Inventory Service Shipment Service Orchestration and its explicit process logic ... Order Service Fetch goods Retrieve payment Ship goods ... does not display THIS form of hidden accidental coupling...
This is how the story develops … Choreo- graphy Orches- tration Process logic Process control explicit central implicit decentral Service coupling high low Not necessarily Not necessarily @martinschimak
Let‘s dig a bit deeper and look into this ... Choreo- graphy Orches- tration Process logic Process control explicit central implicit decentral Service coupling high low Not necessarily Not necessarily @martinschimak
Payment Service Inventory Service Shipment Service Why not asynchronous commands? „Doable“ ... Order Service Fetch goods Retrieve payment Ship goods Request Async Response Request Async Response Async Response Request
How unhealthy is this apple ... Maybe Synchronous Request and Response Style Sender/Receiver Exchange Probably Transient Asynchronous by Nature Fire and Forget Style Publish/Subscribe Exchange Probably Persistent Command Event Or Asynchronous! @martinschimak
? ? ? Payment Service Inventory Service Shipment Service Why not fire the command? And listen to an event? Order Service Fetch goods Retrieve payment Ship goods Payment retrieved Goods fetched Goods shipped Order fulfilled Fire Fire Fire Listen Listen Listen
How unhealthy is this apple ... Maybe Synchronous Request and Response Style Sender/Receiver Exchange Probably Transient Asynchronous by Nature Fire and Forget Style Publish/Subscribe Exchange Probably Persistent Command Event Or Asynchronous! Like Fire and Listen? @martinschimak
? Payment Service ... we get location transparency inside a context ... Order Service Retrieve payment Payment retrieved Publish Subscribe Financial Services Bounded Context @martinschimak
? Payment Service ... and the ability to subscribe to „everything“. Order Service Retrieve payment Payment retrieved Publish Subscribe Financial Services Bounded Context Subscribe Subscribe Business Monitoring Bounded Context „Order Context wanted payment to happen!“ „Financial Context retrieved payment!“
The apple doesn’t look so unhealthy anymore ... Maybe Synchronous Request and Response Style Sender/Receiver Exchange Probably Transient Asynchronous by Nature Fire and Forget Style Publish/Subscribe Exchange Potentially Persistent Command Event Or Asynchronous! Like Fire and Listen? Like Publish and Promise to Subscribe? @martinschimak
Hey DDD! Do you like purely semantical differences? :-) We specified an intended job This just models our “state of mind” We use imperative tense to express intent Defined by supplier bounded context, created by customer system We created a relevant change This models everybody’s “state of world” We use past tense to express change Defined and created by emitter bounded context and system Command Event @martinschimak
? Payment Service Related semantics are both defined in supplier context Retrieve payment Payment retrieved Financial Services Bounded Context „When YOU publish this WE promise to subscribe and act!“ „When WE publish this YOU decide to subscribe and act!“ Command Event @martinschimak Make the contract explicit!
? Payment Service Mix and match decentral orchestration and choreography Retrieve payment Payment retrieved Financial Context Command Event Order Service Fulfillment Context Payment retrieval failed Event Marketing Context Customer promoted Promotions service Event @martinschimak
New pair of shoes ordered It‘s a supplier‘s responsibility to act on customer intent New pair of shoes ordered Retrieve payment Payment retrieved Command Event(s) Events Customer Supplier Payment Service @martinschimak
New pair of shoes ordered It‘s a customer‘s responsibility to specify intent New pair of shoes ordered Retrieve payment Command Event(s) Events Customer Supplier Order placed Order Service @martinschimak
New pair of shoes ordered Some responsibilities span both phases, but not all New pair of shoes ordered Promote customer Command Event(s) Events Payment retrieved Promotions service Customer promoted Promotions service @martinschimak
Orchestration and choreography... „it really depends“. Choreo- graphy Orches- tration Process logic Process control explicit central implicit decentral Service coupling high low Not necessarily Not necessarily Not necessarily Not necessarily @martinschimak
Embed and contain state managing business logic! Retrieve payment Payment retrieved Payment service “Long-running” service Seconds to weeks Payment retrieval failed Command Event(s) @martinschimak
Why not ... persisting commands, too? Maybe Synchronous Request and Response Style Sender/Receiver Exchange Probably Transient Asynchronous by Nature Fire and Forget Style Publish/Subscribe Exchange Probably Persistent Command Event Or Asynchronous! Like Fire and Listen? Like Publish and Promise to Subscribe? @martinschimak
An eventsourced aggregate ... (1) public List process(RetrievePaymentCommand cmd) { return EventUtil.events( new PaymentRequestedEvent(...) ); } @martinschimak
An eventsourced saga pattern ... (2) public List process(RetrievePaymentCommand cmd) { return EventUtil.events( new PaymentRequestedEvent(...), new WithdrawAmountCommand(...) ); } @martinschimak
Payment retrieval Payment requested Order fulfillment Credit card Account Amount withdrawn Withdraw amount Payment partly covered Credit Card charged Charge credit card Payment retrieved Order placed Retrieve payment Order fulfilled Order items “Fact”sourced sagas know the flow of events and commands created by itself. @martinschimak