Download - Refactoring for microservices
REFACTORING INTO MICROSERVICES
Ismael RiveraLead Software Engineer at Altocloud
MEET THE MONOLITH
• A single codebase
• Hard to scale the application
• Impossible to scale the team
WHY MICROSERVICES?• Modularity
• Separation of Concerns
• Single Responsibility Principle
• Business Agility
• Independently deployable
HOW DO I START?• DevOps
• Service Discovery
• Monitoring
• Logging
• Testing strategy
REFACTOR YOUR APPLICATION
Big Ball of Mud? Stop!
REFACTOR YOUR APPLICATION
• Split front-end & back-end
Presentation layer (HTML)
Data Access Object
Business Logic
Browser
Database
Webapp(HTML)
API
Browser
Database
DAO
Business Logic
REFACTOR YOUR APPLICATION
• Extract services
OrderModule
FulfilmentModule
Payment Module ...
Database
API Gateway
OrderService
FulfilmentService
PaymentService
Database
API
API
API
v1v1
v1
REFACTOR YOUR DATA
Database
OrderService
FulfilmentService
PaymentService
API
API
API
v2
–Werner Vogels
“For us service orientation means encapsulating the data with the business logic that operates on the data, with the only access through a published service interface. No direct database access is allowed from
outside the service, and there’s no data sharing among the services.”
REFACTOR YOUR DATA
OrderService
FulfilmentService
PaymentService
Database
API
API
API
Database Database
REFACTOR YOUR DATA
OrderService
FulfilmentService
PaymentService
Database
API
API
API
Database Database SparkSpark
REFACTOR YOUR DATA
OrderService
Database
API
PaymentService
API
Database FulfilmentService
API
Database
SparkSparkMessage Broker
Fraud detection?
Stolen card
Stolen card
REFACTOR YOUR DATA
• Forget ACID transactions
• Forget a single Enterprise Domain Model
• Forget Relational Integrity
REFACTOR YOUR DATA
OrderService
FulfilmentService
Database
API
API
Database Database
PaymentService
API
tx tx tx
REFACTOR YOUR DATA
Database
Database
Database
PaymentService
API
Message Broker
Order created
Order created
Payment received
Payment received
Order confirmed Order confirmed
Order shippedOrder shipped
FulfilmentService
APIOrderService
API
REFACTOR YOUR DATA• Domain-Driven Design
Customers
Products OrdersCategories
Customers
Orders
Tickets Assignee
Address
Bounded Context
Bounded Context
Microservices is about boundaries and so is DDD.
REFACTOR YOUR DATA• Duplicate vs Pull
OrderService
API
EmailService
API
PaymentService
APIMessage Broker
Pull
OrderOrder
Template
Publish
Subscribe
Publish
REFACTOR YOUR DATA
id name email1 Mark [email protected] John [email protected] Siobhan [email protected]
CUSTOMERS
id customer_id amount1 2 125.332 3 77.81
ORDER
CREATE TABLE orders ( id INT PRIMARY KEY, customer_id INT NOT NULL, ... CONSTRAINT fk_inv_customer_id FOREIGN KEY (customer_id) REFERENCES customers (id) ON DELETE CASCADE);
• Forget Relational Integrity
REFACTOR YOUR DATA
id name email new_column
1 Mark [email protected]
2 John [email protected]
3 Siobhan [email protected]
ALTER TABLE ADD new_column VARCHAR(255);
OrderService
API
[{ "id": 1, "name": "Mark", "email": "[email protected]", "new_column": "my default" }, { "id": 2, ... }]
JSON
REFACTOR YOUR DATA• Strangler
LegacyA
B...
AB
...Legacy
Router Router
REFACTOR YOUR DATA• Branch by Abstraction
Legacy
Consumer
Legacy
Consumer
Abstraction Layer
Legacy New
Abstraction Layer
Consumer
New
Consumer
Abstraction Layer
REFACTOR YOUR DATA
• CRUD
• Event-driven architecture
• CQRS (Command Query Responsibility Segregation)
• Event Sourcing
–Kent Beck
“Make the change easy, then make the easy change.”
TAKE-AWAYS• Start small, look at this as an iterative problem.
• There isn't one right answer.
• Easier to scale.
• Failure will always happen.
• Don't over refactor.