Slide 1

Slide 1 text

Microservices Domain-driven Design Michael Plöd, @bitboss <3

Slide 2

Slide 2 text

Check out my book https://leanpub.com/ddd-by-example This book aims to explain the concepts of Domain- driven Design in a way that it is easily applicable in practice. Each chapter contains a theoretical part which is explained from the point of view of practical applicability and then exercises with solutions based on a comprehensive, complex case study (real estate loans).

Slide 3

Slide 3 text

https://leanpub.com/ddd-by-example https://leanpub.com/ddd-referenz/

Slide 4

Slide 4 text

Disclaimer Michael Plöd - innoQ
 @bitboss Most of these ideas do not come from me personally. I have to thank Eric Evans and Vaughn Vernon for all the inspiration and ideas. If you haven’t: go out and get their amazing books!

Slide 5

Slide 5 text

Feel free to replace developer by architect, team lead, team member or domain expert. Neither Domain-driven Design nor Microservices are silver bullets that solve all your problems just by applying them.

Slide 6

Slide 6 text

Domain-driven Design
 and
 Microservices a few disclaimers DDD and Microservices are not just about Bounded Contexts DDD itself is not just about Aggregates, Entities and Services

Slide 7

Slide 7 text

Domain-driven Design is not
 about technologies

Slide 8

Slide 8 text

Business
 Domain
 Experts Developers
 Architects D
 D D

Slide 9

Slide 9 text

Technical Language Business Language Business Folks Developers Domain Experts IT Folks Tracking 
 Postgres AI
 Engine Rabbit
 Messaging Results
 MySQL Parcel Sent Predict
 Delivery Recipient
 Notified

Slide 10

Slide 10 text

Technical Language Business Language Business Folks IT Folks Ubiquitous Language Domain Experts Developers

Slide 11

Slide 11 text

DOMAIN MODEL Domain Experts Developers Ubiquitous Language Conversations Code Drawings Documentation

Slide 12

Slide 12 text

Model your Microservices along business capabilities

Slide 13

Slide 13 text

Subdomains and Domain Models

Slide 14

Slide 14 text

Goal: independence of systems Not with a central domain model

Slide 15

Slide 15 text

Goal: independence of systems Design & Implementation Releasing & Deployment Runtime & Operations

Slide 16

Slide 16 text

Bounded Context Context Map Shared Kernel Customer / 
 Supplier Conformist Anticorruption
 Layer Separate W ays Open / Host 
 Service Published 
 Language STRATEGIC DESIGN helps us with regards to business capabilities

Slide 17

Slide 17 text

Bounded Context Every sophisticated business (sub-) domain consists of a bunch of Bounded Contexts Each Bounded Context contains a domain model, don’t nest Bounded Contexts The Bounded Context is also a boundary for the meaning of a given model

Slide 18

Slide 18 text

Example - You at Java Forum Stuttgart Reservations Event
 Management Badges Java Forum 
 Stuttgart
 Visitor Name Payment Details Address Company Session Registrations Lunch Preferences Name Job Description Twitter Handle

Slide 19

Slide 19 text

A Bounded Context is the 
 BOUNDARY
 for the meaning of a model

Slide 20

Slide 20 text

Don’t Repeat Yourself

Slide 21

Slide 21 text

Don’t Repeat Yourself INSIDE of a Bounded Context

Slide 22

Slide 22 text

Repeat Yourself BETWEEN several Bounded Contexts

Slide 23

Slide 23 text

Cut your
 Microservices 
 along Bounded Contexts

Slide 24

Slide 24 text

Don’t Repeat Yourself INSIDE of a Bounded Context Mic rose rvice

Slide 25

Slide 25 text

Repeat Yourself BETWEEN several Bounded Contexts Mic rose rvices You may want to

Slide 26

Slide 26 text

Mind Conway’s Law when dealing with communication between Microservices

Slide 27

Slide 27 text

Continuous Delivery is an important enabler for the agile, cultural part of DDD Business
 Domain
 Experts Developers
 Architects Operations
 DDD Dev
 Ops

Slide 28

Slide 28 text

