This document discusses strategies for migrating a monolithic application to microservices. It begins with an overview of decomposing a monolith and implementing new features as microservices. It then provides an example of extracting the delivery module from a monolithic food delivery application into its own delivery microservice. The example outlines the steps of splitting the code, extracting the relevant database tables, defining and deploying the new delivery service, integrating it with the monolith, and removing the old code. Finally, it discusses implementing a delayed delivery service as another example.
6. @crichardson
Agenda
Why microservices?
Overview of refactoring a monolith
Implementing new features as services
Extracting modules into services
Example: extracting the Delivery Service
Example: implementing the Delayed Delivery Service
7. @crichardson
+
Marketplace is volatile, uncertain, complex
and ambiguous
+
Businesses must innovate faster
Deliver software rapidly, frequently and reliably
Software
9. The microservice architecture is
an architectural style
that structures an application as a
set of services*
Each microservice is:
ā¢ highly maintainable and testable
ā¢ loosely coupled
ā¢ independently deployable
ā¢ organized around business capabilities
ā¢ owned by a small team
*Start with one service per team until it becomes a problem
10. @crichardson
Process: Lean + DevOps/Continuous Delivery & Deployment
Organization: Small,
autonomous,
product teams
Architecture:
microservices
Testability
Deployability
Modularity
Modularity
Evolvability
Maintainability
Rapidly, frequently
and reliably delivery of
changes to long-lived
applications
11. @crichardson
Agenda
Why microservices?
Overview of refactoring a monolith
Implementing new features as services
Extracting modules into services
Example: extracting the Delivery Service
Example: implementing the Delayed Delivery Service
14. @crichardson
Make the most of the
monolithic architecture
The monolithic architecture is not an anti-pattern
If software delivery is slow
Optimize development process
Improve deployment pipeline = more automation
Improve team autonomy
Modularize the monolith
Eliminate hand-offs and create cross functional teams
If technology stack is obsolete modernize to a new monolith
ā¦
15. @crichardson
If and only if that is
insufļ¬cient* then consider
migrating to microservices
*Large, complex applications developed by a
(usually) large team that need to be delivered
rapidly, frequently, and reliably
16. @crichardson
How do you decompose your
big, scary monolithic application?
Monolith
Service?
Microservice architecture
Service
Service
Service
18. @crichardson
According to Randy Shoup:
āAs Martin Fowler likes to say, the only
thing a Big Bang rewrite guarantees is a
Big Bang!ā
Via @randyshoup
25. @crichardson
Measuring success
Success != Number of Microservices
Improved metrics:
Reduced lead time
Increased deployment frequency
Reduced changed failure rate
Improvements in other -ilities
ā¦
Anti-pattern:
Microservices as the goal
26. @crichardson
Repeat extracting
services until:
ā¢ Eliminated the
monolith
ā¢ Solved software
delivery problems
ā¢ Higher priority work
Monolith
Time
Monolith
Service
Monolith
Service
Service
Monolith
Service
Service
Service
Service
ā¦. Monolith
Service
Service
Service
Service
Service
Service
Service
Service
Service
Service
Service
Service
Service
Service
Service
Service
ā¦.
Strangler application
The strangler application grows larger over time
The monolith shrinks over time
Years
27. @crichardson
What to extract?
Have the ideal microservice architecture in mind
Limited timeboxed architecture deļ¬nition effort
But be prepared to revise as you learn more
Start with the modules that would give you the greatest return
on investment (ROI)
28. Cost vs. Beneļ¬t of extraction
Beneļ¬t
Solves a signiļ¬cant
problem
Velocity frequently
updated
Scalability Conļ¬icting
resource requirements
ā¦
Cost
Cost of changing the
monolith and adapting/
rewriting module
Difļ¬culty in decoupling/
breaking dependencies
Need to participate in
sagas/compensating
transactions
29. @crichardson
Cost of decoupling: ā # inbound dependencies
FTGO
<<module>>
Module A
<<module>>
Module B
<<module>>
Module C
Ext. API
Ext. API
Ext. API
Easy
Difļ¬cult
0
1
2
Dependencies can determine ordering
31. @crichardson
Extracting a service: cost vs.
Beneļ¬t
Beneļ¬t of extraction
Ease of extraction
High
HighLow
Low
Module B
Module A
Module CModule D
Module E
33. @crichardson
Agenda
Why microservices?
Overview of refactoring a monolith
Implementing new features as services
Extracting modules into services
Example: extracting the Delivery Service
Example: implementing the Delayed Delivery Service
34. @crichardson
Letās imagine you want to implement
a signiļ¬cant new featureā¦.
The Law of Holes:
"if you ļ¬nd yourself in a hole, stop digging"
(https://en.m.wikipedia.org/wiki/Law_of_holes)
Stop making the monolith larger!
35. @crichardson
Implement new functionality as a
service
Monolith Service
Data integration glue
API Gateway
Request
Monolithic
database
Service
database
Code in service/monolith
that enables service to
access monolithās data
Pristine
new
service
36. @crichardson
Service Monolith data integration glue:
API invocation
Monolith Service
Monolithic
database
Service
database
Invokes
Command/
Query
37. @crichardson
Service Monolith data integration glue:
replicate data for reading
Monolith Service
Monolithic
database
Service
database
Application events
DB replicationTables Replica
CDC Events
38. @crichardson
Service Monolith data
access options
Option Beneļ¬ts Drawbacks
API call to
monolith
Simple
Works for writes and reads
Doesnāt efļ¬ciently support
complex queries
Replicate
data to
service
Supports complex queries
Only for reads
Cost/complexity of replication
39. @crichardson
Data replication options
Option Beneļ¬ts Drawbacks
Application events
Easy to publish meaningful events
Decoupled
Requires code changes in monolith
Eventual consistency
Change data capture
CRUD Events
No code changes in monolith
Decoupled
Publishing non-CRUD event is tricky
Eventual consistency
Triggers that publish
CRUD events
Simple
No code changes in monolith
Decoupled
Triggers = yuck
Publishing non-CRUD event is tricky
Eventual consistency
Triggers that update
target DB
Simple
No code changes in monolith
ACID consistency
Triggers = yuck
Coupled to target DB
Other DB replication
mechanism, e.g. OGG
Simple
No code changes in monolith
Coupled to target DB
Eventual consistency
40. @crichardson
Agenda
Why microservices?
Overview of refactoring a monolith
Implementing new features as services
Extracting modules into services
Example: extracting the Delivery Service
Example: implementing the Delayed Delivery Service
42. @crichardson
Deļ¬ne a two way āremotableā
interface
Monolithic database
Moduleās tables
Module
Untangle
dependencies
Monolith
Ext. API
Modifying the
monolith is
tricky
43. @crichardson
Incrementally modify monolith
to invoke module via new API*
Module
Module
All writes - module/service owns its data
Ideally all reads - but can replicate changes back
*Branch by abstraction pattern
44. @crichardson
Transform the database schema
ORDER_ID STATE ā¦ DELIVERY_STATE COURIER_ID
ā¦ ā¦ ā¦ ā¦ ā¦
ORDER_ID STATE ā¦ STATE COURIER_ID
ā¦ ā¦ ā¦ ā¦ ā¦
ID
ā¦
ORDERS table
ORDERS table DELIVERY table
Extract delivery service
Delivery management
45. @crichardson
Replicating data to reduce scope of
refactoring
ORDER_ID STATE ā¦ DELIVERY_STATE COURIER_ID
ā¦ ā¦ ā¦ ā¦ ā¦
ORDER_ID STATE ā¦ STATE COURIER_ID
ā¦ ā¦ ā¦ ā¦ ā¦
ID
ā¦
COURIER_ID
ā¦
ORDERS table
ORDERS table DELIVERY table
Replicate back
Read-only replica Extract delivery service
47. @crichardson
Agenda
Why microservices?
Overview of refactoring a monolith
Implementing new features as services
Extracting modules into services
Example: extracting the Delivery Service
Example: implementing the Delayed Delivery Service
48. @crichardson
AS-IS architecture
FTGO
<<module>>
Order and Delivery Management
<<entity>>
Order
ORDER_ID STATE ā¦ DELIVERY_STATE COURIER_ID
ā¦ ā¦ ā¦ ā¦ ā¦
Consumer
Courier
ORDER table
noteAvailable()
notePickedUp
noteDelivered()
ā¦
Tangled code
and data
Want to
extract
52. @crichardson
AS-IS TO-BE: Overview of
extracting the Delivery Service
1. Split the code: convert delivery management code into a
module within the monolith
2.Split the database: deļ¬ne separate DB schema for delivery
management within the monolith
3.Deļ¬ne and deploy Delivery Service
4.Use the Delivery Service
5.Remove old code from monolith
Commit and deploy changes after each step
56. @crichardson
Deļ¬ning the DELIVERY table
create table ftgo_delivery.delivery (ā¦)
select ā¦subset of columnsā¦
from ftgo.orders
where assigned_courier_id is not null;
CREATE TRIGGER assigned_courier_id_updated
AFTER UPDATE
ON delivery
FOR EACH ROW
BEGIN
UPDATE ftgo.orders
SET assigned_courier_id = NEW.assigned_courier_id
where id = NEW.id;
END;;
Replicate assigned_courier back to ORDERS table
57. @crichardson
Delivery Service schemaFTGO schema
But itās not just the DELIVERY table
COURIER
id
name
availability
COURIER
id
name
availability
ā¦
COURIER_
ACTIONS
ā¦
COURIER_
ACTIONS
ā¦.
RESTAURANT
id
name
address
RESTAURANT
id
name
address
ā¦
Sync
58. @crichardson
Replicating the RESTAURANT
table
create table ftgo_delivery.restaurants
select ā¦subset of columnsā¦
from ftgo.restaurants
CREATE TRIGGER restaurant_created AFTER INSERT
ON ftgo.restaurants
FOR EACH ROW
BEGIN
INSERT INTO ftgo_delivery_service.restaurants(ā¦)
ā¦
61. @crichardson
Integration using asynchronous messaging
Delivery
Service
Delivery
Service
schedule()
reschedule()
cancel()
Delivery
Controller
Order
Service
FTGO
Order Controller
Delivery
Service
Proxy
schedule()
ā¦
Command
Handlers
Schedule
command
message
Message
channel
63. @crichardson
Agenda
Why microservices?
Overview of refactoring a monolith
Implementing new features as services
Extracting modules into services
Example: extracting the Delivery Service
Example: implementing the Delayed Delivery Service
64. @crichardson
FTGO Delayed delivery service
As a workaround for a mediocre
courier scheduling algorithm
that is taking too long to ļ¬xā¦ā¦
Improve customer satisfaction
by proactively notifying
customers (and customer
experience) that their order
wonāt be delivered on time
65. FTGO Delayed delivery service
Periodically looks for
orders that wonāt be
delivered on time
Not picked up and
restaurant closed
Courier delayed
ā¦
Notiļ¬es consumer
and CRM system
getDelayedOrders()
Notiļ¬cation
Service
CRM system
Send
apology
notiļ¬cation
create case
REST
API
???
Delayed Order Service
Ā«stereotypeĀ»
Order
Ā«entityĀ»
Restaurant
Ā«entityĀ»
OpeningHours
Ā«entityĀ»
Notiļ¬cation
API gateway
Ā«repositoryĀ»
Customer
ContactInfo
Repository
Monolith
???
REST
API
Ā«ServiceĀ»
DelayedDelivery
Service
Integration glue
Need to design
66. @crichardson
Delayed delivery service: data
integration strategies
Usage How
Orders
ācomplexā query to
determine whether
delivery will be
delayed
ReplicateRestaurants
Courier
Consumers
Contact info for
notiļ¬cations
Query via API
67. @crichardson
Summary
The microservice architecture enables the rapid, frequent and
reliable delivery of changes to long-lived applications
Incrementally migrate to microservices when you have
outgrown your monolith
Implement new features as services
Extract modules into service
Prioritize extracting modules that give the greatest beneļ¬t