0% found this document useful (0 votes)
7 views

Microservices Design Patterns

Copyright
© © All Rights Reserved
Available Formats
Download as KEY, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Microservices Design Patterns

Copyright
© © All Rights Reserved
Available Formats
Download as KEY, PDF, TXT or read online on Scribd
You are on page 1/ 49

Microservices

Design
Patterns
forJava Applications
Arun GuptaVice President, Developer Advocacy
@arungupta, blog.arungupta.me
[email protected]
Monolith Application

UI CSS
CSS Databas
e Cache
Load HTML
Balancer HTML
HTML HTML
JPA
HTML

Business Logic
HTML
HTML HTML
HTML HTML
HTML DB
CDI REST JSF
Monolith Application
EAR
Load
EAR Cache
Balancer
WAR
WAR
WAR

JAR
JAR
JAR
JAR
DB
Advantages of Monolith
Application

Typically packaged in a single .ear

Easy to test (all required services are up)

Simple to develop
Monolith Application
Version 1

UI CS
S
Databa Version 2
se
HTML
HTML
UI CS
HTML
HTML
S
Databa Version 3
se
HTML
HTML
UI CS
HTML
HTML
S
Databa
Business Logic Version 4
se
HTML HTML
HTML
HTML
HTML HTML
HTMLBusiness
HTML
HTML
HTML
Logic UI CS
S
Databa
se
HTML
HTML
HTML HTMLBusiness
HTML HTML Logic
HTML
HTML HTML HTML

HTML
HTML HTML
HTML HTML
HTML
Business Logic
HTML
HTML HTML
HTML HTML
HTML
Disadvantages of
Monolith Application

Difficult to deploy and maintain

Obstacle to frequent deployments

Makes it difficult to try out new


technologies/framework
Scaling µservices
Microservices
An architectural approach, that emphasizes the
decomposition of applications into single-
purpose, loosely coupled services managed by
“building applications
cross-functional as suites
teams, of services.
for delivering and
As well as complex
maintaining the fact
“Type a that services
software
quote are with the
systems
here.”
independently deployable
velocity and quality and scalable,
required by today’s digital
each service also business
provides a firm module
boundary, even allowing for different
services to be written in different
–Johnny Appleseed
programming languages. They can also
be managed by different teams”
Characteristics of
Order
UI
microservices
👱👱 🙍🙍
👱

Business Logic User

👱👱 🙍🙍
👱

Persistence Catalog

👱 🙍
👱👱 🙍
Any organization that designs a system (defined
more broadly here than just information systems)
will inevitably produce a design whose structure is a
copy of the organization's communication structure.
“Any organization that designs a
system (defined more broadly here than
just information systems) will inevitably
produce a design whose structure is a copy
“Type a quote communication
of the organization's here.”
structure.”–Melvin Conway

–Johnny Appleseed
Teams around business
Characteristics

capability
Order User

👱👱 🙍🙍 👱👱 🙍🙍
👱 👱
Catalog

👱👱 🙍🙍
👱
Single Responsibility
Characteristics

Principle
DO

THING
Explicitly Published
Characteristics

interface
Independently replaceable
Characteristics

and upgradeable
Characteristics

“you build
With great
power, comes
great
responsibility

“you build it, you run


it!”
Characteristics

Designed for failure

Fault tolerance is a require


feature
Characteristics
Characteristics

100% automated
Sync or Async
Characteristics

Messaging
Characteristics

REST vs Pub/Sub
P1
GET
C1

PUT P2
C2
POST P3 Queu
e
C3
DELETE
P4
“Smart endpointsDumb
Characteristics

pipes”
Domain driven design (full-stack developer)

Explicitly published interface

Independently deployable and automated (CI


/ CD)

Decentralized, use IPC to communicate

“Smart endpoints, dumb pipes”


SOA

SOA 2.0

Hipster SOA

SOA done right

SOA++
SOA 2.0?

Conway’s Law
Service Discovery
Immutable VM
Strategies for
decomposing

Verb or usecase - e.g. Checkout UI

