Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Microservice

Viney
August 26, 2020

 Microservice

Introduce microservice including the following items:
* Domain driven design
* event-driven architecture
* Saga pattern
* CQRS
* Event sourcing

Viney

August 26, 2020
Tweet

More Decks by Viney

Other Decks in Technology

Transcript

  1. Agenda • What’s Microservice and the relation with organization •

    What’s the challenge moving to Microservice • Distributed data • Event-driven messaging • Consistency • Summary • Q&A
  2. Deliver software rapidly, frequently and reliably • Velocity • Lead

    time: time from commit to deploy • Deployment frequency: deploys per developer per day • Reliability • Change failure rate: % of deployments that cause an outage • Mean time to recover from a deployment failure
  3. Needs to able to easily modernize applications • Successful applications

    often lives a very long time • Technology changes
  4. Success Triangle Deliver changes to long-lived applications rapidly, frequently and

    reliably Process: SRE, CI / CD, Automation Architecture: ??? Organization: Autonomous team
  5. • SRE (DevOps) • Autonomous team • Long-lived applications Required

    architectural quality attributes (.a.k.a ilities) • Testability • Deployability • Maintainability • Modularity • Evolvability
  6. Ilities of small monoliths • Testability ✅ • Deployability ✅

    • Maintainability ✅ • Modularity ✅ • Evolvability ✅
  7. Rapid, frequent and reliable delivery eventually becomes impossible Maintainability Testability

    Deployability Modularity Evolvability Time (Size complexity) Ilities required to be competitive Risk of disruption
  8. The microservice architecture is an architectural style that structures an

    application as a set of loosely coupled services
  9. Each Microservice is • Highly maintainable and testable • Loosely

    coupled (e.g. database) • Independently deployable • Organized around business capabilities • Owned by a small team
  10. Benefits of Microservice Deliver changes to long-lived applications rapidly, frequently

    and reliably Process: SRE, CI / CD, Automation Architecture: microservice Organization: Small, Cross-function, autonomous teams Testability Deployability Maintainability Modularity Evolvability Modularity
  11. Drawbacks of microservice: Complexity • Development: IPC, consistency, partial failure,

    distributed data • Testing: integration, end to end, … • Infra: monitoring, deploy, … • Correctly identifying service boundaries → bounded context • Refactoring a monolithic application to a microservice architecture
  12. Challenge I: How to apply it into domain model? •

    Object points to one to another. Order *Customer Address City Street OrderItem Quantity *Product Customer CreditLimit Product Price Name
  13. Challenge II: How to implement ACID transaction? BEGIN TRANSACTION …

    SELECT OrderTotal FROM Orders WHERE CustomerID = ? … SELECT CreditLimit FROM Customers WHERE CustomerID = ? … INSERT INTO Orders … … COMMIT TRANSACTION
  14. Aggregate • Cluster of objects that can be treated as

    a unit • Graph consisting of a root entity and one or more other entities and value objects.
  15. Aggregate: rule #1 • Reference other aggregate roots via identity.

    • Domain model → collection of loosely connected aggregates Order customerID Address City Street OrderItem Quantity productID Customer CreditLimit Product Price Name
  16. Aggregate: rule #2 • Process one command by one aggregate.

    • Aggregate scope = Transaction scope → Service Order customerID Address City Street OrderItem Quantity productID Customer CreditLimit Product Price Name Order Service Customer Service Product Service
  17. Aggregate granularity • If an update must be atomic, it

    must be handled by a single aggregate. • Granularity leads different consistency and scalability Customer Order Product Customer Order Product Order Customer Product Consistency Scalability
  18. Two-Phase Commit (.a.k.a 2PC) is not a viable option •

    Guarantees consistency • Not supported by many NoSQL and MQ • Impacts availability in CAP Theorem • …
  19. How do the saga participants communicate? • Synchronous (e.g. REST,

    gRPC, …) • Availability(N) = Availability(N1) x Availability(N2) x Availability(N3) x … • Recovering mechanism: retry Order Service Customer Service createOrder() POST /order reseveCredit() PUT /customer/id/credit
  20. • Collaboration using asynchronous, broker-based messaging • eventual consistency •

    At least once delivery • Ordered delivery • Availability(N) = Availability(N1) Order Service Customer Service createOrder() POST /order reseveCredit() Message Broker
  21. Benefits and drawbacks of Choreography • Benefits • Simple •

    Participants are loosely coupled • Drawbacks • Decentralized implement: difficult to understand • Cyclic dependencies • …
  22. Benefits and drawbacks of Orchestration • Benefits • Centralized coordination

    logic is easier to understand • Reduce cyclic dependencies • Simplified the business logic • Drawbacks • smart orchestrator tells dumb services what to do
  23. How atomically update database and publish an event? • Dual

    write problem Service Database Message Broker ❓ ❓
  24. Data consistency in event-driven architecture • Application events (Transactional outbox):

    • eBay • one transaction two tables, and one app polling
  25. • Transaction log tailing: • LinkedIn • polling transaction log

    • MySQL binlog • Postgres WAL • AWS DynamoDB table streams • MongoDB change streams
  26. • Event Sourcing: • Persists an object as a sequence

    of events View id: 123 author: Jane Doe impact: 10
  27. Benefits and drawbacks of Event Sourcing • Benefits • Solve

    data consistency • Preserve history → replay whole data • Support temporal queries • Simplify retroactive correction • Built-in auditing • Drawbacks • Unfamiliar programming model • Evolving the schema of long-lived events • Only supports PK-based access → CQRS
  28. Summary • Microservice enables whole business, team and architecture •

    Data is distributed which create challenges • Use event-driven architecture for eventual consistency • Use Sagas to maintain data consistency across service • Understand Event Sourcing and CQRS