Slide 1

Slide 1 text

@crichardson DDD, necessary but insu ff i cient: physical design principles for microservices Chris Richardson Microservice architecture consultant and trainer Founder of the original CloudFoundry.com Author of POJOs in Action and Microservices Patterns Founder of Eventuate.io @crichardson [email protected] adopt.microservices.io Copyright © 2023. Chris Richardson Consulting, Inc. All rights reserved Loose coupling and high cohesion X v

Slide 2

Slide 2 text

@crichardson Essence of using the microservice architecture != Setting up Kubernetes = De fi ning services

Slide 3

Slide 3 text

@crichardson Use DDD: bounded contexts => services? Adrian Cockcroft: https://www.slideshare.net/adriancockcroft/dockercon-state-of-the-art-in- microservices

Slide 4

Slide 4 text

@crichardson Design services by applying traditional (logical) software design principles = Good naming Loose coupling High cohesion ?

Slide 5

Slide 5 text

@crichardson Presentation goal Logical design principles are insuf fi cient when designing microservices Physical design principles are also required

Slide 6

Slide 6 text

@crichardson About Chris http://adopt.microservices.io Late 80s 2006 2008 2009 2012-

Slide 7

Slide 7 text

@crichardson About Chris https://chrisrichardson.net/

Slide 8

Slide 8 text

@crichardson Agenda Logical design: coupling and cohesion Overview of the microservice architecture Dark energy forces: encouraging decomposition Dark matter forces: resisting decomposition

Slide 9

Slide 9 text

@crichardson Subdomain Package Class Method Field Software design: naming and organizing software elements Application Subdomain Package Class Method Field Organize software elements into a hierarchy so that change is easy

Slide 10

Slide 10 text

@crichardson Easy to change Loose Coupling High Cohesion 1972 1978 Design for

Slide 11

Slide 11 text

@crichardson About (design-time) coupling Element A Element B Design-time coupling = Likelihood of needing to change for the same reason Tight coupling = frequently changing together

Slide 12

Slide 12 text

@crichardson The problem of tight coupling Increases the scope of the change More to understand More to change Increases coupling between teams Conways law More coordination and communication between the teams Therefore Minimizing coupling is essential

Slide 13

Slide 13 text

Examples of design techniques for loose coupling Implementation API Smaller Larger • Encapsulation • Information hiding • Dependency inversion principle • Interface Segregation principle • … Software element = iceberg

Slide 14

Slide 14 text

@crichardson Cohesion: localizing unavoidable coupling Package X Class A Class B Class C coupled elements (e.g. classes) should have the same parent (e.g. package) Package Y Loosely coupled Cohesive element = tightly coupled sub-elements Class D • Common closure principle • Separation of concerns • … elsewhere

Slide 15

Slide 15 text

@crichardson Bene fi ts of high cohesion Tightly coupled elements are together with same parent Loosely coupled elements are apart Elements unrelated to the change are elsewhere Less to understand when making a change Therefore Designing for high cohesion is essential

Slide 16

Slide 16 text

@crichardson Attractive vs repulsive forces for grouping elements Element A Element B Coupling = Attraction Cohesion = Repulsion

Slide 17

Slide 17 text

@crichardson https://www.domainlanguage.com/wp-content/uploads/2016/05/DDD_Reference_2015-03.pdf

Slide 18

Slide 18 text

@crichardson Microservices are just another element in the hierarchy, right? Hint: not exactly Subdomain Package Class Application Subdomain Package Class Subdomain Service Method Field Method Field

Slide 19

Slide 19 text

@crichardson Agenda Logical design: coupling and cohesion Overview of the microservice architecture Dark energy forces: encouraging decomposition Dark matter forces: resisting decomposition

Slide 20

Slide 20 text

@crichardson Process: DevOps/Continuous Delivery & Deployment Organization: Network of small, loosely coupled, product teams Architecture Testable Deployable Loosely coupled Developing modern software IT must deliver software rapidly, frequently, reliably and sustainably. Supports Supports Measured by DORA metrics: • Deployment frequency • Lead time • Change failure rate • Time to restore service The monolithic architecture is sometimes an obstacle

Slide 21

Slide 21 text

Pattern: microservice architecture A service is: Loosely coupled Independently deployable Implements a business capability Owned by a small team

Slide 22

Slide 22 text

@crichardson API What’s a service…? Operations Event Publisher Commands Queries Synchronous REST/gRPC Asynchronous Messaging Events Event Subscriber API Client Invokes Operations Events Service Database Responsibilities Service Name

Slide 23

Slide 23 text

@crichardson Runtime environment Infrastructure Git repository Application code Infrastructure code … what’s a service?… Application code Infrastructure code Deployment pipeline Deployable /executable Service instance Service instance Infrastructure

Slide 24

Slide 24 text

@crichardson … what is a service? Service ≪subdomain≫ … ≪subdomain≫ Order Management ≪subdomain≫ Customer Management Customer Team Order Team Ideally: One subdomain/team BUT Not always

Slide 25

Slide 25 text

