Post on 29-Jan-2018
Reliable integrationswith NServiceBus
Andreas ÖhlundEnterprise Development Expert
Agenda
• War story
• Dragons and how to manage them
• A “saga” with a happy ending
• Q & A
Fact:A trip to dragon territory cantake a while...
The usual waypublic void Handle(OrderPlaced message){
var order = new Order{ Id = message.OrderID };
order.TrackingCode = fedex.BookPickup(order); order.Status = ShippingStatus.PickupBooked;
repository.Save(order);}
The usual waypublic void Handle(OrderPlaced message){
var order = new Order{ Id = message.OrderID };
order.TrackingCode = fedex.BookPickup(order); order.Status = ShippingStatus.PickupBooked;
repository.Save(order);}
2:
3:
The usual waypublic void Handle(OrderPlaced message){
var order = new Order{ Id = message.OrderID };
order.TrackingCode = fedex.BookPickup(order); order.Status = ShippingStatus.PickupBooked;
repository.Save(order);}
This might take a while!2:
3:
Fact:Dragons doesn’t support transactions.
TX
Database rollback
DB
Order
Store
Ship
TX
Database rollback
DB
Order
Store
Ship
TX
Database rollback
DB
Order
Store
Ship
No TXSupport
TX
Database rollback
DB
Order
Store
Ship
No TXSupport
TX
Database rollback
DB
Order
Store
Ship
Did we just loose an order?
No TXSupport
TX
Database rollback
DB
Order
Store
Ship
No TXSupport
TX
Database rollback
DB
Order
Store
Ship
Order Http
Design guideline
“Interact with non transactional resources using separate endpoints”
R# Extract endpoint public void Handle(OrderPlaced message){ repository.Save(new Order { Id = message.OrderID, Status = ShippingStatus.AwaitingShipment });
bus.Send<BookShipment>(m => { m.OrderID = message.OrderID; });}
R# Extract endpoint public void Handle(OrderPlaced message){ repository.Save(new Order { Id = message.OrderID, Status = ShippingStatus.AwaitingShipment });
bus.Send<BookShipment>(m => { m.OrderID = message.OrderID; });}
R# Extract endpoint public void Handle(OrderPlaced message){ repository.Save(new Order { Id = message.OrderID, Status = ShippingStatus.AwaitingShipment });
bus.Send<BookShipment>(m => { m.OrderID = message.OrderID; });}
R# Extract endpoint
public void Handle(ShipmentBooked message){
var order = repository.Get<Order>(message.OrderID);
order.TrackingCode = message.TrackingCode;
repository.Save(order);}
TX
Consistency across rollbacks
Order
TX
Consistency across rollbacks
Order
DBStore
TX
Consistency across rollbacks
Order
DBStore
Book shipment MQ
TX
Consistency across rollbacks
Order
DBStore
Book shipment MQ
TX
Consistency across rollbacks
Order
DBStore
Book shipment MQ
TX
Consistency across rollbacks
Order
DBStore
Book shipment MQ
Not all return from a trip to the dragons
Fact:Not everyone will make it back.
TX
What happens if the response is lost?
BookShipment
Fedex.Ship
TX
What happens if the response is lost?
BookShipment
Fedex.Ship
TimeoutException
TX
What happens if the response is lost?
BookShipment
Fedex.Ship
TimeoutException
Commit
TX
What happens if the response is lost?
BookShipment
Fedex.Ship
TimeoutException
IntegrationEndpoint
IdempotencyBookShipment
IntegrationEndpoint
IdempotencyBookShipment
http://fedex.com/ship ?id=xyz123
Commit
IntegrationEndpoint
IdempotencyBookShipment
http://fedex.com/ship ?id=xyz123
Commit
IntegrationEndpoint
IdempotencyBookShipment
http://fedex.com/ship ?id=xyz123
TimeoutExceptionX
Commit
IntegrationEndpoint
IdempotencyBookShipment
http://fedex.com/ship ?id=xyz123
http://fedex.com/ship?id=xyz123
TimeoutExceptionX
Commit
IntegrationEndpoint
IdempotencyBookShipment
http://fedex.com/ship ?id=xyz123
Discard
http://fedex.com/ship?id=xyz123
TimeoutExceptionX
Commit
IntegrationEndpoint
IdempotencyBookShipment
http://fedex.com/ship ?id=xyz123
Discard
http://fedex.com/ship?id=xyz123
TimeoutExceptionX
Commit
IntegrationEndpoint
IdempotencyBookShipment
http://fedex.com/ship ?id=xyz123
Discard
http://fedex.com/ship?id=xyz123
TimeoutExceptionX
ShipmentBooked
Fact:A trip to the dragons may be costly.
Timeouts vNext
• Thread.Sleep is not a good solution
• Need a way to have durable timeouts
• The NServiceBus timeout manager solves this for us
Fact:Dragons have home turf advantage.
Scalable integrations
• Manage traffic peaks
• Allow you to control the pace
Use throttling to:
A more scalable design
Client Server
A more scalable design
Client Server
Request
A more scalable design
Client Server
Request Send
A more scalable design
Client Server
Request
Ticket - come back in Thttp://fedex.com/responses/xyz123
Send
A more scalable design
Client Server
Request
Ticket - come back in Thttp://fedex.com/responses/xyz123
Send
CacheRecv
A more scalable design
Client Server
Request
Ticket - come back in Thttp://fedex.com/responses/xyz123
} T
http://fedex.com/responses/xyz123
Send
CacheRecv
A more scalable design
Client Server
Request
Ticket - come back in Thttp://fedex.com/responses/xyz123
} T
http://fedex.com/responses/xyz123
Response / come back in T2
Send
CacheRecv
A more scalable design
Client Server
Request
Ticket - come back in Thttp://fedex.com/responses/xyz123
} T
http://fedex.com/responses/xyz123
Response / come back in T2
Send
CacheRecv
A more scalable design
Client Server
Request
Ticket - come back in Thttp://fedex.com/responses/xyz123
} T
http://fedex.com/responses/xyz123
Response / come back in T2
Send
CacheRecv
Using sagas to control message flow
BookShipment
IntegrationSaga
FedexEndpointTimeout
ManagerFedex
Using sagas to control message flow
BookShipment
IntegrationSaga
FedexEndpointTimeout
Manager
Get ticket
Fedex
Using sagas to control message flow
BookShipment
IntegrationSaga
FedexEndpointTimeout
Manager
Get ticket
Fedex
Using sagas to control message flow
BookShipment
IntegrationSaga
FedexEndpointTimeout
Manager
Get ticket
Fedex
Using sagas to control message flow
BookShipment
IntegrationSaga
FedexEndpointTimeout
Manager
Wake me up in T
Get ticket
Fedex
Using sagas to control message flow
BookShipment
IntegrationSaga
FedexEndpointTimeout
Manager
Wake me up in T
Get ticket
Fedex
Using sagas to control message flow
BookShipment
IntegrationSaga
FedexEndpointTimeout
Manager
Wake me up in T
Get ticket
Get Data
Fedex
Using sagas to control message flow
BookShipment
IntegrationSaga
FedexEndpointTimeout
Manager
Wake me up in T
Get ticket
Get Data
Fedex
Using sagas to control message flow
BookShipment
IntegrationSaga
FedexEndpointTimeout
Manager
Wake me up in T
Get ticket
Get Data
Fedex
Code...
Thanks for listening!
Thanks for listening!
Andreas ÖhlundEnterprise Development Expert