Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Welcome to the (state) machine @ ExploreDDD 2019

66 views

Published on

Stateless all the thing, they say. In the last few years we’ve been brainwashed: design stateless systems, otherwise they cannot scale, they cannot be highly available, and they are hard to maintain and evolve. In a nutshell stateful is bad. However complex software systems need to do collaborative processing, that is stateful by definition. Stateless myth busted! Collaborative domains deal with long running business transactions and need to interact with distributed resources. The traditional distributed transactions approach, even if tempting, is a time bomb.

This is when Sagas come into play. Sagas allow to model complex collaborative domains without the need for distributed transactions and/or orchestration across multiple resources. Join Mauro on a journey that aims to disclose what sagas are, how they can be used to model a complex collaborative domain, and what role they play when it comes to designing systems with failure and eventual consistency in mind.

(It’s all right, I know where you’ve been)

Published in: Software
  • Be the first to comment

  • Be the first to like this

Welcome to the (state) machine @ ExploreDDD 2019

  1. 1. welcome to the (state) machine It’s all right, I know where you’ve been mauroservienti Mauro Servienti
  2. 2. All I want to do when I wake up in the morning is… (cit. Rosanna, Toto, Toto IV) mauroservienti
  3. 3. mauroservienti
  4. 4. buy ticket(s) stage dive… :-) mauroservienti
  5. 5. All what they want to do is… mauroservienti
  6. 6. mauroservienti
  7. 7. display available tickets reserve ticket(s) charge my credit card ship ticket(s) mauroservienti
  8. 8. sometimes order doesn’t matter sometimes it does! mauroservienti
  9. 9. Naïve way var seats = await DisplayAvailableSeats(); var selectedSeats = seats.Take(2); var reservation = await ReserveSeats(selectedSeats); var authorization = await AuthorizeCard(reservation); if (authorization.Succeeded) { await ChargeCard(authorization); await ShipTickets(reservation); } mauroservienti
  10. 10. Naïve way var seats = await DisplayAvailableSeats(); var selectedSeats = seats.Take(2); var reservation = await ReserveSeats(selectedSeats); var authorization = await AuthorizeCard(reservation); if (authorization.Succeeded) { await ChargeCard(authorization); await ShipTickets(reservation); } Remote requests mauroservienti Remote requests
  11. 11. Tickets Selection Credit Card Gateway/ACL Orders Manager Order Checked-out AuthorizeCard request/response Reservation ConfirmTickets request/response Credit Card Gateway/ACL ChargeCard request/response Shipping Gateway/ACL Shiptickets request/response mauroservienti Reservation SelectTickets request/response Process Managers
  12. 12. Tickets Selection Credit Card Gateway/ACL Orders Manager Checkout request AuthorizeCard request/response Reservation ConfirmTickets request/response Credit Card Gateway/ACL ChargeCard request/response Shipping Gateway/ACL Shiptickets request/response Some sort of orchestration is still required mauroservienti Reservation SelectTickets request/response Process Managers
  13. 13. Orchestration is not the only issue we should get rid of…
  14. 14. Process Managers (a different point of view) New requirement: collect tickets at the venue OrderId ShippingId ShippingStatus Etc 12 1337 Delivered … 58 1338 Pending … Orders table mauroservienti
  15. 15. mauroservienti like a punch card Process Managers own the process
  16. 16. Ticket(s) Insurance mauroservienti
  17. 17. Process Managers, a retrospective. •Violate Single Responsibility Principle •Single Unit of Deployment •Conflicting Changes/Merge Conflicts •Contention/Performance Bottleneck mauroservienti
  18. 18. mauroservienti there is no Spoon Process Manager
  19. 19. chop up mauroservienti
  20. 20. Autonomy Collaborative Domains mauroservienti
  21. 21. sagas autonomy in collaborative domains mauroservienti
  22. 22. multiple workflows, each providing compensating actions for every step of the workflow where it can fail mauroservienti
  23. 23. multiple workflows, each providing compensating actions for every step of the workflow where it can fail mauroservienti
  24. 24. reserve ticket(s) charge my credit card ship tickets mauroservienti
  25. 25. Shipping Finance Tickets Ordering Saga mauroservienti ReservationReservation Checked-out Payment Authorized Order Created Payment Succeeded Available Tickets Select tickets Shopping Cart Proceed to checkout workflow workflow workflow
  26. 26. Shipping Finance Tickets Ordering Saga mauroservienti ReservationReservation Checked-out Payment Authorized Order Created Payment Succeeded Available Tickets Select tickets Shopping Cart Proceed to checkout Policy Policy Policy
  27. 27. Shipping Finance mauroservienti Reservation Available Tickets Select tickets Shopping Cart Proceed to checkout Policy Policy Policy Reservation Checked-out Payment Authorized Order Created Payment Succeeded Pivotal Events
  28. 28. mauroservienti Let’s open the black box
  29. 29. Shipping ReservationPayment Gateway Finance Finance Initiate Payment Authorize Card Courier Gateway Tickets Ordering Saga mauroservienti Available Tickets Select tickets Reservation Shopping Cart Proceed to checkout Reservation Reservation Checked-out Payment Authorized Order CreatedFinance Payment Gateway Charge Card Payment Succeeded Delivery Pick-up
  30. 30. Sounds more complex than the naïve approach, doesn’t it? mauroservienti
  31. 31. New requirement • Collect tickets at the venue mauroservienti
  32. 32. Shipping ReservationPayment Gateway Finance Finance Initiate Payment Authorize Card Courier Gateway mauroservienti Available Tickets Select tickets Reservation Shopping Cart Proceed to checkout Reservation Reservation Checked-out Payment Authorized Order CreatedFinance Payment Gateway Charge Card Payment Succeeded Delivery Pick-up Shipping Policy
  33. 33. Order Created Payment Succeeded NY Batch Shipping at the Venue Policy Store for Venue Delivery Mark as Complete Courier Gateway Batch Delivery Pick-up Courier Gateway Delivery Pick-up Deliver Tickets Mark as Complete mauroservienti Shipping Policy Shipping Policy Delivery-Mode: - Collect-at-the-Venue - Ship-at-Home ? Is Collect-at-the-Venue?
  34. 34. Order Created Payment Succeeded NY Store for Venue Delivery Mark as Complete Courier Gateway Delivery Pick-up Deliver Tickets Mark as Complete mauroservienti Shipping Policy Shipping Policy Delivery-Mode: - Collect-at-the-Venue - Ship-at-Home ? Is Collect-at-the-Venue? Batch Shipping at the Venue Policy Courier Gateway Batch Delivery Pick-up Mark as Complete
  35. 35. multiple workflows, each providing compensating actions for every step of the workflow where it can fail mauroservienti
  36. 36. Shipping ReservationPayment Gateway Finance Finance Initiate Payment Authorize Card Courier Gateway Tickets Ordering Saga mauroservienti Available Tickets Select tickets Reservation Shopping Cart Proceed to checkout Reservation Reservation Checked-out Payment Authorized Order CreatedFinance Payment Gateway Charge Card Payment Succeeded Delivery Pick-up
  37. 37. Payment Gateway Finance Finance Initiate Payment Authorize Card Payment Policy mauroservienti Reservation Checked-out Payment Authorized Finance Payment Gateway Charge Card Payment Succeeded Do we need a Reservation Failed event?
  38. 38. Payment Gateway Finance Finance Initiate Payment Authorize Card Payment Policy mauroservienti Payment Authorized 48hrs to release money Reservation Checked-out
  39. 39. Reservation Checked-out Payment Gateway Finance Finance Initiate Payment Authorize Card Payment Policy mauroservienti Payment Authorized Finance Payment Gateway Charge Card Payment Succeeded 48hrs to release money Payment Gateway Release Money Reserved?N Y Do nothing… Reservation Order Created 48hrs later… Mark as Complete
  40. 40. What’s a timeout? void Handle(ReservationCheckedout evt) { Send(new AuthorizationRequest(…)); } void Handle(AuthorizationRequest msg) { var authId = await AuthorizeCard(…); Reply(new AuthorizationResponse(authId)); } void Handle(AuthorizationResponse msg) { Publish(new PaymentAuthorized()); RequestTimeout(TimeSpan.FromHours(48)); } void Timeout() { if(NotReserved){ … } } Payment Gateway Finance Authorize Card Payment Authorized 48hrs to release money Reservation Checked-out Reserved? Payment Gateway FinanceFinanceFinance
  41. 41. What’s a timeout? void Handle(ReservationCheckedout evt) { Send(new AuthorizationRequest(…)); } void Handle(AuthorizationRequest msg) { var authId = await AuthorizeCard(…); Reply(new AuthorizationResponse(authId)); } void Handle(AuthorizationResponse msg) { Publish(new PaymentAuthorized()); RequestTimeout(TimeSpan.FromHours(48)); } void Timeout() { if(NotReserved){ … } } Payment Gateway Finance Authorize Card Payment Authorized 48hrs to release money Reservation Checked-out Reserved? Payment Gateway FinanceFinanceFinance
  42. 42. What’s a timeout? void Handle(ReservationCheckedout evt) { Send(new AuthorizationRequest(…)); } void Handle(AuthorizationRequest msg) { var authId = await AuthorizeCard(…); Reply(new AuthorizationResponse(authId)); } void Handle(AuthorizationResponse msg) { Publish(new PaymentAuthorized()); RequestTimeout(TimeSpan.FromHours(48)); } void Timeout() { if(NotReserved){ … } } Payment Gateway Finance Authorize Card Payment Authorized 48hrs to release money Reservation Checked-out Reserved? Payment Gateway FinanceFinanceFinance
  43. 43. Sagas (a different point of view) OrderId ShippingId ShippingStatus Etc 12 1337 Delivered … 58 1338 Pending … Orders table Reservation Shipping Finance OrderId TicketId 12 ABC 58 ACD Reservations table OrderId Address 12 Milan, Italy 58 Paris, France Shipping table OrderId Status 12 Paid 58 Overdue Invoices table VS mauroservienti
  44. 44. Sagas (a different point of view) Reservation Shipping Finance Reservations table OrderId Address 12 Milan, Italy 58 Paris, France Shipping table OrderId Status 12 Paid 58 Overdue Invoices table mauroservienti Each service can evolve independently OrderId TicketId 12 ABC 58 ACD
  45. 45. Sagas •Business Process is distributed •Respect Single Responsibility Principle •Simpler/not conflicting evolution •Independent Units of Deployment •Independent scale out mauroservienti
  46. 46. Every year is getting shorter never seem to find the time… Sagas Demo bit.ly/eddd-state-machine Udi Dahan about Sagas go.particular.net/eddd-state-machine mauroservienti
  47. 47. Demos: bit.ly/eddd-state-machine Videos: go.particular.net/eddd-state-machine Pitfalls • Monitor the (distributed) state mauroservienti
  48. 48. Demos: bit.ly/eddd-state-machine Videos: go.particular.net/eddd-state-machine Takeaways • Behaviors define how to designs processes • Follow the coupling, not the data • Identify processes and chop them up by “service” mauroservienti
  49. 49. Demos: bit.ly/eddd-state-machine Videos: go.particular.net/eddd-state-machine Takeaways • Behaviors define how to designs processes • Use Delayed Messaging to model time • Simplifies making decisions in an async world mauroservienti
  50. 50. there is no such thing as orchestration mauroservienti
  51. 51. @mauroservienti | #EDDD Mauro Servienti Solution Architect @ Particular Software the makers of NServiceBus mauro.servienti@particular.net @mauroservienti //github.com/mauroservienti //milestone.topics.it mauroservienti
  52. 52. @mauroservienti | #EDDD Demos: bit.ly/eddd-state-machine Videos: go.particular.net/eddd-state-machine mauroservienti

×