際際滷

際際滷Share a Scribd company logo
welcome to the
(state) machine
Its all right, I know where youve been
mauroservienti
Mauro Servienti
All I want to do when I wake up in
the morning is
(cit. Rosanna, Toto, Toto IV)
mauroservienti
mauroservienti
buy ticket(s)
stage dive :-)
mauroservienti
All what they want to do is
mauroservienti
mauroservienti
display available tickets
reserve ticket(s)
charge my credit card
ship ticket(s)
mauroservienti
sometimes order doesnt matter
sometimes it does!
mauroservienti
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
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
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
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
Orchestration is not the only
issue we should get rid of
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
mauroservienti
like a punch card
Process Managers
own the process
Ticket(s)
Insurance
mauroservienti
Process Managers, a retrospective.
Violate Single Responsibility Principle
Single Unit of Deployment
Conflicting Changes/Merge Conflicts
Contention/Performance Bottleneck
mauroservienti
mauroservienti
there is no Spoon
Process Manager
chop
up
mauroservienti
Autonomy
Collaborative Domains
mauroservienti
sagas autonomy in
collaborative domains
mauroservienti
multiple workflows,
each providing
compensating
actions for every
step of the
workflow where it
can fail
mauroservienti
multiple workflows,
each providing
compensating
actions for every
step of the
workflow where it
can fail
mauroservienti
reserve ticket(s)
charge my credit card
ship tickets
mauroservienti
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
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
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
mauroservienti
Lets open the black box
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
Sounds more complex than
the na誰ve approach, doesnt it?
mauroservienti
New requirement
 Collect tickets at the venue
mauroservienti
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
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?
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
multiple workflows,
each providing
compensating
actions for every
step of the
workflow where it
can fail
mauroservienti
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
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?
Payment
Gateway
Finance
Finance
Initiate Payment
Authorize Card
Payment Policy
mauroservienti
Payment Authorized
48hrs to release money
Reservation
Checked-out
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
Whats 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
Whats 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
Whats 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
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
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
Sagas
Business Process is distributed
Respect Single Responsibility Principle
Simpler/not conflicting evolution
Independent Units of Deployment
Independent scale out
mauroservienti
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
Demos: bit.ly/eddd-state-machine
Videos: go.particular.net/eddd-state-machine
Pitfalls
 Monitor the (distributed) state
mauroservienti
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
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
there is no
such thing as
orchestration
mauroservienti
@mauroservienti | #EDDD
Mauro Servienti
Solution Architect @ Particular Software
the makers of NServiceBus
mauro.servienti@particular.net
@mauroservienti
//github.com/mauroservienti
//milestone.topics.it
mauroservienti
@mauroservienti | #EDDD
Demos: bit.ly/eddd-state-machine
Videos: go.particular.net/eddd-state-machine
mauroservienti

More Related Content

Welcome to the (state) machine @ ExploreDDD 2019

Editor's Notes

  1. Red Hot Chili Peppers, 1989 Ritmo tribale Litfiba
  2. Surface vs Highways
  3. tricked by domain experts, they talk about tickets ordering process
  4. Whats a collaborative domain? IKEA furniture and shipping at a retail shop