refactoring for microservices
TRANSCRIPT
![Page 1: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/1.jpg)
REFACTORING INTO MICROSERVICES
Ismael RiveraLead Software Engineer at Altocloud
![Page 2: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/2.jpg)
MEET THE MONOLITH
• A single codebase
• Hard to scale the application
• Impossible to scale the team
![Page 3: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/3.jpg)
WHY MICROSERVICES?• Modularity
• Separation of Concerns
• Single Responsibility Principle
• Business Agility
• Independently deployable
![Page 4: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/4.jpg)
HOW DO I START?• DevOps
• Service Discovery
• Monitoring
• Logging
• Testing strategy
![Page 5: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/5.jpg)
REFACTOR YOUR APPLICATION
Big Ball of Mud? Stop!
![Page 6: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/6.jpg)
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
![Page 7: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/7.jpg)
REFACTOR YOUR APPLICATION
• Extract services
OrderModule
FulfilmentModule
Payment Module ...
Database
API Gateway
OrderService
FulfilmentService
PaymentService
Database
API
API
API
![Page 8: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/8.jpg)
v1v1
v1
REFACTOR YOUR DATA
Database
OrderService
FulfilmentService
PaymentService
API
API
API
v2
![Page 9: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/9.jpg)
![Page 10: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/10.jpg)
–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.”
![Page 11: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/11.jpg)
REFACTOR YOUR DATA
OrderService
FulfilmentService
PaymentService
Database
API
API
API
Database Database
![Page 12: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/12.jpg)
REFACTOR YOUR DATA
OrderService
FulfilmentService
PaymentService
Database
API
API
API
Database Database SparkSpark
![Page 13: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/13.jpg)
REFACTOR YOUR DATA
OrderService
Database
API
PaymentService
API
Database FulfilmentService
API
Database
SparkSparkMessage Broker
Fraud detection?
Stolen card
Stolen card
![Page 14: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/14.jpg)
REFACTOR YOUR DATA
• Forget ACID transactions
• Forget a single Enterprise Domain Model
• Forget Relational Integrity
![Page 15: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/15.jpg)
REFACTOR YOUR DATA
OrderService
FulfilmentService
Database
API
API
Database Database
PaymentService
API
tx tx tx
![Page 16: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/16.jpg)
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
![Page 17: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/17.jpg)
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.
![Page 18: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/18.jpg)
REFACTOR YOUR DATA• Duplicate vs Pull
OrderService
API
EmailService
API
PaymentService
APIMessage Broker
Pull
OrderOrder
Template
Publish
Subscribe
Publish
![Page 19: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/19.jpg)
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
![Page 20: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/20.jpg)
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
![Page 21: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/21.jpg)
REFACTOR YOUR DATA• Strangler
LegacyA
B...
AB
...Legacy
Router Router
![Page 22: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/22.jpg)
REFACTOR YOUR DATA• Branch by Abstraction
Legacy
Consumer
Legacy
Consumer
Abstraction Layer
Legacy New
Abstraction Layer
Consumer
New
Consumer
Abstraction Layer
![Page 23: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/23.jpg)
REFACTOR YOUR DATA
• CRUD
• Event-driven architecture
• CQRS (Command Query Responsibility Segregation)
• Event Sourcing
![Page 24: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/24.jpg)
–Kent Beck
“Make the change easy, then make the easy change.”
![Page 25: Refactoring for microservices](https://reader031.vdocuments.us/reader031/viewer/2022030305/587085d11a28ab57368b790d/html5/thumbnails/25.jpg)
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.