Dark energy and dark matter are useful metaphors for the repulsive forces, which encourage decomposition into services, and the attractive forces, which resist decomposition. You must balance these conflicting forces when defining a microservice architecture including when designing system operations (a.k.a. requests) that span services.
In this talk, I describe the dark energy and dark matter forces. You will learn how to design system operations that span services using microservice architecture collaboration patterns: Saga, Command-side replica, API composition, and CQRS patterns. I describe how each of these patterns resolve the dark energy and dark matter forces differently.
5. @crichardson
Agenda
Dark energy, dark matter and de
fi
ning a microservice
architecture
Overview of service collaboration patterns
Designing system commands
Designing system queries
6. @crichardson
How to de
fi
ne an
ArchitectureâŠ
Application
âȘsubdomainâ«
Customer
management
âȘaggregateâ«
Customer
âȘsubdomainâ«
Order
management
âȘaggregateâ«
Order
createCustomer()
createOrder()
fi
ndOrder()
fi
ndOrderHistory()
System operations
Distill
Requirements
Event storming
Functional requirements
As a consumer
I want to place an Order
So that I can âŠ.
As a Restaurant
I want to accept an Order
So that I can âŠ.
User stories
âą SLA: Reliability/Latency
âą Scalability
âą âŠ
System quality attributes
The ârequestsâ that the
application implements
Customer Team
Order Team
About Subdomains
âą Business capability/function/etc
âą Logical view: packages and classes
âą Team-sized
âą Loosely coupled (Conways law)
1
2
7. @crichardson
Application
⊠how to de
fi
ne an
ArchitectureâŠ
Application
âȘsubdomainâ«
Customer
âȘaggregateâ«
Customer
âȘsubdomainâ«
Order
âȘaggregateâ«
Order
createCustomer()
createOrder()
fi
ndOrder()
fi
ndOrderHistory()
System operations
Component
âȘsubdomainâ«
âȘsubdomainâ«
âȘsubdomainâ«
âȘsubdomainâ«
How to group
subdomains
into
components*?
Component Component
* Component = deployable, executable, e.g. WAR
fi
le, executable JAR, âŠ
3
Deployable, Executable,
e.g. WAR
fi
le, executable
JAR, âŠ
8. @crichardson
Application
⊠how to de
fi
ne an
Architecture
createCustomer()
createOrder()
fi
ndOrder()
fi
ndOrderHistory()
System operations
Component
âȘsubdomainâ«
âȘsubdomainâ«
Component
âȘsubdomainâ«
âȘsubdomainâ«
Component
Design
collaborations*
Distributed
4
*Collaboration =
interactions via REST,
messages etc.
9. @crichardson
Grouping subdomains into
components: together or separate?
âȘsubdomainâ«
Customer
âȘaggregateâ«
Customer
âȘsubdomainâ«
Order
âȘaggregateâ«
Order
Attraction
Repulsion
Generated by
system operations
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 interactions
Prefer ACID or BASE
Minimize runtime coupling
âŠ
https://chrisrichardson.net/post/microservices/2021/11/30/dark-matter-dark-energy.html
10. @crichardson
Dark energy: repulsive forces
subdomains in different services
https://chrisrichardson.net/post/microservices/2021/11/30/dark-matter-dark-energy.html
Service
Service
«Subdomain» A
«Aggregate»
X
«Subdomain» B
«Aggregate»
Y
Simple components
Team autonomy
Fast deployment pipeline
Support multiple technology stacks
Cost eïŹective scaling
Segregate regulated software
Segregate highly available components
11. @crichardson
Dark matter: attractive forces
subdomains in same service
https://chrisrichardson.net/post/microservices/2021/11/30/dark-matter-dark-energy.html
Subdomain A
«Aggregate»
X
Subdomain B
«Aggregate»
Y
Service A Service B
Simple interactions
Prefer ACID over BASE
Minimize runtime coupling
EïŹcient communication
Minimize design time coupling
SystemOperation()
Generates
12. @crichardson
The role of the architect is to
balance con
fl
icting forces
Minimize attraction
Service
Subdomain
Subdomain
Minimize
repulsion
Service
Subdomain
Subdomain
Minimize
repulsion
Con
fl
icting goals
13. @crichardson
Balancing forces is an iterative process
Partition subdomains to
de
fi
ne services to resolve
dark energy forces
Design system operations
(service collaborations) to
resolve dark matter forces
Adjust service de
fi
nitions to enable
resolution of dark matter forces
14. @crichardson
Agenda
Dark energy, dark matter and de
fi
ning a microservice
architecture
Overview of service collaboration patterns
Designing system commands
Designing system queries
15. @crichardson
After partitioning subdomains into services:
âą Simple interactions
âą Prefer ACID over BASE
âą Minimize runtime coupling
âą Ef
fi
cient inter-service communication
âą Minimize design time coupling
createCustomer()
Customer
Service
createOrder()
Order
Service
Restaurant
Service
Customer
Service
Collaboration
â ?
Local operation Distributed operation
âȘentityâ«
Restaurant
âȘentityâ«
Customer
âȘentityâ«
Order
âȘentityâ«
Customer
ACID transaction ??? transaction
Owned by
service
16. @crichardson
Distributed system operation =
ACID transaction
Customer
Service
Restaurant
Service
createOrder()
Order Service
Distributed ACID transaction
BEGIN transaction
âŠ
COMMIT transaction
17. @crichardson
Pattern: Shared database schema
Order Service
Customer
Service
Order
table
Customer
table
createOrder()
ACID
Transaction
(Maybe 2PC)
https://microservices.io/patterns/data/shared-database.html
reserveCredit()
creditOrder()
⊠Service
⊠table
BEGIN transaction
âŠ
COMMIT transaction
18. @crichardson
Bene
fi
ts and drawbacks: shared schema
Dark energy repulsive forces
Support multiple technology stacks â DBs be able to participate in same transaction
Dark matter attractive forces
Simple interactions â No service-service interaction!?
Prefer ACID over BASE â ACID transaction
Minimize runtime coupling â DB-level coupling/interference
Ef
fi
cient inter-service communication â No service-service interaction!?
Minimize design time coupling
â Schema changes must be coordinated
â Duplicated business logic
19. @crichardson
Pattern: Private database schema
Order Service
Customer
Service
Order
table
Customer
table
API
createOrder()
2PC
Transaction
https://microservices.io/patterns/data/database-per-service.html
reserveCredit()
BEGIN transaction
âŠ
COMMIT transaction
20. @crichardson
Private database + 2PC: Bene
fi
ts and drawbacks
Dark energy repulsive forces
Support multiple technology stacks
â Participants must use 2PC-
compatible technology
Dark matter attractive forces
Simple interactions ? Depends on operation
Prefer ACID over BASE
â
Minimize runtime coupling â all participants must be available
Ef
fi
cient inter-service communication ? Depends on operation
Minimize design time coupling ? Depends on operation
21. @crichardson
Distributed system operation =
Basically Available, Soft state, Eventually
consistent transaction
Service A
Transaction scope
=
service
distributedOperation()
BEGIN transaction
âŠ
COMMIT transaction
Service B
BEGIN transaction
âŠ
COMMIT transaction
Service C
BEGIN transaction
âŠ
COMMIT transaction
22. @crichardson
ACID vs. BASE
2PC BASE
Dark energy repulsive forces
Support multiple technology stacks â â
Dark matter attractive forces
Prefer ACID over BASE â â
Minimize runtime coupling â â (Mostly)
23. @crichardson
Eventually consistent service
collaboration patterns
API Composition
Provider
Service
Service
query()
Composer
Provider
Service
Provider
Service
CQRS
Provider
Service
Service
query()
View
Provider
Service
Provider
Service
Event
Event
Event
Service
Service Service
Transaction
Compensating
transaction
Transaction
Compensating
transaction
Transaction
Compensating
transaction
command()
Saga
Service
Service
command()
Replica Source
Event
Command-side replica
Commands
Queries
24. @crichardson
Agenda
Dark energy, dark matter and de
fi
ning a microservice
architecture
Overview of service collaboration patterns
Designing system commands
Designing system queries
25. @crichardson
Letâs imagine that you are implementing
the createOrder() commandâŠ
Service Operation Potential failure
Order Service Create Order Invalid quantities
Restaurant Service
Validate and price menu
items (Order subtotal)
Invalid menu items
Customer Service Reserve credit Insuf
fi
cient credit
Kitchen Service Create Ticket Out of stock ingredient
27. @crichardson
From a 1987 paper Service
Service Service
Transaction
Compensating
transaction
Transaction
Compensating
transaction
Transaction
Compensating
transaction
command()
Saga
28. @crichardson
Saga = sequence of local transactions
Service A
Local ACID
transaction
Service B
Local ACID
transaction
Service C
Local ACID
transaction
https://microservices.io/patterns/data/saga.html
29. @crichardson
The CreateOrderSaga
Step Participant Transaction
Compensating
transaction
1 Order Service createOrder() rejectOrder()
2 Restaurant Service validateAndPrice() -
3 Order Service updateSubtotal() -
4 Customer Service reserveCredit() releaseCredit()
5 Kitchen Service createTicket()
6 Order Service approveOrder() Can fail!
Undo
Lock
Unlock
Unlock
Semantic lock countermeasure = Semantic ACID
Can fail!
Need to rollback
31. @crichardson
Choreography-based Create Order
Saga
Order Created
Credit Reserved
Create Order
Order
state
total
âŠ
create()
âŠ
Order events channel
Customer events channel
Order
Service
Customer
Service
Restaurant
Service
Restaurants events channel
Order total calculated
MenuItems Priced
Ticket Created
Kitchen
Service
Kitchen events channel
Order credit reserved
approve()
33. @crichardson
Bene
fi
ts and drawbacks of Sagas
Dark energy repulsive forces
Support multiple technology stacks
â
Dark matter attractive forces
Simple interactions ? Depends on command
Prefer ACID over BASE â
Minimize runtime coupling ? Depends on command
Ef
fi
cient inter-service communication ? Depends on command
Minimize design time coupling ? Depends on command
34. Force: Prefer ACID over BASE â
No automatic undo =>
extra work to implement
compensating
transactions
Potential complexity of
undoing changes to data
that was changed by
another transaction
Create Order Saga Create Order Saga
Order Service:
createOrder()
Customer Service:
reserveCredit()
Order Service:
createOrder()
Customer Service:
reserveCredit()
Kitchen Service:
createTicket()
Customer Service:
releaseCredit()
FAILS!
Compensating transaction:
âą Undoes change to available credit
âą Fortunately: add/subtract commute
Updates
available credit
35. Force: Prefer ACID over BASE â
Sagas are ACD - lack I =
isolation => concurrent
execution => potential data
anomalies:
Dirty reads
Lost updates
Fuzzy reads
Must implement
countermeasures = application-
level ACID, e.g. Semantic lock
Cancel Order Saga Create Order Saga
Order Service:
cancelOrder()
Customer Service:
releaseCredit()
Order Service:
createOrder()
Customer Service:
reserveCredit()
Kitchen Service:
createTicket()
Customer Service:
reserveCredit()
Dirty read:
Exceeds customer
credit limit
FAILS!
Compensating transaction
36. @crichardson
Force: Minimize runtime coupling -
depends on when response is sent
Order
Service
createOrder()
Order Created
1
Response
2
3
â
Order
Service
createOrder()
Ticket Created
1
Response
7
8
Self-Contained
Restaurant
Service
Customer
Service
Kitchen Service
Tightly coupled â
40. @crichardson
Using a command-side replica
Order Service
createOrder()
Restaurant
Service
Orders
Restaurants
Restaurant events
Replica
Validate and price line
items without invoking
Restaurant Service
Restaurant
MenuItem
MenuItem
createRestaurant()
41. @crichardson
Bene
fi
ts and drawbacks of Command-side replica
Dark energy repulsive forces
Support multiple technology stacks â e.g. even different DBs
Dark matter attractive forces
Simple interactions
â Eliminates interaction with participant
â Other cmds must publish events
Prefer ACID over BASE â Risk of stale/inconsistent data
Minimize runtime coupling
â Command does not interact with
participant
Ef
fi
cient inter-service communication
â Command is less distributed
â Cost of replication - storage + events
Minimize design time coupling
? Risk of tightly coupling view to
providers
42. @crichardson
Force: Minimize runtime coupling -
non-local but self-contained operation
Order Service
createOrder()
Orders
Restaurants
Response
Outcome of
validating and
pricing line
items
https://microservices.io/patterns/decomposition/self-contained-service.html
43. @crichardson
Agenda
Dark energy, dark matter and de
fi
ning a microservice
architecture
Overview of service collaboration patterns
Designing system commands
Designing system queries
44. @crichardson
Letâs imagine that you are
implementing the
fi
ndOrder() query
Service Operation
Order Service Status of order: line items
Kitchen Service Status of ticket: ready for pickup
Delivery Service Status of delivery: ETA
Accounting Service Status of payment: total including tax
46. @crichardson
API Composition pattern
Order Service
Order
status
Delivery
Service
Delivery
Accounting
Service
Order
status
API Gateway
GET /orders/id
GET /orders?orderId=id
GET /orders/id
GET /deliveries?orderId=id
status
eta
Kitchen
Service
Ticket
status
prepareBy
GET /tickets?orderId=id
{⊠response âŠ}
API
Composer
Providers
Composer
47. @crichardson
Bene
fi
ts and drawbacks of API Composition
Dark energy repulsive forces
Support multiple technology stacks
â
Dark matter attractive forces
Simple interactions ? Possibility of many roundtrips
Prefer ACID over BASE
â Multiple queries - potential for inconsistencies
Minimize runtime coupling
â Use resiliency patterns (eg. Circuit breaker) to
reduce impact
Ef
fi
cient inter-service communication
? Parallelization can reduce latency
Possibility of many roundtrips or large data transfers
Minimize design time coupling â Composer coupled to providers
48. @crichardson
Inef
fi
cient API composition:
fi
ndOrderHistory()
Order Service
Order
status
Delivery Service
Delivery
Accounting Service
Order
status
API Gateway
GET /orders?
consumerId=abc&
text=noodles&offset=ABC
GET /orders?
consumerId=abc&
text=noodles&offset=ABC
GET /deliveries?
consumerId=abc&
text=noodles&offset=ABC
GET /orders?
consumerId=abc&
text=noodles&offset=ABC
X X
No line items for text search
Different sort order
status
eta
Order
Line
Item
Composer
50. @crichardson
Implementing a query using CQRS
Order
Service
Delivery
Service
Order events
Delivery events
fi
ndOrderHistory()
Order events channel
Delivery events channel
Order
History
Service
Replica
View
Database
https://microservices.io/patterns/data/cqrs.html
⊠Service
⊠events channel
⊠events
51. @crichardson
Bene
fi
ts and drawbacks of CQRS
Dark matter repulsive forces
Support multiple technology stacks
â View DB different than Provider DBs
Dark matter attractive forces
Simple interactions
â Query is local
â Other cmds must publish events
Prefer ACID over BASE â Out of order events => Inconsistencies
Minimize runtime coupling â
Ef
fi
cient inter-service communication
â Query is self-contained
â Cost of replication - storage + events
Minimize design time coupling
? Risk of view being tightly coupled to
providers
52. @crichardson
Summary: a microservice architecture
must balance con
fl
icting forces
Service
Subdomain
Subdomain
Service
Subdomain
Subdomain
Minimize dark
energy repulsive
forces
Con
fl
icting
Minimize dark matter
attractive forces
systemOperation()
Minimize dark
energy repulsive
forces
generates
53. @crichardson
Summary: design system operations
using the collaboration patterns
API Composition
Provider
Service
Service
query()
Composer
Provider
Service
Provider
Service
CQRS
Provider
Service
Service
query()
View
Provider
Service
Provider
Service
Event
Event
Event
Service
Service Service
Transaction
Compensating
transaction
Transaction
Compensating
transaction
Transaction
Compensating
transaction
command()
Saga
Service
Service
command()
Replica Source
Event
Command-side replica
Distributed ACID transactions
X