Noun - e.g. Catalog product service

Single Responsible Principle - e.g. Unix


utilities
Strategies for
decomposing

Verb or usecase - e.g. Checkout UI

Noun - e.g. Catalog product service

Single Responsible Principle - e.g. Unix


utilities
Towards microservices
Cache
EAR
Load
Balancer WAR
WAR

JAR
JAR
JAR
JAR DB

Service A Cache DB

WAR
Aggregator Pattern #1
Load
Balancer Service A Cache DB

WAR

Aggregator
Aggregator Cache DB
Service B
WAR

Service C Cache DB

WAR
Proxy Pattern #2
Load
Balancer Service A Cache DB

WAR

Proxy
Aggregator Cache DB
Service B
WAR

Service C Cache DB

WAR
Chained Pattern #3
Load
Balancer

Service A Service B Service C


WAR WAR WAR

Cache DB Cache DB Cache DB


Load
Branch Pattern #4
Balancer
Service B
WAR

Cache DB

Service A
WAR
Service C Service D
WAR WAR
Cache DB

Cache DB Cache DB
Shared Resources #5
Load
Balancer
Service B
WAR

Cache DB

Service A
WAR
Service C Service D
WAR WAR
Cache DB

Cache DB
Async Messaging #5
Load
Balancer
Service B
WAR

Queue
Cache DB

Service A
WAR
Service C Service D
WAR WAR
Cache DB

Cache DB Cache DB
dzone.com/refcardz/getting-started-with-microservices
Advantages of
microservices
Easier to develop, understand, maintain

Starts faster than a monolith, speeds up deployments

Local change can be easily deployed, great enabler of


CD

Each service can scale on X- and Z-axis

Improves fault isolation

Eliminates any long-term commitment to a technology


stack

Freedom of choice of technology, tools, frameworks


“If you can't build a [well-structured]
monolith, what makes you think microservices
are the answer?”

http://www.codingthearchitecture.com/2014/07/06/distributed_big_balls_of_mud.html

“If your monolith is a big ball of mud,


your microservice will be a bag of dirt”
Arun Gupta
Design Principles for
Monoliths
DDD

SoC using MVC

High cohesion, low coupling

DRY

CoC

YAGNI
Web Classes Config
Pages C
Files
User C
C C
S
O
O
M
M
O
Databas
S M
Catalog M
M
M
e
O
O
J N
N
O
Order S N
Shopping Cart UI

User Order Catalog


Web Pages

Classes

Config Files

discover reg
register discover
Databas Databas Databas
e e e

Service Registry
Monolith vs
Microservice
Monolith Microservice
5 (Contracts, Order,
Archives 1
User, Catalog, Web)
Web 8 8
pages
4 (persistence.xml,
Config
web.xml, load.sql, 12(3 per archive)
Files
template.xhtml)
26 (Service
Classes 12 registration/discovery,
Application)
Archive
24 KB ~52 KB total
Size

http://blog.arungupta.me/monolithic-microservices-refactoring-ja
Design Considerations
UI and Full stack

Client-side composition (JavaScript?)

Server-side HTML generation (JSF?)

One service, one UI

REST Services

Compensating transactions instead of 2PC


NoOps
Service replication (k8s, fabric8, etcd, ZK, …)

Dependency resolution (Nexus, …)

Failover (Circuit Breaker)

Resiliency (Circuit Breaker)

Service monitoring, alerts and events (New


Relic, Log stash, …)
https://gigaom.com/2012/01/31/why-2013-is-the-year-of-noops-f
phic/
Drawbacks of
microservices
Additional complexity of distributed systems

Significant operational complexity, need


high-level of automation

Rollout plan to coordinate deployments

Slower ROI, to begin with


Microservice Premium
“don’t even
consider
microservice
s unless you
have a
system
that’s too
complex to
manage as a
monolith”

http://martinfowler.com/bliki/MicroservicePremium.html
References

github.com/arun-gupta/microservices

github.com/javaee-samples/docker-java

dzone.com/refcardz/getting-started-with-microservices

You might also like