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.

Dealing with eventual consistency

As software architects we want to make our systems more performant, maintainable, understandable, or any other thing-able. We use infrastructure like Azure Service Bus or Service Fabric. Maybe we’ll introduce patterns like CQRS and Event Sourcing. Many of these choices introduce eventual consistency, but users expect immediate consistency. They don’t want to wait for eventually. They expect feedback now. There are, however, ways to work around this.

So what exactly is eventual consistency and how can we make it work? In this session, we’ll have a look at different patterns, both in the user interface and the back end, that give our users immediate feedback even though the back-end system is not. We’ll discuss how to solve the complexity of dealing with eventual consistency, without sacrificing decomposability or performance.

  • Be the first to comment

Dealing with eventual consistency

  1. 1. Dennis van der Stelt Dealing with Dennis van der Stelt distributed systems addict http://dennis.bloggingabout.net/ http://compilesoftware.nl/ Eventual Consistency @dvdstelt
  2. 2. Dennis van der Stelt2
  3. 3. Dennis van der Stelt
  4. 4. Dennis van der Stelt
  5. 5. Dennis van der Stelt https://www.pathe.nl/film/22678/game-of-thrones-finale
  6. 6. Dennis van der Stelt
  7. 7. Dennis van der Stelt
  8. 8. Dennis van der Stelt
  9. 9. Dennis van der Stelt let’s check history how did we get to those issues?
  10. 10. Dennis van der Stelt SELLING TICKETS And when we are too successful for our own good Hi, how can I help you? I’d like to order tickets to the finale of Game of Thrones. Sure, no problem. How many would you like? I have no friends, so I’ll be alone. Just one ticket, please.
  11. 11. Dennis van der Stelt WHEN BUSINESS IS BOOMING We need to scale out Hi, how can I help you? Hi, how can I help you? Game of Thrones finale, 3 tickets. You’ve got them. Game of Thrones finale, 4 tickets. You’ve got them.
  12. 12. Dennis van der Stelt and then… the internet happened
  13. 13. Dennis van der Stelt DIGITIZING COMMERCE The birth of eCommerce https://movies.com/
  14. 14. Dennis van der Stelt DEALING WITH STRONG CONSISTENCY begin transaction var quantity = select Quantity from Movies where Id = @MovieId if (quantity >= amountOfTicketsCustomerWants) update Movies set Quantity = quantity – amountOfTicketsCustomerWants where Id = @MovieId commit transaction Id Name TheaterId Price Quantity 42 Star Wars Rise of Skywalker 1 $ 15.00 400 1337 Game of Thrones 2 $ 0.00 100 Movies table How every solutionstarts as a very, very small problem
  15. 15. Dennis van der Stelt DEALING WITH STRONG CONSISTENCY Id Name TheaterId Price Quantity 42 Star Wars Rise of Skywalker 1 $ 15.00 400 1337 Game of Thrones 2 $ 0.00 100 Movies table How every solutionstarts as a very, very small problem SINGLE ITEM CONGESTION
  16. 16. Dennis van der Stelt DIGITIZING COMMERCE The birth of eCommerce
  17. 17. Dennis van der Stelt
  18. 18. Dennis van der Stelt
  19. 19. Dennis van der Stelt table index table index table index Relational Database ALTERNATIVE SOLUTIONS
  20. 20. Dennis van der Stelt table index table index table index Relational Database Document Database ALTERNATIVE SOLUTIONS
  21. 21. Dennis van der Stelt table index table index table index Relational Database Document Database Datacenters North America Europe data travels only at the speed of light ALTERNATIVE SOLUTIONS
  22. 22. Dennis van der Stelt Datacenters North America Europe data travels only at the speed of light Dennis van der Stelt ALTERNATIVE SOLUTIONS
  23. 23. Dennis van der Stelt Datacenters North America Europe data travels only at the speed of light Messaging ALTERNATIVE SOLUTIONS Searching for data that might not be there yet.
  24. 24. Dennis van der Stelt asynchronous communication
  25. 25. Dennis van der Stelt DEALING WITH EVENTUAL CONSISTENCY What is the problem? Get a subscription Enter your details Full Name: https://movies.com/subscription/edit/ Dennis van der Stelt Address: Van Zandvlietplein 1 City: Rotterdam Country: The Netherlands Get subscription Home Movies Theaters Subscription Your subscription details Full Name https://movies.com/subscription/details/ Address City Country Edit subscription Home Movies Theaters Subscription type: Premium Payment type: Monthly Subscription type Payment type : Dennis van der Stelt : Van Zandvlietplein 1 : Rotterdam : The Netherlands Premium Monthly payment
  26. 26. Dennis van der Stelt
  27. 27. Dennis van der Stelt How to deal with it? Do we really need strong consistency?
  28. 28. Dennis van der Stelt READING WRITING what you are
  29. 29. Dennis van der Stelt DEALING WITH EVENTUAL CONSISTENCY When you’re not reading your own writes… Add a new movie Name: https://movies.com/admin/movies/add Zombieland: Double tap Description: they face off against evolved zombies, Rating: Rated R Classification: Add movie Home Movies Theaters Now running https://movies.com/ Home Movies Theaters Price: $ 10 Hall Main hall Action Comedy Horror x x x
  30. 30. Dennis van der Stelt DEALING WITH EVENTUAL CONSISTENCY When you’re not reading your own writes… Register for an account Name: https://movies.com/users/registration Dennis van der Stelt Email: dennis@bloggingabout.net Password: Register Home Movies Theaters https://movies.com/users/thanks Home Movies Theaters Password again: Check the email we’ve sent you to verify your email address.
  31. 31. Dennis van der Stelt DEALING WITH EVENTUAL CONSISTENCY When you are reading your own writes Get a subscription Enter your details Full Name: https://movies.com/subscription/edit/ Dennis van der Stelt Address: Van Zandvlietplein 1 City: Rotterdam Country: The Netherlands Get subscription Home Movies Theaters Subscription Your subscription details Full Name https://movies.com/subscription/details/ Address City Country Edit subscription Home Movies Theaters Subscription type: Premium Payment type: Monthly Subscription type Payment type : Dennis van der Stelt : Van Zandvlietplein 1 : Rotterdam : The Netherlands Premium Monthly payment
  32. 32. Dennis van der Stelt DEALING WITH EVENTUAL CONSISTENCY Reading your own writes – the simplest solution Get a subscription Enter your details Full Name: https://movies.com/subscription/edit/ Dennis van der Stelt Address: Van Zandvlietplein 1 City: Rotterdam Country: The Netherlands Get subscription Home Movies Theaters Subscription type: Premium Payment type: Monthly https://movies.com/subscription/thanks Home Movies Theaters Thanks for submitting your details take me back to movies We are reviewing your changes. This can take a few minutes.
  33. 33. Dennis van der Stelt Get a subscription Enter your details Full Name: https://movies.com/subscription/edit/ Address: City: Country: Get subscription Home Movies Theaters Subscription type: <make a selection> Payment type: <make a selection> DEALING WITH EVENTUAL CONSISTENCY Reading your own writes – anothersimple solution Get a subscription Enter your details Full Name: https://movies.com/subscription/edit/ Dennis van der Stelt Address: Van Zandvlietplein 1 City: Rotterdam Country: The Netherlands Get subscription Home Movies Theaters Subscription type: Premium Payment type: Monthly Thanks for submitting your details
  34. 34. Dennis van der Stelt DEALING WITH EVENTUAL CONSISTENCY Reading your own writes – providing the best user experience, but for who? <div class="visible"> <textarea name="comment" /> <button type="submit"> Add comment </button> </div> <div class="invisible"> <span class="comment-copy"></div> <a href="/users/1337">Name</a> </div>
  35. 35. Dennis van der Stelt DEALING WITH EVENTUAL CONSISTENCY Reading your own writes – providing the best user experience, but for who? var comments = repository.GetComments(); foreach (var comment in comments) { // display comments } var comments = localStorage.GetItem("com foreach (var comment in comments) { if (!commentWasDisplayedAlready) // display localStorage comment }
  36. 36. Dennis van der Stelt Demo Using LocalStorage to mimic fast response times.
  37. 37. Dennis van der Stelt $('#submitReviewButton').click(function(e) { var comment = $('#comment').val(); var newReview = { Identifier: guid, ReviewedAt: new Date(), Description: comment }; localStorage.setItem('popcornStoredReviews', JSON.stringify(newReview)); $('#comment').val(''); $('#reviews').append('<li>').append( $('<div>').attr('class', 'comment').text(comment) ); });
  38. 38. Dennis van der Stelt DEALING WITH EVENTUAL CONSISTENCY Reading your own writes – providing the best user experience, but for who? browser SessionStorage LocalStorage IndexedDB (web)server Cache Redis Database BENEFITS onserver
  39. 39. Dennis van der Stelt DEALING WITH EVENTUAL CONSISTENCY Reading your own writes – providing the best user experience, but for who? (web)server browser SessionStorage LocalStorage IndexedDB database Cache Redis Database
  40. 40. Dennis van der Stelt DEALING WITH EVENTUAL CONSISTENCY Another simple solution – waiting for something happening in the background https://movies.com/subscription/details/ Home Movies Theaters
  41. 41. Dennis van der Stelt DEALING WITH EVENTUAL CONSISTENCY Another simple solution – waiting for something happening in the background
  42. 42. Dennis van der Stelt ASYNCHRONOUS EXECUTION Keeps a callback thread open on the client and frequently polls the server Get a subscription Enter your details Full Name: https://movies.com/subscription/edit/ Dennis van der Stelt Address: Van Zandvlietplein 1 City: Rotterdam Country: The Netherlands Get subscription Home Movies Theaters Subscriptiontype: VIP Payment type: Monthly https://movies.com/subscription/thanks/ Home Movies Theaters
  43. 43. Dennis van der Stelt ASYNCHRONOUS COMMUNICATION Using asynchronous messaging and SignalR Get a subscription Enter your details Full Name: https://movies.com/subscription/edit/ Dennis van der Stelt Address: Van Zandvlietplein 1 City: Rotterdam Country: The Netherlands Get subscription Home Movies Theaters Subscriptiontype: VIP Payment type: Monthly Thanks for applying for a VIP subscription. You can already order movie tickets for free.* After we’ve received the first payment, your snacks in our theater will also be free. The payment will be fully automatic. All you need to do is sit back and enjoy your favorite movies! SignalR SubmitOrder OrderSubmission
  44. 44. Dennis van der Stelt Demo Ordering movie tickets using NServiceBus & SignalR
  45. 45. Dennis van der Stelt $('#submit_button').click(function (e) { var connection = new signalR.HubConnectionBuilder().withUrl('/ticketHub').build(); connection.on('OrderSubmission', function (message) { $('#ackonwledgement').html(message); }); var order = $('#ticketOrderingForm').serialize(); connection.invoke("SubmitOrder", order); }); ASYNCHRONOUSLY SUBMITTING A FORM Set up a SignalR connection and submit the form to the backend
  46. 46. Dennis van der Stelt public class TicketHub : Hub { IMessageSession messageSession; public TicketHub(IMessageSession messageSession) { this.messageSession = messageSession; } public Task SubmitOrder(var order) { return messageSession.Send(new SubmitOrder(order)); } } ASYNCHRONOUSLY SUBMITTING A FORM Accept SignalR message and use NServiceBus to send message to queue
  47. 47. Dennis van der Stelt public class SubmitOrderHandler : IHandleMessages<SubmitOrder> { public async Task Handle(SubmitOrder message, IMessageHandlerContext context) { var result = await RegisterOrder(message); return await context.Reply(new OrderSubmission(result)); } } ASYNCHRONOUSLY SUBMITTING A FORM NServiceBus handler processing message and replying with answer.
  48. 48. Dennis van der Stelt public class OrderSubmissionHandler : IHandleMessages<OrderSubmission> { IHubContext<TicketHub> ticketHubContext; public OrderSubmissionHandler(IHubContext<TicketHub> ticketHubContext) { this.ticketHubContext = ticketHubContext; } public Task Handle(OrderSubmission message, IMessageHandlerContext context) { return ticketHubContext.Clients.User(user).SendAsync("OrderSubmission", screenMessage); } } ASYNCHRONOUSLY SUBMITTING A FORM NServiceBus handler in website using SignalR to reply to browser
  49. 49. Dennis van der Stelt LET’S DISCUSS THE DESIGN So is this the way to approach this, Dennis?
  50. 50. Dennis van der Stelt DEALING WITH EVENTUAL CONSISTENCY Reading from multiple datastores Add a new movie Name: https://movies.com/admin/movies/add Zombieland: Double tap Description: they face off against evolved zombies, Rating: Rated R Classification: Add movie Home Movies Theaters Now running https://movies.com/ Home Movies Theaters Price: $ 10 Hall Main hall Action Comedy Horror x x x Movie prices Movie description remember this slide?
  51. 51. Dennis van der Stelt MULTI-PAGE CHECKOUT PROCESS How to trickthe user into thinking everything is immediately processed Shopping Cart https://www.amazon.com/peaa-martin/ Patterns of Enterprise Application Architecture by Martin Fowler Proceed to checkout Subtotal: $42.00 QuantityItem Price $42.00 1 This list contains a gift. or Sign in for 1 click ordering. 1 start checkout
  52. 52. Dennis van der Stelt Where should we deliver your order? Enter a shipping address Full Name: https://www.amazon.com/buy/shippingaddress/ Dennis van der Stelt AddressLine 1: Van Zandvlietplein 1 City: Rotterdam Country: The Netherlands Ship to this Address MULTI-PAGE CHECKOUT PROCESS How to trickthe user into thinking everything is immediately processed 2 shipping address
  53. 53. Dennis van der Stelt Select a payment option Your credit and debit cards https://www.amazon.com/buy/payment/ Amazon accepts all major credit and debit cards > Add a card Continue MasterCard ending in 2464 Dennis van der Stelt 09/2026 More payment options Please tell us the currency of your card My card is in US Dollar My card is in a different currency: select a currency MULTI-PAGE CHECKOUT PROCESS How to trickthe user into thinking everything is immediately processed 4 payment method
  54. 54. Dennis van der Stelt Review your order https://www.amazon.com/buy/payment/ Shipping address Dennis van der Stelt Van Zandvlietplein 1 Rotterdam, The Netherlands Payment method ending in 2464 Billing Address Same as shipping address Estimated delivery: Aug 17, 2018 – Aug 21, 2018 Patterns of Enterprise Application Architecture by Martin Fowler $42.00 - Quantity: 1 change change change Place your order Order Summary Items: Shipping: Total before tax: Estimated tax: Order Total $42.00 $7.98 $49.98 $0.00 $49.98 MULTI-PAGE CHECKOUT PROCESS How to trickthe user into thinking everything is immediately processed 5 summary
  55. 55. Dennis van der Stelt MULTI-PAGE CHECKOUT PROCESS How to trickthe user into thinking everything is immediately processed
  56. 56. Dennis van der Stelt What about the business? Dealing with eventual consistency in the business
  57. 57. Dennis van der Stelt Id Name Price Quantity 42 i can has cheezburger? $ 15.00 10 1337 The Winds of Winter $ 25.00 1000 HOW THE BUSINESS DEALS WITH EVENTUAL CONSISTENCY Inventory table Eventual consistency is baked into the business ProductId Delta Timestamp 1337 -5 09:03:22 17-1-2016 1337 -3 09:03:24 17-1-2016 1337 -4 09:03:25 17-1-2016 1337 -1 09:03:27 17-1-2016 1337 +250 09:03:28 17-1-2016 1337 -4 09:03:30 17-1-2016 Inventory table EventSourcing done simple No locking, append only Inventory is eventually consistent No transactional lock to discover the current inventory left
  58. 58. Dennis van der Stelt STEAM CONTROLLER
  59. 59. Dennis van der Stelt HOW THE BUSINESS DEALS WITH EVENTUAL CONSISTENCY Fake consistency in the user interface Patterns of Enterprise Application Architecture 42 customer reviews Add to cart $42.00 save $13.37 Kindle Hardcover This item ships to Rotterdam, The Netherlands Others with more on the way https://www.amazon.com/peaa-martin/ how important is it that all numbers are 100% accurate?
  60. 60. Dennis van der Stelt HOW THE BUSINESS DEALS WITH EVENTUAL CONSISTENCY When you’re faking it, please do it well…
  61. 61. Dennis van der Stelt HOW THE BUSINESS DEALS WITH EVENTUAL CONSISTENCY When you’re faking it, please do it well…
  62. 62. Dennis van der Stelt HOW THE BUSINESS DEALS WITH EVENTUAL CONSISTENCY When you’re faking it, please do it well…
  63. 63. Dennis van der Stelt
  64. 64. Dennis van der Stelt HOW THE BUSINESS DEALS WITH EVENTUAL CONSISTENCY A hybrid solution, optimized for customer satisfaction and consistency
  65. 65. Dennis van der Stelt HOW THE BUSINESS DEALS WITH EVENTUAL CONSISTENCY Putting good user experience before occasional money loss Tom Cruise, 57 DETAILS https://happypancake.com/profile/tom_cruise/ Location BeverlyHills,California,USA Searching for Women,45-55 Favorite activity Base-jumpingfrombuildings Tom Cruise, 37 DETAILS https://happypancake.com/profile/tom_cruise/ Location BeverlyHills,California,USA Searching for Women,25-35 Favorite activity Base-jumpingfrombuildings strong consistency VS user experience
  66. 66. Dennis van der Stelt
  67. 67. Dennis van der Stelt https://www.pathe.nl/film/22678/game-of-thrones-finale
  68. 68. Dennis van der Stelt WHAT PROBLEM ARE WE SOLVING? Strive for high customer satisfaction
  69. 69. Dennis van der Stelt Dennis van der Stelt dennis@bloggingabout.net dennis.bloggingabout.net @dvdstelt linkedin.com/in/dvdstelt/ http://bit.ly/eventualconsistency Solution Architect at Particular Software, makers of NServiceBus

×