Guillermo Aguirre - April 2023
Applying Microservices
Patterns to a Modular Monolith

Guillermo Aguirre - RailsConf 2023
Welcome!

Guillermo Aguirre - RailsConf 2023
About me
@guillermoap
@guillermoap_
@guillermo.aguirre
Guillermo Aguirre
/gee-SHER-moh ah-GHEE-rreh/

Guillermo Aguirre - RailsConf 2023
Where I work

Guillermo Aguirre - RailsConf 2023
Agenda

Guillermo Aguirre - RailsConf 2023
Agenda
My journey

Guillermo Aguirre - RailsConf 2023
Agenda
My journey
Our Domain

Guillermo Aguirre - RailsConf 2023
Agenda
My journey
Our Domain
Pattern explanations

Guillermo Aguirre - RailsConf 2023
Agenda
My journey
Our Domain
Pattern explanations
Example App

Guillermo Aguirre - RailsConf 2023
Agenda
My journey
Our Domain
Pattern explanations
Example App
Demo

Applying Microservices Patterns to a Modular Monolith
My journey

Applying Microservices Patterns to a Modular Monolith
My journey
MVPs

Applying Microservices Patterns to a Modular Monolith
My journey
MVPs
Big monoliths

Applying Microservices Patterns to a Modular Monolith
My journey
MVPs
Big monoliths
Microservices

Applying Microservices Patterns to a Modular Monolith
Monoliths
The good

Applying Microservices Patterns to a Modular Monolith
Monoliths
Widely used
The good

Applying Microservices Patterns to a Modular Monolith
Monoliths
Nothing wrong with them
Widely used
The good

Applying Microservices Patterns to a Modular Monolith
Monoliths
My experience

Applying Microservices Patterns to a Modular Monolith
1 | class User < ApplicationRecord
... | ...
2473 | end
Monoliths
My experience

Applying Microservices Patterns to a Modular Monolith
Monoliths
The bad

Applying Microservices Patterns to a Modular Monolith
Long deployment times
Monoliths
The bad

Applying Microservices Patterns to a Modular Monolith
Long deployment times
Monoliths
The bad
Tightly coupled code

Applying Microservices Patterns to a Modular Monolith
Long deployment times
Monoliths
The bad
Tightly coupled code => Harder to maintain & extend

Applying Microservices Patterns to a Modular Monolith
Microservices
The good

Applying Microservices Patterns to a Modular Monolith
Smaller cohesive teams
Microservices
The good

Applying Microservices Patterns to a Modular Monolith
Smaller cohesive teams
Distributed
Microservices
The good

Applying Microservices Patterns to a Modular Monolith
Microservices
My experience

Applying Microservices Patterns to a Modular Monolith
Data inconsistencies
Microservices
My experience

Applying Microservices Patterns to a Modular Monolith
Data inconsistencies
Hot-
f
ix issues in production
Microservices
My experience

Applying Microservices Patterns to a Modular Monolith
Microservices
The bad

Applying Microservices Patterns to a Modular Monolith
Signi
f
icantly higher infrastructure complexity
Microservices
The bad

Applying Microservices Patterns to a Modular Monolith
Signi
f
icantly higher infrastructure complexity
Distributed mess
Microservices
The bad

Applying Microservices Patterns to a Modular Monolith
Modularization

Applying Microservices Patterns to a Modular Monolith
Modularization

Applying Microservices Patterns to a Modular Monolith
Monolith vs Microservices by Simon Brown
Modularization

Applying Microservices Patterns to a Modular Monolith
Monolith vs Microservices by Simon Brown
Modularization

Applying Microservices Patterns to a Modular Monolith
Monolith vs Microservices by Simon Brown
Modularization

Applying Microservices Patterns to a Modular Monolith
Cross-pollination between architectures
Leverage and reuse work from the
microservices world into our modular app

Applying Microservices Patterns to a Modular Monolith
Domain for example

Applying Microservices Patterns to a Modular Monolith
Domain for example

Applying Microservices Patterns to a Modular Monolith
Domain for example

Applying Microservices Patterns to a Modular Monolith
Domain for example

Applying Microservices Patterns to a Modular Monolith
Domain for example

Applying Microservices Patterns to a Modular Monolith
Simple architecture

Applying Microservices Patterns to a Modular Monolith
Separating domains

Applying Microservices Patterns to a Modular Monolith
Problems when leaving one database
A
C
I
D
Local transactions are ensured by

Applying Microservices Patterns to a Modular Monolith
Problems when leaving one database
A
C
I
D
Atomicity
Consistency
Isolation
Durability
Local transactions are ensured by

