Microservices
Design
Patterns
for
Java Applications
Arun Gupta
Vice President, Developer Advocacy
@arungupta, blog.arungupta.me
[email protected]
Monolith Application
UI CSS
CSS
Database
Load HTML
HTML Cache
HTML
HTML JPA
Balancer HTML
Business Logic
HTML
HTML HTML
HTML HTML
HTML
CDI REST JSF
DB
Monolith Application
EAR
EAR
Load 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 Database Version 2
HTML UI HTML
HTML Database Version 3
HTML
HTML UI HTML
HTML Database
HTML
Business Logic Version 4
HTML HTML
HTML
HTML
HTML HTML HTML
HTML HTML Logic
Business
HTML UI Database
HTML
HTML
HTML HTMLBusiness
HTML HTML Logic
HTMLHTML
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
http://akfpartners.com/techblog/2008/05/08/splitting-applications-or-services-for-scale/
An architectural approach, that emphasizes the
decomposition of applications into single-purpose,
loosely coupled services managed by cross-functional
teams, for delivering and maintaining complex software
systems with the velocity and quality required by today’s
digital business
UI
👱
Order
👱👱 🙍🙍
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.”
–Melvin Conway
http://www.melconway.com/Home/Committees_Paper.html
ic s
is t
te r
c
ara
C h
Teams around business
capability
Order User
👱👱 🙍🙍 👱👱 🙍🙍
👱 👱
Catalog
👱👱 🙍🙍
👱
ic s
is t
te r
c
hara
C
Single Responsibility Principle
1
DO
THING
ic s
is t
te r
c
hara
C
Explicitly Published interface
ic s
is t
te r
c
ara
C h
Independently replaceable
and upgradeable
ic s
is t
te r
c
hara
C
With great
power, comes great
responsibility
“you build it, you run it!”
ic s
is t
te r
c
hara
C
Designed for failure
Fault tolerance is a requirement, not a feature
http://techblog.netflix.com/2012/02/fault-tolerance-in-high-volume.html
ic s
is t
te r
c
hara
C
ic s
is t
te r
c
hara
C
100% automated
ic s
is t
te r
c
hara
C
Sync or Async Messaging
ic s
is t
te r
c
hara
C
REST vs Pub/Sub
P1
GET
C1
PUT P2
C2
POST P3
Queue C3
DELETE
P4
ic s
is t
te r
c
ara
C h
“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
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
EAR Cache
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 Service B
Aggregator Cache DB
WAR
Service C
Cache DB
WAR
Proxy Pattern #2
Load
Balancer Service A
Cache DB
WAR
Proxy Service B
Aggregator Cache DB
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
Branch Pattern #4
Load
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 Pages Classes Config Files
User C C C
S O O
S M M Database
Catalog M M
J O O
Order S N N
Shopping Cart UI
User Order Catalog
Web Pages
Classes
Config Files
discover register
register discover register
Database Database Database
Service Registry
Monolith vs Microservice
Monolith Microservice
5 (Contracts, Order, User,
Archives 1
Catalog, Web)
Web pages 8 8
4 (persistence.xml,
12
Config Files web.xml, load.sql,
(3 per archive)
template.xhtml)
26 (Service registration/
Classes 12
discovery, Application)
Archive Size 24 KB ~52 KB total
http://blog.arungupta.me/monolithic-microservices-refactoring-javaee-applications/
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-for-programmers-infographic/
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
microservices
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