@crichardson Designing services using coupling and cohesion = not bad …. Service X Subdomain A Subdomain B Subdomain C Tightly coupled subdomains should be in the same service Service Y Loosely coupled Cohesive services = tightly coupled subdomains Subdomain D elsewhere

Slide 26

Slide 26 text

@crichardson … but insuf fi cient ≪subdomain≫ Customer Mgmt. ≪aggregate≫ Customer ≪subdomain≫ Order Mgmt. ≪aggregate≫ Order Attraction Repulsion Simple components Team-sized services Fast deployment pipeline … Dark energy: an anti- gravity that’s accelerating the expansion of the universe Dark matter: an invisible matter that has a gravitational effect on stars and galaxies. https://www.nasa.gov/feature/goddard/2020/new-hubble-data-explains-missing-dark-matter Simple, ef fi cient interactions Prefer ACID over BASE Minimize runtime coupling … https://microservices.io/post/architecture/2023/03/26/dark-energy-dark-matter-force-descriptions.html Generate systemOperation()

Slide 27

Slide 27 text

@crichardson Dark energy and dark matter forces Subdomain A «Aggregate» X Subdomain B «Aggregate» Y Service A Service B Attraction Simple interactions Efficient interactions Prefer ACID over BASE Minimize runtime coupling Minimize design time coupling Simple components Team autonomy Fast deployment pipeline Support multiple technology stacks Segregate by characteristics Repulsion Dark energy Dark matter Metaphor for Metaphor for … SystemOperation() Generates Forces that act on subdomains and determine grouping into services Two types of coupling! Mostly “physical concerns”

Slide 28

Slide 28 text

@crichardson Designing an architecture = making trade-offs Con fl icting forces Therefore Some services/ operations will have an optimal design Others will be suboptimal but good enough Subdomain A «Aggregate» X Subdomain B «Aggregate» Y Service A Service B Attraction Simple, efficient interactions Prefer ACID over BASE Minimize runtime coupling Minimize design time coupling Simple components Team autonomy Fast deployment pipeline Support multiple technology stacks Segregate by characteristics Repulsion Dark energy Dark matter Metaphor for Metaphor for Fine-grained services Monolith

Slide 29

Slide 29 text

@crichardson Agenda Logical design: coupling and cohesion Overview of the microservice architecture Dark energy forces: encouraging decomposition Dark matter forces: resisting decomposition

Slide 30

Slide 30 text

@crichardson Dark energy repulsive forces 㱺 subdomains in different services https://chrisrichardson.net/post/microservices/2021/04/15/mucon-2021-dark-energy-dark-matter.html Service Service «Subdomain» A «Aggregate» X «Subdomain» B «Aggregate» Y Simple components Team autonomy Fast deployment pipeline Support multiple technology stacks Segregate by characteristics Repulsive dark energy forces Characteristics: • Resource requirements • Regulations, e.g. SaMD/PCI • Business criticality/tier • Security, e.g. PII, … • DDD core/supporting/generic • ... Designing cohesive services might help but is not guaranteed to resolve these forces

Slide 31

Slide 31 text

@crichardson Simpler components/services Service Service Service Subdomain A Subdomain A Subdomain B Subdomain B More complex service: Dependencies, and runtime behavior Simpler services: easier to understand, develop, test, … versus

Slide 32

Slide 32 text

@crichardson Team autonomy = service per team Service Service Service Subdomain A Subdomain A Subdomain B Subdomain B Coordination required Build, test and deploy independently vs. Team A Team B Team A Team B

Slide 33

Slide 33 text

@crichardson Subdomains owned by different teams must be loosely design-time coupled (Conway’s law)

Slide 34

Slide 34 text

@crichardson Fast deployment pipeline: short lead time @mipsytipsy https://speakerdeck.com/charity/cd?slide=17 Service Subdomain Subdomain Service Subdomain Shorter lead time Simpler build Longer lead time More complex build* * Accelerate build/test: • Merge queue • Incremental testing through DIP and ISP • Parallelization/clustered builds • Selective test execution

Slide 35

Slide 35 text

@crichardson Support multiple technology stacks: per-subdomain Service Python Service Java Service JVM Subdomain A Subdomain A Subdomain B Subdomain B Single technology stack Separate technology stacks Right tool for the job versus

Slide 36

Slide 36 text

@crichardson Support multiple technology stacks: incremental upgrades Need to periodically upgrade each component in order to keep its technology stack current A component must be upgraded completely - not partially Task size ∝ complexity(component) The business often resists large tasks that don’t implement features Therefore Separate subdomains to reduce service upgrade effort

Slide 37

Slide 37 text

@crichardson Separate subdomains by characteristics Subdomain characteristic Issue Resource requirements Cost-effective, scalability Regulations, e.g. SaMD/ PCI DevOps vs. Slower regulated process Business criticality/tier Maximize availability Security, e.g. PII, … Improve security DDD core/supporting/ generic Focus on being competitive

Slide 38

Slide 38 text