Applying Microservices Patterns to a Modular Monolith
Problems when leaving one database
A
C
I
D
Atomicity
Consistency
Isolation
Durability
Local transactions are ensured by

Applying Microservices Patterns to a Modular Monolith
Not handling transactions

Applying Microservices Patterns to a Modular Monolith
Not handling transactions
Consistency issues

Applying Microservices Patterns to a Modular Monolith
Not handling transactions
Consistency issues
Devolve into
f
ixing data issues in production

Applying Microservices Patterns to a Modular Monolith
Not handling transactions
Consistency issues
Devolve into
f
ixing data issues in production
Important
f
lows break and we have no way of noticing

Applying Microservices Patterns to a Modular Monolith
Patterns
Proven solutions to recurring problems

Applying Microservices Patterns to a Modular Monolith
Microservices Patterns

Applying Microservices Patterns to a Modular Monolith
Microservices Patterns

Applying Microservices Patterns to a Modular Monolith
What patterns are we going to use?

Applying Microservices Patterns to a Modular Monolith
What patterns are we going to use?
One database per service/domain

Applying Microservices Patterns to a Modular Monolith
What patterns are we going to use?
One database per service/domain
Saga

Applying Microservices Patterns to a Modular Monolith
What patterns are we going to use?
One database per service/domain
Saga
Transactional Outbox

Applying Microservices Patterns to a Modular Monolith
One database per service/domain
Isolation of data structure

Applying Microservices Patterns to a Modular Monolith
One database per service/domain
Private tables per service
Isolation of data structure

Applying Microservices Patterns to a Modular Monolith
One database per service/domain
Private tables per service
Schema per service
Isolation of data structure

Applying Microservices Patterns to a Modular Monolith
One database per service/domain
Private tables per service
Schema per service
Database per service
Isolation of data structure

Applying Microservices Patterns to a Modular Monolith
Saga
Chris Richardson - https://microservices.io/patterns/data/saga.html
Eventual consistency across systems

Applying Microservices Patterns to a Modular Monolith
Saga
Chris Richardson - https://microservices.io/patterns/data/saga.html
Sequence of local transactions
Eventual consistency across systems

Applying Microservices Patterns to a Modular Monolith
Transactional Outbox
Chris Richardson - https://microservices.io/patterns/data/transactional-outbox.html
Atomically update state and publish events

Applying Microservices Patterns to a Modular Monolith
Transactional Outbox
Chris Richardson - https://microservices.io/patterns/data/transactional-outbox.html
Atomically update state and publish events

Applying Microservices Patterns to a Modular Monolith
Modular architecture

Applying Microservices Patterns to a Modular Monolith
Modular architecture
To be able to reference a User
entity indirectly, it will have the
same identi
f
ier across domains
and databases.

Applying Microservices Patterns to a Modular Monolith
Saga
f
low

Applying Microservices Patterns to a Modular Monolith
Saga
f
low

Applying Microservices Patterns to a Modular Monolith
Saga
f
low
User con
f
irms email

Applying Microservices Patterns to a Modular Monolith
Saga
f
low
User con
f
irms email

Applying Microservices Patterns to a Modular Monolith
Saga
f
low
User con
f
irms email
We update the status_code
attribute to con
f
irmed

Applying Microservices Patterns to a Modular Monolith
Saga
f
low

Applying Microservices Patterns to a Modular Monolith
Saga
f
low
Outbox record gets
created

Applying Microservices Patterns to a Modular Monolith
Saga
f
low

Applying Microservices Patterns to a Modular Monolith
Saga
f
low

Applying Microservices Patterns to a Modular Monolith
Saga
f
low
User record gets
created

Applying Microservices Patterns to a Modular Monolith
Saga
f
low

Applying Microservices Patterns to a Modular Monolith
Saga
f
low
Event collaboration

Applying Microservices Patterns to a Modular Monolith
Saga
f
low

Applying Microservices Patterns to a Modular Monolith
Saga
f
low
Successful sequence

Applying Microservices Patterns to a Modular Monolith
Saga
f
low
Successful sequence
Outbox record gets
created

Applying Microservices Patterns to a Modular Monolith
Saga
f
low

Applying Microservices Patterns to a Modular Monolith
Saga
f
low

Applying Microservices Patterns to a Modular Monolith
Saga
f
low
Rollback sequence

Applying Microservices Patterns to a Modular Monolith
Saga
f
low
Rollback sequence
Outbox record gets
created

Applying Microservices Patterns to a Modular Monolith
Saga
f
low
Rollback sequence

