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.

Death of the batch job - NServiceBus Sagas

764 views

Published on

Presentation on NServiceBus Sagas
Democode can be found on GitHub : https://github.com/dvdstelt/sagas

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Death of the batch job - NServiceBus Sagas

  1. 1. Dennis van der Stelt all your batch jobs are belong to us Dennis van der Stelt Software Architect http://dennis.bloggingabout.net/ dennis@bloggingabout.net Particular Software engineer death of the batch job @dvdstelt #nservicebus
  2. 2. Dennis van der Stelt AGENDA
  3. 3. Dennis van der Stelt NServiceBus In Particular
  4. 4. Dennis van der Stelt NServiceBus It’s not like WCF, which does RPC But closes to WCF than to BizTalk
  5. 5. Dennis van der Stelt BUS TOPOLOGY
  6. 6. Dennis van der Stelt Messaging What is it and why do I need it?
  7. 7. reduce coupling
  8. 8. Dennis van der Stelt SpatialTemporalPlatform coupling aspects
  9. 9. Dennis van der Stelt PLATFORM Also known as ‘interoperability’ http, json, xml, xsd, etc…
  10. 10. Dennis van der Stelt TEMPORAL Store Front End Shipping Service
  11. 11. Dennis van der Stelt TEMPORAL Store Front End Shipping ServiceShipping Service
  12. 12. Dennis van der Stelt TEMPORAL Store Front End Shipping ServiceOrder Queue
  13. 13. Dennis van der Stelt TEMPORAL Store Front End Shipping ServiceOrder Queue
  14. 14. Dennis van der Stelt TEMPORAL Store Front End Shipping ServiceOrder Queue
  15. 15. Dennis van der Stelt Messaging  Reduces spatial coupling  XML/JSON for platform coupling  Asynchronous for temporal coupling
  16. 16. demo Messaging using WCF
  17. 17. Dennis van der Stelt PERFORMANCE RPC versus Messaging
  18. 18. Dennis van der Stelt NServiceBus Sagas A pattern by relation database community
  19. 19. Dennis van der Stelt
  20. 20. demo NServiceBus Sagas
  21. 21. Dennis van der Stelt Sagas Recap “What have you done” – Within Temptation
  22. 22. Dennis van der Stelt HANDLING MESSAGES Behavior like normal message handlers class MySaga : IHandleMessages<MyMessage> { public void Handle(MyMessage message) { … } }
  23. 23. Dennis van der Stelt STARTING SAGAS Extends the default IHandleMessages<T> class MySaga : IAmStartedByMessages<MyMessage> { public void Handle(MyMessage message) { … } }
  24. 24. Dennis van der Stelt STORING STATE Extends the default IHandleMessages<T> class MySaga : Saga<MySagaData>, IAmStartedByMessages<MyMessage> { public void Handle(MyMessage message) { this.Saga.Data.MyStateProperty = Message.MyProperty; } }
  25. 25. Dennis van der Stelt CORRELATING MESSAGES TO SAGA INSTANCE class MySaga : Saga<MySagaData>, IAmStartedByMessages<MyMessage> { protected override void ConfigureHowToFindSaga(SagaPropertyMapper<MySagaData> mapper) { mapper.ConfigureMapping<MyMessage>(m => m.MyProperty).ToSaga(s => s.MyStateProperty); } public void Handle(MyMessage message) { this.Saga.Data.MyStateProperty = Message.MyProperty; } }
  26. 26. Dennis van der Stelt REQUESTING TIMEOUTS Reminders to the Saga itself class MySaga : Saga<MySagaData>, IAmStartedByMessages<MyMessage>, IHandleTimeouts<MyTimeout> { public void Handle(MyMessage message) { this.Saga.Data.MyStateProperty = Message.MyProperty; RequestTimeout<MyTimeout>(TimeSpan.FromSeconds(10)); } public void Timeout(MyTimeout state) { … } }
  27. 27. Dennis van der Stelt SENDING MESSAGES Reminders to the Saga itself class MySaga : Saga<MySagaData>, IAmStartedByMessages<MyMessage> { public void Handle(MyMessage message) { this.Saga.Data.MyStateProperty = Message.MyProperty; this.Bus.Send(new MyCommand()); this.Bus.Publish(new MyEvent()); } }
  28. 28. Dennis van der Stelt SAGA STATE Memento class MySagaData : IContainSagaData { public virtual Guid Id { get; set; } public virtual string Originator { get; set; } public virtual string OriginalMessageId { get; set; } [Unique] public virtual Guid MySagaId { get; set; } } ALTER TABLE [dbo].[MySagaData] ADD UNIQUE NONCLUSTERED ([MySagaId] ASC) ON [PRIMARY]
  29. 29. Dennis van der Stelt SAGA STATE Memento class MySagaData : IContainSagaData { public virtual Guid Id { get; set; } public virtual string Originator { get; set; } public virtual string OriginalMessageId { get; set; } [Unique] public virtual Guid MySagaId { get; set; } public virtual IList<Product> Products { get; set; } } public class Product { public virtual Guid ProductId { get; set; } }
  30. 30. Dennis van der Stelt SAGA STATE Memento class MySagaData : IContainSagaData { public virtual Guid Id { get; set; } public virtual string Originator { get; set; } public virtual string OriginalMessageId { get; set; } [Unique] public virtual Guid MySagaId { get; set; } public virtual IList<Product> Products { get; set; } } public class Product { public virtual Guid ProductId MyUniqueId { get; set; } }
  31. 31. Dennis van der Stelt Death to the batch job Because we don’t want to depend on operations ;-)
  32. 32. Dennis van der Stelt
  33. 33. Dennis van der Stelt scheduled tasks Your CEO had insomnia and was using the system in the middle of the night. The batch job failed somewhere in the middle of updating 74 million records… You need to figure out which row it failed on (how?), why it failed, correct the issue, then start the job again from where it left off, because if you have to start from the beginning, it won't get done before peak hours in the morning. because that sounds better than batch job
  34. 34. Dennis van der Stelt BATCH JOB All customers that ordered $5000 in the last year, get preferred status DateTime cutoff = DateTime.Today.AddDays(-365); foreach(var customer in customers) { var orderTotal = customer.Orders .Where(o => o.OrderDate > cutoff) .Sum(order => order.OrderValue); customer.Prefered = orderTotal > 5000; }
  35. 35. Dennis van der Stelt Tromsø, Norway
  36. 36. Dennis van der Stelt what if we can see things before they happen?
  37. 37. Dennis van der Stelt customer preferred status Dev: Let's say Steve orders something for $100. At what point does that amount no longer count toward Steve's preferred status? BA: That's easy, after 365 days!
  38. 38. Dennis van der Stelt -$300-$100 +$300 DURABLE TIMEOUTS Our way to predict the future 2015 2016 +$100
  39. 39. Dennis van der Stelt DURABLE TIMEOUTS public void Handle(OrderPlaced message) { if(this.Data.CustomerId == 0) this.Data.CustomerId = message.CustomerId; this.Data.RunningTotal += message.Amount; this.RequestTimeout<OrderExpired>(TimeSpan.FromDays(365), timeout => timeout.Amount = message.Amount); CheckForPreferredStatusChange(); } public void Handle(OrderExpired message) { this.Data.RunningTotal -= message.Amount; CheckForPreferredStatusChange(); }
  40. 40. Dennis van der Stelt DURABLE TIMEOUTS private void CheckForPreferredStatusChange() { if(this.Data.PreferredStatus == false && this.Data.RunningTotal >= 5000) { this.Bus.Publish<CustomerHasBecomePreferred>( evt => evt.CustomerId = this.Data.CustomerId); } else if(this.Data.PreferredStatus == true && this.Data.RunningTotal < 5000) { this.Bus.Publish<CustomerHasBecomeNonPreferred( evt => evt.CustomerId = this.Data.CustomerId); } }
  41. 41. Dennis van der Stelt Best Practices The silver bullets?
  42. 42. Dennis van der Stelt Rule #1 : Don’t query data Never ever, ever, ever query data - From the saga to another data source - Owned by saga with a 3rd party tool
  43. 43. Dennis van der Stelt Business eventsTimeoutsStarting sagas out of order
  44. 44. Dennis van der Stelt ASYNCHRONOUS COMMUNICATION Order of message delivery OrderAccepted OrderAccepted OrderBilled
  45. 45. Dennis van der Stelt ASYNCHRONOUS COMMUNICATION Order of message delivery OrderAccepted OrderAccepted OrderBilled
  46. 46. Dennis van der Stelt ASYNCHRONOUS COMMUNICATION Order of message delivery OrderAccepted OrderAccepted OrderBilled OrderCancelled How to solve?
  47. 47. race conditions do not exist
  48. 48. Dennis van der Stelt STARTING SAGAS What business events can start a Saga? class MySaga : Saga<MySagaData>, IAmStartedByMessages<MyMessage>, IAmStartedByMessages<YourMessage>, IAmStartedByMessages<AnotherMesssage> { public void Handle(MyMessage message) { … if (VerifyState()) MarkAsComplete(); } }
  49. 49. Dennis van der Stelt 404 ON SAGAS What if the Saga was already MarkedAsComplete public class SagaNotFoundHandler : IHandleSagaNotFound { IBus bus; public SagaNotFoundHandler(IBus bus) { this.bus = bus; } public void Handle(object message) { bus.Reply(new SagaDisappearedMessage()); } }
  50. 50. Dennis van der Stelt AUTO CORRELATION Avoids the need for mapping responses class MySaga : Saga<MySagaData>, IAmStartedByMessages<MyMessage>, IHandleMessages<Response> { protected override void ConfigureHowToFindSaga(SagaPropertyMapper<MySagaData> mapper) { mapper.ConfigureMapping<MyMessage>(m => m.MyProperty).ToSaga(s => s.MyStateProperty); // Mapping for Respose not needed! } } public class RequestHandler : IHandleMessages<Request> { public void Handle(Request message) { Bus.Reply(new Response()); } }
  51. 51. Dennis van der Stelt AUTO CORRELATION Avoids the need for mapping responses class MySaga : Saga<MySagaData>, IAmStartedByMessages<MyMessage>, IHandleMessages<MyTimeout> { public void Timeout(MyTimeout state) { if (state.RasiedAtUtc == … } } public class MyTimeout { public DateTime RaisedAtUtc { get; set; } = DateTime.UtcNow; } Can be a POCO No `virtual` as it’s serialized to string format
  52. 52. Dennis van der Stelt CONCURRENCY [LockMode(LockModes.Read)] class ShoppingCartSagaData : IContainSagaData { … [RowVersion] public virtual int RowVersion { get; set; } [Unique] public virtual Guid CartId { get; set; } }
  53. 53. Dennis van der Stelt CONCURRENCY DELETE FROM ShoppingCartSagaData WHERE Id = '1C669378-3508-4A20-8EFD-A5C60168893E' AND Originator is null AND OriginalMessageId = 'd6436b7-eb17-4de5-8b92-a5c60168888c' AND CartId = '49784AF0-0854-4FF9-B8CD-E57CC8EE6CFC‘ AND … AND … AND … DELETE FROM ShoppingCartSagaData WHERE Id = '1C669378-3508-4A20-8EFD-A5C60168893E' AND RowVersion = 1
  54. 54. Sagas is business agility
  55. 55. http://bit.ly/nlnsbug
  56. 56. Dennis van der Stelt Please… stay in touch! http://dennis.bloggingabout.net dennis@bloggingabout.net

×