DDD Software
 Engineering Domain
 Knowledge Agile

Slide 29

Slide 29 text

Communication between Contexts Call Flow How are calls and events flowing within a system / context landscape? Model Flow How do models propagate between systems? Where are boundaries? Influence Flow How do the actions of one team affect other teams? Who influences whom? or or Team Communication / Politics How do teams communicate with each other? Where are politics being played? or or

Slide 30

Slide 30 text

Eric Evans defined those seven 
 context mapping patterns Context Map Patterns by the books Shared Kernel Customer / Supplier Conformist Anticorruption Layer Separate Ways Open / Host Service Published Language

Slide 31

Slide 31 text

Vaughn Vernon added two more patterns to the list Context Map Patterns by the books Shared Kernel Customer / Supplier Conformist Anticorruption Layer Separate Ways Open / Host Service Published Language Partnership Big Ball Of Mud

Slide 32

Slide 32 text

Where should Context Maps help? Governance A Context Map helps to identify governance issues between applications and teams. Transformation A Context Map is an excellent starting point for future transformations: it gives an in-depth insight into integration aspects and subdomain / context relations Bad Models By introducing a Context Map we get a clear view on where and how bad models propagate through application landscapes Politics By not just looking at technical integration aspects the Context Map also helps us in seeing how teams communicate and „play politics“.

Slide 33

Slide 33 text

Upstream / Downstream Credit Agency Scoring Influence
 Flow Upstream System Downstream System Actions of the upstream team, have a
 direct impact on the downstream team.

Slide 34

Slide 34 text

Categories for 
 Context Mapping Patterns

Slide 35

Slide 35 text

Context Map Patterns Categorized Credit Agency Scoring Upstream-centric Patterns: • Open Host Service Downstream-centric Patterns: • Customer / Supplier • Conformist • Anticorruption Layer Bilateral Patterns: • Shared Kernel • Separate Ways • Published Language

Slide 36

Slide 36 text

Context Map Patterns Categorized Credit Agency Scoring Upstream-centric Patterns: • Open Host Service Downstream-centric Patterns: • Customer / Supplier • Conformist • Anticorruption Layer Bilateral Patterns: • Shared Kernel • Separate Ways • Published Language

Slide 37

Slide 37 text

Open Host Service Each Bounded Context offers a defined set of services that expose functionality for other systems. Any downstream system can then implement their own integration. This is especially useful for integration requirements with many other systems. SOAP / REST for example

Slide 38

Slide 38 text

Context Map Patterns Categorized Credit Agency Scoring Upstream-centric Patterns: • Open Host Service Downstream-centric Patterns: • Customer / Supplier • Conformist • Anticorruption Layer Bilateral Patterns: • Shared Kernel • Separate Ways • Published Language

Slide 39

Slide 39 text

Customer / Supplier Upstream System Downstream System There is a customer / supplier relation ship between two teams. The down- stream team is considered to be the customer not having a full influence but has some influence. Downstream Team discusses model with Upstream Team

Slide 40

Slide 40 text

Conformist Upstream System Downstream System The downstream team conforms to the model of the upstream team. There is no translation of models and no vetoing. If the upstream model is a mess, it propagates to the downstream model. The Downstream Team adapts to the model of the Upstream Team

Slide 41

Slide 41 text

Anticorruption Layer The anticorruption layer is a layer that isolates a client’s model from another system’s model by translation. External model to internal model transformation

Slide 42

Slide 42 text

Context Map Patterns Categorized Credit Agency Scoring Upstream-centric Patterns: • Open Host Service Downstream-centric Patterns: • Customer / Supplier • Conformist • Anticorruption Layer Bilateral Patterns: • Shared Kernel • Separate Ways • Published Language

Slide 43

Slide 43 text

Shared Kernel Two teams share a subset of the domain model including code and maybe the database. Shared Kernel

Slide 44

Slide 44 text

Separate Ways There is no connection between the bounded contexts of a system. This allows teams to find their own solutions in their domain. However there may be a „hidden“ connection between the teams. Look out of for „organizational“ Solutions (Excel, Access, ..)

Slide 45

