developing event driven microservices with event sourcing
play

Developing event-driven microservices with event sourcing and CQRS - PowerPoint PPT Presentation

Developing event-driven microservices with event sourcing and CQRS Chris Richardson Author of POJOs in Action Founder of the original CloudFoundry.com @crichardson chris@chrisrichardson.net http://plainoldobjects.com http://microservices.io


  1. Developing event-driven microservices with event sourcing and CQRS Chris Richardson Author of POJOs in Action Founder of the original CloudFoundry.com @crichardson chris@chrisrichardson.net http://plainoldobjects.com http://microservices.io @crichardson

  2. Presentation goal Show how Event Sourcing and Command Query Responsibility Segregation (CQRS) are a great way to implement microservices @crichardson

  3. About Chris @crichardson

  4. About Chris Founder of a startup that’s creating a platform for developing event-driven microservices: http://eventuate.io/ Consultant helping organizations improve how they architect and deploy applications using cloud, micro services, polyglot applications, NoSQL, ... Creator of http://microservices.io @crichardson

  5. For more information http://microservices.io http://github.com/cer/microservices-examples https://github.com/cer/event-sourcing-examples http://plainoldobjects.com/ https://twitter.com/crichardson @crichardson

  6. Agenda Why build event-driven microservices? Overview of event sourcing Designing microservices with event sourcing Implementing queries in an event sourced application @crichardson

  7. Traditional monolithic architecture WAR/EAR ACID Spring MVC Banking UI HTML REST/JSON Accounts Browser/ Load RDBMS Client balancer Spring Transfers Hibernate Customers Simple ... develop test Tomcat deploy scale @crichardson

  8. But large and/or complex monolithic applications = Trouble! @crichardson

  9. Apply the scale cube Y axis - functional decomposition Scale by g n splitting r i n a o l i m i different things t i t i s r a g p n a i t t t a i s l p d g s n - y i h s b t i x e a l a Z c S X axis - horizontal duplication @crichardson

  10. Today: use a microservice, polyglot architecture Standalone services Banking UI MoneyTransfer Management Account Management Service Service Account MoneyTransfer Database Database NoSQL DB Sharded SQL @crichardson

  11. But this results in distributed data management problems @crichardson

  12. Example: Money transfer Account Management MoneyTransfer Service Management Service MoneyTransfer Account Database Database No Money Transfer Account #1 ACID Account #2 No 2PC @crichardson

  13. Use an event-driven Services publish events when state changes Services subscribe to events and update their state Maintain eventual consistency across multiple aggregates (in multiple datastores) Synchronize replicated data @crichardson

  14. Eventually consistent money transfer transferMoney() MoneyTransferService AccountService MoneyTransfer MoneyTransfer MoneyTransfer Account Account Account Account fromAccountId = 101 fromAccountId = 101 fromAccountId = 101 id = 101 id = 101 id = 202 id = 202 toAccountId = 202 toAccountId = 202 toAccountId = 202 balance = 250 balance = 195 balance = 125 balance = 180 amount = 55 amount = 55 amount = 55 state = INITIAL state = DEBITED state = COMPLETED Subscribes to: Publishes: AccountDebitedEvent Subscribes to: publishes: AccountCreditedEvent AccountDebitedEvent MoneyTransferCreatedEvent MoneyTransferCreatedEvent AccountCreditedEvent DebitRecordedEvent DebitRecordedEvent Message Bus @crichardson

  15. How to atomically update state and publish an event @crichardson

  16. Update and publish using 2PC Guaranteed atomicity BUT Need a distributed transaction manager Database and message broker must support 2PC Impacts reliability Not fashionable 2PC is best avoided @crichardson

  17. Use data store as message queue Use datastore as a message queue Txn #1: Update database: new entity state & event Txn #2: Consume event Txn #3: Mark event as consumed Eventually consistent mechanism (used by eBay) See BASE: An Acid Alternative, http://bit.ly/ebaybase BUT Tangled business logic and event publishing code Difficult to implement when using a NoSQL database :-( @crichardson

  18. Agenda Why build event-driven microservices? Overview of event sourcing Designing microservices with event sourcing Implementing queries in an event sourced application @crichardson

  19. Event sourcing For each aggregate in your domain model: Identify (state-changing) domain events Define Event classes For example, Account: AccountOpenedEvent, AccountDebitedEvent, AccountCreditedEvent ShoppingCart: ItemAddedEvent, ItemRemovedEvent, OrderPlacedEvent @crichardson

  20. Persists events X NOT current state Account table 101 450 Account Event table balance 101 901 AccountOpened 500 open(initial) debit(amount) 101 902 AccountCredited 250 credit(amount) 101 903 AccountDebited 300 @crichardson

  21. Replay events to recreate state Events AccountOpenedEvent(balance) AccountDebitedEvent(amount) AccountCreditedEvent(amount) Account balance @crichardson

  22. Two actions that must be atomic Before: update state + publish events Now: persist (and publish) events Single action that can be done atomically @crichardson

  23. Request handling in an event-sourced application Microservice A pastEvents = findEvents(entityId) new() applyEvents(pastEvents) HTTP Event Account Handler Store newEvents = processCmd(SomeCmd) saveEvents(newEvents) @crichardson

  24. Event Store publishes events - consumed by other services Microservice B update() subscribe(EventTypes) Aggregate publish(event) Event Event Subscriber Store update() publish(event) NoSQL materialized view @crichardson

  25. Event store implementations Home-grown/DIY geteventstore.com by Greg Young My event store - http://bit.ly/trialeventuate @crichardson

  26. Optimizing using snapshots Most aggregates have relatively few events BUT consider a 10-year old Account ⇒ many transactions Therefore, use snapshots: Periodically save snapshot of aggregate state Typically serialize a memento of the aggregate Load latest snapshot + subsequent events @crichardson

  27. Hybrid OO/Functional style example aggregate @crichardson

  28. OO = State + Behavior Account State balance processCommand(cmd : Command) : Seq[Events] Behavior applyEvent(event : Event) : Account @crichardson

  29. Aggregate traits Used by Event Store Apply event returning to reconstitute aggregate updated Aggregate Map Command to Events @crichardson

  30. Account - command processing Prevent overdraft @crichardson

  31. Account - applying events Immutable @crichardson

  32. Event Store API Reactive/Async API @crichardson

  33. Functional example aggregate @crichardson

  34. FP = Separation of State and Behavior Account AccountAggregate processCommand(Account, Command) : Seq[Events] balance applyEvent(Account, Event) : Account State Behavior @crichardson

  35. Aggregate type classes/implicits @crichardson

  36. Functional-style MoneyTransfer Aggregate State Behavior @crichardson

  37. FP-style event store Enables inference of T, and EV Tells ES how to instantiate aggregate and apply events @crichardson

  38. Business benefits of event sourcing Built-in, reliable audit log Enables temporal queries Publishes events needed by big data/predictive analytics etc. Preserved history ⇒ More easily implement future requirements @crichardson

  39. Technical benefits of event sourcing Solves data consistency issues in a Microservice/NoSQL- based architecture: Atomically save and publish events Event subscribers update other aggregates ensuring eventual consistency Event subscribers update materialized views in SQL and NoSQL databases (more on that later) Eliminates O/R mapping problem @crichardson

  40. Drawbacks of event sourcing Weird and unfamiliar Events = a historical record of your bad design decisions Handling duplicate events can be tricky Application must handle eventually consistent data Event store only directly supports PK-based lookup (more on that later) @crichardson

  41. Agenda Why build event-driven microservices? Overview of event sourcing Designing microservices with event sourcing Implementing queries in an event sourced application @crichardson

  42. Use the familiar building blocks of DDD Entity Value object With some Services differences Repositories Aggregates @crichardson

  43. Partition a bounded context’s domain model into Aggregates @crichardson

  44. Aggregate design Graph consisting of a root entity and one or more other Order entities and value objects customerId Each core business entity = Aggregate: e.g. customer, Account, Order, Product, …. OrderLine Address Item Reference other aggregate street roots via primary key quantity city productId … Often contains partial copy productName productPrice of other aggregates’ data

  45. Aggregate granularity is important Transaction = processing one command by one aggregate No opportunity to update multiple aggregates within a transaction If an update must be atomic (i.e. no compensating transaction) then it must be handled by a single aggregate e.g. scanning boarding pass at security checkpoint or when entering jetway @crichardson

  46. Aggregate granularity Forum Forum Forum moderator moderator moderator User User User author author Post author Post Post Scalability/ Consistency User experience @crichardson

  47. Identify the state changing events for each Aggregate @crichardson

Recommend


More recommend