Microservice Architecture
Microservice Architecture
ARCHITECTURE
INTRODUCTION
Strategic objective in modern organizations
Infrastructure – private cloud/virtualized medium
Compute
Storage
Network
Everything as a service
PasS
Standardized Application Development
Application Store
Zero production intervention
LEVEL
ARCHITEC
Backend for Frontend Routing/Caching
DMZ
Intranet
MICROSERVICE DESIGN
– HOW TO ?
• A guidance framework for successfully design a
microservice architecture
• Define how dependencies between services will be managed
• Identify services by applying patterns for:
• Decoupling functionality
• Decomposing functionality
• Maximize loose coupling of services through the designs of their
interfaces:
• Decoupled from implementations
• Decoupled from data
• Implement a lifecycle management strategy for services
MICROSERVICE DESIGN –
HOW TO ?
Prework • Define application domain
covered for
example by
Kubernetes/Open
Dependen • Distributed configuration to
shift
cy decouple services from the
Managem environment
• Service discovery –
ent decouple services from
each other
Strategies
Services • Iterate
• Decomposition strategies Partially
Identificat • Decouple using anti- covered by API
ion corruption layers
Management
Service
Lifecycle • Versioning
Managem • Service dependencies
tracking
ent
MICROSERVICES
DESIGN - PREWORK
• For the organization:
• Adapt internal processes for microservices :
• high degree of process maturity:
• development
• operational
• High degree of automation
• Organize people and build skills for microservices
• Development teams: small autonomous teams accountable for all aspects of
the service they manage, coding -> operations
• Define the technology platform for microservices:
• The platform should help managing the complexity, e.g. operational
complexity
Service A
Server side service Instance 1
discovery
Example LBs:
AWS Elastic Load Service Discovery
Balancer update
Service A
Instance 2
Nginix
Service discovery
component generates Consumer Service
request
nginix.conf file and runs Instance
command for nginix to request Service A
reload configuration Instance 3
Load Balancer
Openshift/Kubernetes
Services
SERVICE DISCOVERY
Register availability
Client side service discovery +
Heartbeat
Service A
Instance 1
Service Discovery
Service A
1. Lookup
Instance 2
DNS/REST
Consumer Service
Instance
2. request
Service A
Instance 3
SERVICES IDENTIFICATION
SITION
STRATEGI Extend Extend the monolith
ES
SPLIT THE MONOLITH
DECOMPOSITION A B C D
INITIAL STATE
STRATEGIES – SPLIT E
THE MONOLITH
Split the monolith: F G H I
ITERATION 1
Look for Seams/Fissures where there is low degree of
dependency – candidates for splitting points:
E
architectural/deployment pattern – e.g. separate by
layer/tier, backend from frontend or presentation,
business logic and data access
F G H I
logic – areas of functionality with limited relationship
between them
data – areas of application dealing with data entities with
limited relationship between them
Example:
A B C D
Online banking application:
ITERATION 2
Accounts
Transactions list E
Payments
Investments
F G H I
User profile management
STRANGLE/SHAVE THE MONOLITH
DECOMPOSITION A B C D
INITIAL STATE
STRATEGIES – SHAVE
THE MONOLITH
E
F G H I
ITERATION 1
Identify dependencies in logic and data
Lucky case: no dependencies -> can be E
directly decoupled
Dependencies found -> design an
abstraction layer to keep new service F G H I
decoupled
With each iteration monolith gets smaller
and smaller
Monolith is changed with each iteration A B C D
ITERATION 2
E
F G H I
EXTEND THE MONOLITH
DECOMPOSITION A B C D
STRATEGIES –
INITIAL STATE
EXTEND THE
E
MONOLITH F G H I
ITERATION 1
Implement new features as independent
services E
Example:
add notification service to order F G H I Y
management system
Monolith could have small update to
publish order status as event
Further notification service may be Z A B C D X
ITERATION 2
managed independently and may trigger
new functionality in other services such
as for example stock replenishment. E
T F G H I Y
DECOUPLING USING ANTI-
CORRUPTION LAYERS
Goal:
Hide internal domain models of legacy application and prevent them from corrupting the design of the new service
Allows system components to use optimal domain models internally and to interact with components using other models, e.g.
legacy models
Decouples old and new environments
Microservice interacting with monolithic application:
Shared repository
The new service does not have its own model
ACL must provide interfaces that implement repository that is backed by the legacy application
Synchronized repository
The new service has its own specific persistence implementation
ACL implements synchronization between service domain and legacy domain
Synchronization must ensure data consistency:
Data-driven, e.g. batch reconciliation
Event-driven
ACL
Abstraction layer
implementation:
Mediator
Façade
Gateway
DECOUPLING USING ANTI-
CORRUPTION LAYERS
Shared repository
ACL
A B C
Mediator X
E Data
Monolith
Domain
translation
C<->X
H<->Y
F G H
Y
ACL
A B C
Async
Coordinator updates X
E
Monolith
Domain Async
translation updates
C<->X
H<->Y
F G H
Y
ACL
events
A B C
Coordinator X
E events
Monolith
Domain
translation
C<->X
events
H<->Y
F G H
Y
Service
App Process
Boundary
Outer API
API Mediation
Microservice ABC domain / Inner APIs Microservice XYZ domain / Inner APIs
Request-
Event driven response
branch
step2
Fork/join
step3
- Implementation:
Code it! step4
Advantages:
Service A
Loosely coupled model event1 Service B
No central definition of the flow <<pub>>
Additional participants can be introduced as
publishers/subscribers <<pub>>
event2
Asynchronous processing <<sub>> <<sub>>
Scalability – no need to scale up orchestrator but
only participants
event3
Disadvantages
Service C Service D
Hard to follow interactions <<pub>> Event Broker
Need to correlate and aggregate logging
SERVICES DESIGN
- DATA DECOUPLING
Maximum agility and independence between services principles:
Data owned by a microservice can only be accessed through the interface provided
by the microservice
No database enforced relationship (e.g. referential integrity) between data owned
by a microservice and data owned by another
Advantages
Agility+flexibility+Independence decision on about data persistence per service
Implementations
existing database and decoupled data table per service
Schema per service
Database per service
Disadvantages
Service must manage:
Consistency
Data relationships
SERVICES DESIGN
- DATA DECOUPLING
Monolith
– independent services – decoupled services
– coupled services
– coupled data - decoupled data
– coupled data
Deployment unit Deployment unit Deployment unit Deployment unit Deployment unit
Application Data Store Application Data Store Service A Data Store Service B Data Store
Higher decoupling
Lower decoupling
SERVICES Deployment unit Deployment unit
DESIGN
- DATA Service A
Check constraint
Service B
DECOUPLING
Example:
Foreign key constraint
check
Service B exposes an
interface towards Service
A where constraints can
be checked Service A Data Service B Data
A B
1
PK id PK id
*
... FK a_id
...
SERVICES DESIGN
- DATA DECOUPLING /a/{id}/b
Make transfer
SERVICES DESIGN
- DATA DECOUPLING
Service Implementation
POST
Query
/transfers
J:TRANSFER_COMPLETED
D: ACCOUNT_DEBITED H: ACCOUNT_CREDITED
A: TRANSFER_NEW F: DEBIT_ACK B: TRANSFER_NEW
G: DEBIT_ACK
E: ACCOUNT_DEBITED I: ACCOUNT_CREDITED
Event Store
Transfer Event Queue Account Event Queue
Optimize reads
Solution: Command Query
Responsibility Segregation
Independently optimizes read and writes
when unbalanced, e.g. many reads, few
writes
Disadvantages
Complexity
Costs
Synchronization synchronization
Service A Data Service A Data
Change-data-capture Write Model Read Model
Data pump to pull updates
Event sourcing: write svc keeps event
log as persistence model and read
service subscribes
Service Consumer
Get account
Update Account
balance
SERVICES DESIGN
Service Implementation
- DATA DECOUPLING
Command/
Query
Modify
publish
append
Event 1: Account W
opened
LIFECYCLE
Build
Deprecate Run
Service implementation
Lifecycle
Plan
(frequent)
Deploy
Build