Slide 45 text

Published Language Published Language is quite similar to Open Host Service. However it goes as far as to model a Domain as a common language between bounded contexts. Typical examples are iCal or vCard. Implements according to Published Language Implements according to Published Language

Slide 46

Slide 46 text

Example: iCal The top-level element in iCalendar is the Calendaring and Scheduling Core Object, a collection of calendar and scheduling information. Typically, this information will consist of a single iCalendar object. However, multiple iCalendar objects can be grouped together. The first line must be BEGIN:VCALENDAR, and the last line must be END:VCALENDAR; the contents between these lines is called the "icalbody".
 The second line VERSION:2.0 indicates that the data is in iCalendar format. VERSION:1.0 was used to specify that data is in the old vCalendar format. The body of the iCalendar object (the icalbody) is made up of a list of calendar properties and one or more calendar components. The calendar properties apply to the entire calendar. The calendar components are several calendar properties which create a calendar schematic (design). For example, the calendar component can specify an event, a to-do list, a journal entry, time zone information, or free/busy time information, or an alarm. Empty lines are not allowed in some versions of usage (Google calendar). Source: https://en.wikipedia.org/wiki/ICalendar

Slide 47

Slide 47 text

Example: vCard vCard, also known as VCF (Virtual Contact File), is a file format standard for electronic business cards. vCards are often attached to e-mail messages, but can be exchanged in other ways, such as Multimedia Messaging Service(MMS), on the World Wide Web, instant messaging or through QR code. They can contain name and address information, telephone numbers, e-mail addresses, URLs, logos, photographs, and audio clips. vCard is used as data interchange format in personal digital assistants (PDAs), personal information managers (PIMs) and customer relationship management (CRMs). To accomplish these data interchange applications, other "vCard variants" have been used and proposed as "variant standards", each for its specific niche: XML representation, JSON representation, or web pages. Source: https://en.wikipedia.org/wiki/VCard

Slide 48

Slide 48 text

Context Maps and Conway’s Law Team independence Tight Coupling / Integration Shared Kernel Customer / Supplier Conformist Anticorruption Layer Separate Ways Open / Host Service Published Language Team Communication

Slide 49

Slide 49 text

U D D U U D Example: Context Maps Credit
 Application Scoring Credit
 Agency Customer Customer
 Contact O
 H
 S C
 F S K SUP ACL OHS C U
 S

Slide 50

Slide 50 text

Microservice architectures should be evolvable

Slide 51

Slide 51 text

Aggregates (Internal) 
 Building Blocks Entities Value Objects Factories Services Repositories TACTICAL DESIGN helps us with regards to evolvability of microservices

Slide 52

Slide 52 text

Building
 Blocks Entities Entities represent the core business objects of a bounded context’s model Each Entity has a constant identity Each Entity has its own lifecycle Customer Credit Application Shipment

Slide 53

Slide 53 text

Building
 Blocks Value Objects Value Objects derive their identity from their values Value Objects do not have their own lifecycle, they inherit it from Entities that are referencing them You should always consider value objects for your domain model Color Monetary Amount Customer

Slide 54

Slide 54 text

Building
 Blocks Is „Customer“ an Entity or 
 a Value Object If an object can be considered an Entity or a Value Object always depends on the (Bounded Context) it resides in. Customer Example: A customer is an entity in a CRM-like microservice but not in a microservice that prints badges, there we are just interested in name, job description and Twitter handle

Slide 55

Slide 55 text

Value Objects Expressiveness Value Objects add expressive names to key concepts in a model and increase the conceptual cohesiveness of objects. Focus for Entities Value Objects allow entities to focus on identity by delegating behavioral logic to them. Domain Concepts Value Objects enable moving from using primitives for simple domain concepts like postcode of an address.

Slide 56

Slide 56 text

Entity Value Value Using only Entities and Value Objects you will end up with big object graphs

Slide 57

Slide 57 text