Applying Microservices Patterns to a Modular Monolith
Saga
f
low
Rollback sequence
Update UserRegistration to its previous
waiting_for_con
f
irmation state

Applying Microservices Patterns to a Modular Monolith
Saga
f
low
Rollback sequence
Update UserRegistration to its previous
waiting_for_con
f
irmation state
User record gets
destroyed

Applying Microservices Patterns to a Modular Monolith
Saga
f
low

Applying Microservices Patterns to a Modular Monolith
Demo

Applying Microservices Patterns to a Modular Monolith
Demo
Implementation of the mentioned domain and app

Applying Microservices Patterns to a Modular Monolith
Demo
Implementation of the mentioned domain and app
Rails multi-database support

Applying Microservices Patterns to a Modular Monolith
Demo
Implementation of the mentioned domain and app
Rails multi-database support
Ka
f
ka as a message bus

Applying Microservices Patterns to a Modular Monolith
Demo
Implementation of the mentioned domain and app
Rails multi-database support
Ka
f
ka as a message bus
Ka
f
ka Connect as the outbox publisher

Applying Microservices Patterns to a Modular Monolith
Demo
Implementation of the mentioned domain and app
Rails multi-database support
Ka
f
ka as a message bus
Ka
f
ka Connect as the outbox publisher
Kara
f
ka for consumers

Applying Microservices Patterns to a Modular Monolith

Applying Microservices Patterns to a Modular Monolith

Applying Microservices Patterns to a Modular Monolith
How did we accomplish this?

Applying Microservices Patterns to a Modular Monolith
How did we accomplish this?

Applying Microservices Patterns to a Modular Monolith
How did we accomplish this?

Applying Microservices Patterns to a Modular Monolith
Event consumers

Applying Microservices Patterns to a Modular Monolith
Event consumers

Applying Microservices Patterns to a Modular Monolith
Event consumers

Applying Microservices Patterns to a Modular Monolith
Event consumers

Applying Microservices Patterns to a Modular Monolith
Creating the Outbox record

Applying Microservices Patterns to a Modular Monolith
Creating the Outbox record

Applying Microservices Patterns to a Modular Monolith
Creating the Outbox record

Applying Microservices Patterns to a Modular Monolith
Creating the Outbox record

Applying Microservices Patterns to a Modular Monolith
Creating the Outbox record

Applying Microservices Patterns to a Modular Monolith
Creating the Outbox record

Applying Microservices Patterns to a Modular Monolith
Creating the Member record

Applying Microservices Patterns to a Modular Monolith
Creating the Member record

Applying Microservices Patterns to a Modular Monolith
Creating the Member record

Applying Microservices Patterns to a Modular Monolith
Creating the Member record

Applying Microservices Patterns to a Modular Monolith
Creating the Member record

Applying Microservices Patterns to a Modular Monolith
Creating the Member record

Applying Microservices Patterns to a Modular Monolith
Registration rollback

Applying Microservices Patterns to a Modular Monolith
Registration rollback

Applying Microservices Patterns to a Modular Monolith
Where to
f
ind the code
github.com/
rootstrap/
rails-modular-monolith-with-ddd/
tree/
main-saga

Guillermo Aguirre - RailsConf 2023
What to take away
Applying Microservices Patterns to a Modular Monolith

Guillermo Aguirre - RailsConf 2023
What to take away
3 widely used patterns
Applying Microservices Patterns to a Modular Monolith

Guillermo Aguirre - RailsConf 2023
What to take away
3 widely used patterns
Dealing with distributed transactions
Applying Microservices Patterns to a Modular Monolith

Guillermo Aguirre - RailsConf 2023
What to take away
3 widely used patterns
Dealing with distributed transactions
It's possible to leverage di
ff
erent solutions
Applying Microservices Patterns to a Modular Monolith

Guillermo Aguirre - RailsConf 2023
What to take away
3 widely used patterns
Dealing with distributed transactions
It's possible to leverage di
ff
erent solutions
Not a silver bullet
Applying Microservices Patterns to a Modular Monolith

Guillermo Aguirre - RailsConf 2023
What to take away
3 widely used patterns
Dealing with distributed transactions
It's possible to leverage di
ff
erent solutions
Not a silver bullet
Inspiring others
Applying Microservices Patterns to a Modular Monolith

Guillermo Aguirre - RailsConf 2023
Thank you!
Applying Microservices Patterns to a Modular Monolith
@guillermoap
@guillermoap_
@guillermo.aguirre
Guillermo Aguirre
/gee-SHER-moh ah-GHEE-rreh/