25
REFACTORING INTO MICROSERVICES Ismael Rivera Lead Software Engineer at Altocloud

Refactoring for microservices

Embed Size (px)

Citation preview

Page 1: Refactoring for microservices

REFACTORING INTO MICROSERVICES

Ismael RiveraLead Software Engineer at Altocloud

Page 2: Refactoring for microservices

MEET THE MONOLITH

• A single codebase

• Hard to scale the application

• Impossible to scale the team

Page 3: Refactoring for microservices

WHY MICROSERVICES?• Modularity

• Separation of Concerns

• Single Responsibility Principle

• Business Agility

• Independently deployable

Page 4: Refactoring for microservices

HOW DO I START?• DevOps

• Service Discovery

• Monitoring

• Logging

• Testing strategy

Page 5: Refactoring for microservices

REFACTOR YOUR APPLICATION

Big Ball of Mud? Stop!

Page 6: Refactoring for microservices

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

REFACTOR YOUR APPLICATION

• Extract services

OrderModule

FulfilmentModule

Payment Module ...

Database

API Gateway

OrderService

FulfilmentService

PaymentService

Database

API

API

API

Page 8: Refactoring for microservices

v1v1

v1

REFACTOR YOUR DATA

Database

OrderService

FulfilmentService

PaymentService

API

API

API

v2

Page 9: Refactoring for microservices
Page 10: Refactoring for microservices

–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

REFACTOR YOUR DATA

OrderService

FulfilmentService

PaymentService

Database

API

API

API

Database Database

Page 12: Refactoring for microservices

REFACTOR YOUR DATA

OrderService

FulfilmentService

PaymentService

Database

API

API

API

Database Database SparkSpark

Page 13: Refactoring for microservices

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

REFACTOR YOUR DATA

• Forget ACID transactions

• Forget a single Enterprise Domain Model

• Forget Relational Integrity

Page 15: Refactoring for microservices

REFACTOR YOUR DATA

OrderService

FulfilmentService

Database

API

API

Database Database

PaymentService

API

tx tx tx

Page 16: Refactoring for microservices

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

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

REFACTOR YOUR DATA• Duplicate vs Pull

OrderService

API

EmailService

API

PaymentService

APIMessage Broker

Pull

OrderOrder

Template

Publish

Subscribe

Publish

Page 19: Refactoring for microservices

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

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

REFACTOR YOUR DATA• Strangler

LegacyA

B...

AB

...Legacy

Router Router

Page 22: Refactoring for microservices

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

REFACTOR YOUR DATA

• CRUD

• Event-driven architecture

• CQRS (Command Query Responsibility Segregation)

• Event Sourcing

Page 24: Refactoring for microservices

–Kent Beck

“Make the change easy, then make the easy change.”

Page 25: Refactoring for microservices

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.