Issues related to big object graphs Issues Navigation Endless navigation possibilities lead to less understandable code and is harder to maintain. Mind the „Law Of Demeter“. Consistency Big object graphs usually lead to large transactional boundaries and to a „everything must be consistent right now“ attitude. Cascading of errors The bigger the object graph, the easier it is for bugs to propagate or to pop up at places no one would expect. Testing Big and growing object graphs are hard to test. Sooner or later you end up with a ton of mocks.

Slide 58

Slide 58 text

Loose Coupling + Information Hiding + Cohesion Lets talk about

Slide 59

Slide 59 text

Entity Value Value Neither loose coupling, nor information hiding or cohesion are achieved with large object graphs

Slide 60

Slide 60 text

Building
 Blocks Aggregates Do not underestimate the power of the Aggregate

Slide 61

Slide 61 text

Aggregates group Entities and Value Objects

Slide 62

Slide 62 text

ROOT ROOT ROOT ROOT Each Aggregate has a Root Entity, aka Aggregate Root

Slide 63

Slide 63 text

Aggregates Domain Concepts Aggregates represent higher level business concepts. Behavior Try moving behavior to Value Objects in the Aggregates. The Entities should deal with lifecycle and identitiy. Invariants Aggregates allow us to implement and enforce rules and invariants (a fincancial situation must have in- and outgoings)

Slide 64

Slide 64 text

Connascence „In software engineering, two components are connascent if a change in one would require the other to be modified in order to maintain the overall correctness of the system.“ https://en.wikipedia.org/wiki/Connascence

Slide 65

Slide 65 text

Mind Microservice integration styles

Slide 66

Slide 66 text

Orchestration vs Choreography Building Microservices by Sam Newman, O'Reilly Event Stream Choreography Orchestration

Slide 67

Slide 67 text

Choreography Credit
 Application Scoring Credit Decision Customer Credit
 Application
 Submitted Event

Slide 68

Slide 68 text

Credit
 Application
 Submitted Event We can use Domain Events for the communication between Microservices. This leads us to Event-driven Microservices

Slide 69

Slide 69 text

! Model information about activity in the domain as a series of discrete events.

Slide 70

Slide 70 text

An event is something 
 that happened in the past t now Event Event Event Event Event

Slide 71

Slide 71 text

How do I identify events?

Slide 72

Slide 72 text

Event Storming !

Slide 73

Slide 73 text

Invite the right people Provide unlimited 
 modelling space Explore the domain starting 
 from Domain Events Explore the the origins of Domain Events Look for Aggregates Source: http://ziobrando.blogspot.de/2013/11/introducing-event-storming.html Avoid tables / chairs
 in the room

Slide 74

Slide 74 text

Command Aggregate Domain
 Event External 
 System Read
 Model Policy Command on on returns returns updates triggers invokes invokes

Slide 75

Slide 75 text

REST + Events
 =
 Feeds

Slide 76

Slide 76 text

Application Approved Event Credit Decision The Credit Decision Microservice publishes the Application Approved Events as an Atom Feed via HTTP Contract Offers The Contract Offers Microservice polls the HTTP based Atom Feed at a predefined rate

Slide 77

Slide 77 text

In order to offer Feeds we must? Persist our Domain Events!

Slide 78

Slide 78 text

Event Sourcing is an architectural pattern in which the state of the application is being determined by a sequence of events

Slide 79

Slide 79 text

Using the ideas behind CQRS we can derive views that are optimized for queries from the event store

Slide 80

Slide 80 text

Migrating from monoliths to Microservices

Slide 81

Slide 81 text

Starting point: The monolith from the gates of hell

Slide 82

Slide 82 text

Bounded Contexts Top Down Context Maps Bottom Up Aggregates Microservice Candidates

Slide 83

Slide 83 text

!Never perform a big bang migration. Work in small steps and each step should deliver an improvement.

Slide 84

Slide 84 text

Microservices Domain-driven Design Michael Plöd, @bitboss <3

Slide 85

Slide 85 text

Check out my book https://leanpub.com/ddd-by-example This book aims to explain the concepts of Domain- driven Design in a way that it is easily applicable in practice. Each chapter contains a theoretical part which is explained from the point of view of practical applicability and then exercises with solutions based on a comprehensive, complex case study (real estate loans).