Monoliths
as an intermediate phase of microservices
Modular
Slide 2
Slide 2 text
2
2
About product
• Business domain: Insurance
• Product: Policy Management System
• Clients: Insurance Companies (b2b)
• Target market: USA
Slide 3
Slide 3 text
3
3
Topics of this meetup
• Benefits of microservices
• Pitfalls that may occur
• How modular monolith may help
• Fast transition from modular monolith to microservices
Slide 4
Slide 4 text
4
4
Why Microservices?
Benefits & Pitfalls
Slide 5
Slide 5 text
5
5
Microservices: Dev benefits
• Team expertise
• Horizontal scaling
• Isolation
• Experiments
Testing in production?
Slide 6
Slide 6 text
6
6
Microservices: Business benefits
• Fast releases
• Product & Organization scaling
Convey law
• Integration points
• With already existing customer’s system
• With some already existing system that we can buy
• For system integrators
10
10
Distributed transactions:
Two phase commit (2pc)
Slide 11
Slide 11 text
11
11
2pc: Drawbacks
• Performance
• Transaction coordinator can be a single point of failure (SPOF)
• Vendor lock
• Complex configuration
Slide 12
Slide 12 text
12
12
Distributed transactions: Sagas
• A saga is a sequence of local transactions
• Local transaction = changes in db + messages
• On failure - compensate each local transaction
https://microservices.io/patterns/data/saga.html
Slide 13
Slide 13 text
13
13
Sagas: Drawbacks
• Compensations for all business operations
• even for async ones
• Requires discipline
• and testing
• What if compensation fails?
Slide 14
Slide 14 text
14
14
Summary
• Microservices have great benefits
• For tech and business
• But also have pretty dangerous pitfalls
• Mostly related to incorrect decomposition
• Especially related to transactions / consistency
• And high cost
Slide 15
Slide 15 text
15
15
Just a clever thought
https://twitter.com/simonbrown/status/573072777147777024
Slide 16
Slide 16 text
16
16
Modular Monolith
What is it and how it can help?
Slide 17
Slide 17 text
17
17
Modular Monolith
• Consists of components
not layers
• Component ~ bounded context
pure business logic
• Component is a black box
Classes are internal
• Component exports only IoC module
• Component lives in some host
https://blog.cleancoder.com/uncle-bob/2011/09/30/Screaming-Architecture.html
Slide 18
Slide 18 text
18
18
Component Host
• Final Executable
Web service, console app, etc.
• Host components as a plugins
• Isolates outside world
• Infrastructure extensibility point
Maybe useful for microservices
Slide 19
Slide 19 text
19
19
Communications: In Process
• Component publishes contract
assembly with interfaces and DTOs
• Component registers
implementation of these interfaces
• Other components use interfaces
from contracts
Slide 20
Slide 20 text
20
20
In Process: Drawbacks
• Transaction Coupling
• Could be compatible only with in process hosting
• No any sign of incorrect decomposition
• Migration to microservices can be hard
Slide 21
Slide 21 text
21
21
Communications: Message bus
“A Message Bus is a combination of a common
data model, a common command set, and a
messaging infrastructure to allow different systems
to communicate through a shared set of
interfaces.”
Slide 22
Slide 22 text
22
22
Message Bus: Messages
1. Commands
1. Without response (void)
2. With response
2. Queries
3. Responses
4. Events
Slide 23
Slide 23 text
23
23
Message Bus: Interfaces
Slide 24
Slide 24 text
24
24
Message Bus: Handling abstractions
Slide 25
Slide 25 text
25
25
Communication Via Bus
• Contract consists of queries,
events, commands
• Server component handles queries
and commands, publishes events
• Client component sends queries,
commands, consumes events
Slide 26
Slide 26 text
26
26
Communication Via Bus: Notes
• 1 message = 1 transaction
• Simpler than REST
• Use Cases = Message Handlers + Sagas
• Asynchronous communications
• Messages are serializable
out of the box, by definition
• Various transport implementations
• InProc
• RabbitMq
• Azure Service Bus
• If decomposition is incorrect you will feel it
Slide 27
Slide 27 text
27
27
Web API
• Controllers convert http requests
to messages
• No logic in controllers
• Can be hosted in a single process
Slide 28
Slide 28 text
28
28
One Step To Microservices
Slide 29
Slide 29 text
29
29
Summary
• Message Handlers + Sagas = Universal application layer
Almost no changes in components
• Easy transition to microservices
• Flexible hosting model
• Infrastructure extensibility point
• Logging
• Metrics
• Tracing
• Message processing pipeline
https://www.youtube.com/watch?v=gkDqQgcbu08
Slide 30
Slide 30 text
30
30
Can modular monolith reduce risks of incorrect
decomposition?
• No
• But it reduces cost of boundary changes
• Cost of change matters
Slide 31
Slide 31 text
31
31
Thanks for your attention!
Kirill Kovalevskiy
Application Architect
kkovalevskiy@oneinc.ru