@crichardson Cost effective scaling Service Service Service Subdomain A Subdomain A Subdomain B Subdomain B versus CPU MEM GPU Scale together • Wasteful • Costly CPU MEM GPU Scale separately • Ef fi cient • Cheaper Load Load Load Load EC2: p4d.24xlarge EC2: p4d.24xlarge EC2: m5.24xlarge 8x cost!

Slide 39

Slide 39 text

@crichardson Example: Segregate by business criticality Service Service Service Payment Processing Payment Processing Merchant management Merchant management Shared infrastructure Risk of interference Separate infrastructure Isolated vs. chargeCard() 2.9% + 30c/ request Revenue loss and penalties chargeCard() Critical Important

Slide 40

Slide 40 text

@crichardson Segregate regulated software Service Service Service SaMD Subdomain SaMD Subdomain Other Subdomain Other Subdomain ISO 13485 ISO/IEC 62304 ISO 13485 ISO/IEC 62304 DevOps Slower Faster

Slide 41

Slide 41 text

@crichardson Agenda Logical design: coupling and cohesion Overview of the microservice architecture Dark energy forces: encouraging decomposition Dark matter forces: resisting decomposition

Slide 42

Slide 42 text

@crichardson Dark matter attractive forces 㱺 subdomains in same service https://microservices.io/post/architecture/2023/03/26/dark-energy-dark-matter-force-descriptions.html Subdomain A «Aggregate» X Subdomain B «Aggregate» Y Service A Service B Simple interactions Efficient interactions Prefer ACID over BASE Minimize runtime coupling Minimize design time coupling Attractive dark matter forces SystemOperation() Generates ✅ Less cohesive services

Slide 43

Slide 43 text

@crichardson Customer Service Customer Subdomain Order Service Minimize design time coupling: avoid expensive lockstep changes Order Subdomain For zero-downtime deployments: 1. Change biz logic + Add new version of API 2. Migrate clients to new API 3. Remove old API Multiple commits

Slide 44

Slide 44 text

@crichardson Code repository Order Service Customer Subdomain Eliminate tight design time coupling between services Order Subdomain Customer Service Customer Subdomain Order Service Order Subdomain OR OK: Same repository Best: Package together But deployments aren’t atomic: API evolution issues

Slide 45

Slide 45 text

@crichardson Simple interactions Create Order() Service Subdomain A Subdomain B Service B Service A Subdomain A Subdomain B Create Order() Complex distributed operation Simple local operation: easier to understand, troubleshoot, … vs.

Slide 46

Slide 46 text

@crichardson Ef fi cient interactions Create Order() Service Subdomain A Subdomain B Service B Service A Subdomain A Subdomain B Create Order() Network latency, limited bandwidth In-memory, fast! vs. Must satisfy SLOs

Slide 47

Slide 47 text

@crichardson Prefer ACID over BASE System Operation() Service Subdomain A Subdomain B Service B Service A Subdomain A Subdomain B System Operation() Distributed, eventually consistent transaction Simple, Local ACID transaction vs. ACID txn ACID txn ACID txn

Slide 48

Slide 48 text

@crichardson Challenges of eventual consistency Operation = sequence of local ACID transactions About commands Typically implemented using the Saga pattern Sagas are ACD - lack Isolation due interleaved execution Application must use countermeasures to implement Isolation About queries Implemented using API Composition or CQRS Query can return inconsistent results

Slide 49

Slide 49 text

@crichardson Minimize runtime coupling Runtime coupling between service A and B is the degree to which the availability of service A is affected by the availability of service B Order Service Customer Service POST /orders 1 PUT /customers/id Response 4 2 3 Tight runtime-coupling: lower availability ID Outcome

Slide 50

Slide 50 text

@crichardson Minimize runtime coupling System Operation() Service Subdomain A Subdomain B Service B Service A Subdomain A Subdomain B System Operation() Risk of runtime coupling, e.g. not meeting SLOs No runtime coupling: higher availability, lower latency vs. Must satisfy SLOs

Slide 51

Slide 51 text

@crichardson Summary Loose coupling and high cohesion are important microservice design goals Using DDD to de fi ne subdomains/bounded contexts is valuable BUT You must also consider the dark energy and dark matter forces Dark energy forces Encourage decomposition Might increase design-time coupling Dark matter forces: Resist decomposition Includes minimize design-time coupling Might reduce service cohesion https://www.nasa.gov/feature/goddard/2019/nasa-s-james-webb-space-telescope-has-been-assembled-for-the- fi rst-time Subdomain A «Aggregate» X Subdomain B «Aggregate» Y Service A Service B Attraction Simple interactions Efficient interactions Prefer ACID over BASE Minimize runtime coupling Minimize design time coupling Simple components Team autonomy Fast deployment pipeline Support multiple technology stacks Segregate by characteristics Repulsion Dark energy Dark matter Metaphor for Metaphor for … SystemOperation() Generates Forces that act on subdomains and determine grouping into services

Slide 52

Slide 52 text

@crichardson @crichardson [email protected] adopt.microservices.io Questions?