Top Banner
PATTERNS & PRACTICES MICROSERVICES
74

Patterns & Practices of Microservices

Jan 10, 2017

Download

Technology

Wesley Reisz
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Patterns & Practices of Microservices

PATTERNS & PRACTICES

MICROSERVICES

Page 2: Patterns & Practices of Microservices

Proxy / Load Balancer

Web Server

Database Server

Application Server

Web Server

~4 YEARS

Page 3: Patterns & Practices of Microservices

Proxy / Load Balancer

Web Server

Database Server

Application Server

Web Server Web Server

Application Server

~3 YEARS

Page 4: Patterns & Practices of Microservices

Proxy / Load Balancer

Web Server

Database Server

Application Server

Web Server Web Server

Application Server

Proxy / Load Balancer

Web Server

Application Server

Web Server Web Server

Application Server

Hot

Cold

Database Server

~2 YEARS

Page 5: Patterns & Practices of Microservices

Proxy / Load Balancer

Web Server

Database Server

Application Server

Web Server Web Server

Application Server

Proxy / Load Balancer

Web Server

Application Server

Web Server Web Server

Application Server

Hot

Cold

GLB / DNS

Reporting Database Server

~2 YEARS

Page 6: Patterns & Practices of Microservices

Proxy / Load Balancer

Web Server

Database Server

Application Server

Web Server Web Server

Application Server

Proxy / Load Balancer

Web Server

Application Server

Web Server Web Server

Application Server

Reporting Database Server

Hot

Hot

Database Server

GLB / DNS

~2 YEARS

Page 7: Patterns & Practices of Microservices

X18

x5

~2 YEARS

Page 8: Patterns & Practices of Microservices

~2 YEARS

Page 9: Patterns & Practices of Microservices

x2

2 YEARS

Page 10: Patterns & Practices of Microservices

W E S L E Y R E I S Z

San Francisco / London / New York

CHAIR QCON

Page 11: Patterns & Practices of Microservices
Page 12: Patterns & Practices of Microservices
Page 13: Patterns & Practices of Microservices

“Loosely coupled Service Oriented Architecture with Bounded Contexts”

-Adrian Cockcroft, Microservices Thoughtleader

IF EVERY SERVICE HAS TO BE

UPDATED AT THE SAME TIME

IT’S NOT LOOSELY COUPLED

IF YOU HAVE TO KNOW TOO

MUCH ABOUT SURROUNDING

SERVICES YOU DON’T HAVE A

BOUNDED CONTEXT. SEE THE

DOMAIN DRIVEN DESIGN BOOK

BY ERIC EVANS.

Page 14: Patterns & Practices of Microservices

Why Microservices?

Page 15: Patterns & Practices of Microservices

CONS

PROS

• Monitoring/Logging becomes more important than ever.

• You trade code simplicity for operational complexity.

• Easy to chase the latest shiny technology / mix new languages & tech in.

• Another dimension to scale the application.

• Easier to have different services usedata persistence most appropriate for the use case.

• Easier to reason about moving applications to the cloud.

• Easier to have a developer understandthe portion of the system and contribute

• Easier to test & deploy a single service.

• Cascading failures can cause problems.

• Orchestration can become complex

Page 16: Patterns & Practices of Microservices

FIRST PRINCIPLES DevOps Culture,

Evolutionary Architecture, Scale Cube & CAP Theorem

MICROSERVICESPATTERNS & PRACTICES

PATTERNS & PRACTICESTechnical, Operational, Cultural

Page 17: Patterns & Practices of Microservices

DEVOPS CULTURE IS A MUST

• Rapid provisioning

• Basic monitoring

• Rapid Application Deployment

http://martinfowler.com/bliki/MicroservicePrerequisites.html

Page 18: Patterns & Practices of Microservices

“Signing up for Microservices is signing up for evolutionary architecture. There’s no point where you’re just done.” -Josh Evans, Director of Operations Engineering @Netflix

Page 19: Patterns & Practices of Microservices

Proxy / Load Balancer

Web Server

Database Server

Application Server

Web Server Web Server

Application Server

Proxy / Load Balancer

Web Server

Application Server

Web Server Web Server

Application Server

Reporting Database Server

Hot

Hot

Database Server

GLB / DNS

Page 20: Patterns & Practices of Microservices

https://www.infoq.com/presentations/netflix-failure-multiple-regions

Page 21: Patterns & Practices of Microservices

SCALE CUBE

X axis - horizontal Scaling (Scale by Cloning)

Z ax

is - D

ata P

artiti

oning

(Sca

le by

Sha

rding

)Y axis - Functional Decomposition

(Scale by Splitting)

Page 22: Patterns & Practices of Microservices

The CAP theorem is the idea that a distributed computing system is not able to provide partition tolerance, consistency and availability at

the same time.

Page 23: Patterns & Practices of Microservices

MICROSERVICESPATTERNS & PRACTICES

AVAILABILITY/PARTITION TOLERANCE

CONSISTENCY/PARTITION TOLERANCE CP: Choose Consistency over Availability when your business

requirements dictate atomic reads and writes. Wait for a response from the partitioned node which could result in a timeout error. The

system can also choose to return an error, depending on the scenario you desire.

AP: Choose Availability over Consistency when your business requirements allow for some flexibility around when the data in the

system synchronizes. Return the most recent version of the data you have, which could be stale. This system state will also accept writes that can be processed later when the partition is resolved. Availability is also

a compelling option when the system needs to continue to function in spite of external errors (shopping carts, etc.)

Page 24: Patterns & Practices of Microservices

Proxy / Load Balancer

Web Server

Database Server

Application Server

Web Server Web Server

Application Server

Proxy / Load Balancer

Web Server

Application Server

Web Server Web Server

Application Server

Reporting Database Server

Hot

Hot

Database Server

GLB / DNS

Page 25: Patterns & Practices of Microservices

Evolu&onary Architecture

Scale Cube

CAP Theorem

Microservices is a architecture that conFnues to change

and adapt

Scale Cube consists of an X, Y and Z axes – each

addressing a different approach to scale a service.

The idea that a distributed compuFng system is not able

to provide parFFon tolerance, consistency and availability

at the same Fme.

FIRST PRINCIPLESThings we need to

understand about as we plan/scale systems

DevOps is a MustMicroservices is the first architecture post devops culture

shiP

Page 26: Patterns & Practices of Microservices

TECHNICAL Tools, Event Sourcing, CQRS,

Circuit Breaker, Retrys

OPERATIONALObservability, Containers, Polyglot

CULTURALDevOps Culture, Conway’s Law

MICROSERVICESPATTERNS & PRACTICES

123

Page 27: Patterns & Practices of Microservices

TOOLS

“Everything is a tradeoff just be intentional about them”

Page 28: Patterns & Practices of Microservices

SPINE MODEL

• Needs: We are here to satisfy ________

• Values before PrinciplesWe optimize for ________

• Principles before Practices We leverage _____ to change the system

• Practices before tools We do _____ to create value

• We have always been Tool users and makers. Tools should answer the question: We use ____ to get our work done.

http://spinemodel.info/explanation/introduction/

Page 29: Patterns & Practices of Microservices

Proxy / Load Balancer

Web Server

Database Server

Application Server

Web Server Web Server

Application Server

Proxy / Load Balancer

Web Server

Application Server

Web Server Web Server

Application Server

Reporting Database Server

Hot

Hot

Database Server

GLB / DNS

Page 30: Patterns & Practices of Microservices

Hot

Hot

GLB / DNS

User Facing AvailabilityConcerns

Internal Consistency Concerns

User Facing AvailabilityConcerns

Internal Consistency Concerns

Page 31: Patterns & Practices of Microservices

Hot

Hot

GLB / DNS

High Reads

High Writes

High Reads

High Writes

Page 32: Patterns & Practices of Microservices

Hot

Hot

GLB / DNS

High Reads

High Writes

High Reads

High Writes

Page 33: Patterns & Practices of Microservices

https://www.infoq.com/presentations/wix-microservices-devops

Page 34: Patterns & Practices of Microservices

EVENT SOURCING

“Yes, I think we should talk about this again.”

Page 35: Patterns & Practices of Microservices

Student Enroll In Workshop Security Module Student Module Workshop Module

reserveSeat() isAuthorized()

isEligible()

isAvailable()

updateSchedule()

updateSchedule()

ALL SYNCHRONOUS

MUTIABLE STATE!

Page 36: Patterns & Practices of Microservices

The fundamental idea of Event Sourcing is that of ensuring every change to the state of an application is captured in an event object, and that these event objects are themselves stored in the sequence they were applied for the same lifetime as the application

state itself.- M Fowler

Page 37: Patterns & Practices of Microservices

Student Enroll In Workshop Security Module Student Module Workshop ModuleQueue/Topic

subscribe()subscribe()

subscribe()

reserveSeat() isAuthorized()

reserveSeat()

ASYNCHRONOUS

IMMUTIABLE STATE!

updateSchedule()

updateSchedule()

Par

Page 38: Patterns & Practices of Microservices

Immutable Events

Recreate the exact state

Increase in complexity

Performance / Load tes&ng

Provides a model to think about scalability

Because you have all of the state changes, you have all of

the changes that affected state

No longer doing CRUD. You have calculate the result.

Leverage things like snapshots

You an reply the events across the Enterprise

EVENT SOURCINGThe fundamental idea of Event

Sourcing is that of ensuring every change to the state of an

application is captured in an event object, and that these

event objects are themselves stored in the sequence they

were applied for the same lifetime as the application state

itself.

Page 39: Patterns & Practices of Microservices

CQRS

“Decouple queries from Commands to scale each independently.”

Page 40: Patterns & Practices of Microservices

Presentation

Business Logic

DAO Data Storage

Updates / Queries

Page 41: Patterns & Practices of Microservices

Command and Query Responsibility Segregation (CQRS) is a pattern that segregates the operations

that read data (Queries) from the operations that update data (Commands) by using separate

interfaces.

Page 42: Patterns & Practices of Microservices

Presentation

Commands

Data Persistance

Data Storage

Updates (Writes)Domain Logic

Queries (Reads)

Presentation

Page 43: Patterns & Practices of Microservices

Presentation

Commands

Data Persistance

Data Storage

Updates (Writes)Domain Logic

Queries (Reads)

Presentation

Presentation

Presentation

Page 44: Patterns & Practices of Microservices

Presentation

Commands

Data Persistance

Data Storage

Updates (Writes)

Domain Logic

Queries (Reads)

Presentation

Presentation

Presentation

Presentation

Presentation

Presentation

Read Data StoreWrite Data Store

EVENTUAL

CONSISTENCY?

Page 45: Patterns & Practices of Microservices

Domain Specific Models

Scale

Tradeoff

Performance

Clean Domain models queried

Scale the reads above the writes

Increased the operaFonal complexity of the data stores

Tune different datastore for different performance needs

CQRSCommand-Query-Responsibility-

Separation (CQRS) separates query processing from command

processing, which updates entities using business logic that's

commonly implemented using event sourcing.

SecurityUpdates to the model controlled different from the reads

Page 46: Patterns & Practices of Microservices

CIRCUIT BREAKER

“Decouple queries from Commands to scale each independently.”

Page 47: Patterns & Practices of Microservices

Service A called B.

TRIPPED

Service B is Sick, let’s flag is and

try back in 5min

Service C

Different Instance of Service B

Service B is sick. Don’t send traffic there for 5

minute (Tripped)

Page 48: Patterns & Practices of Microservices

Service A called B.

OPEN

Service B is still sick

Service C

Different Instance of Service B

Service B is still sick.

Isolate it and remove from

ELB

Page 49: Patterns & Practices of Microservices

Service responds normally.

Service C

After 5 minutes. Traffic back to service B.

Closed

set the flag to closed. Nothing

else to do.service B

responded correctly,

Page 50: Patterns & Practices of Microservices

Prevents Doomed to Fail

Three states

Prevents Cascading Failures

purpose is to detect external service failures and prevent

the applicaFon from trying to perform an acFon that is

doomed to fail.

Closed (Normal), Open(Tripped), & Half-Open state

Helps prevent cascading failures and improves resiliency.

CIRCUIT BREAKER

Circuit breaker is used to detect failures and

encapsulates logic of preventing a failure to reoccur

constantly (during maintenance, temporary external system failure or

unexpected system difficulties). Part of Hysterix

Part of Ne\lix OSS library for latency & fault tolerance

Page 51: Patterns & Practices of Microservices

RETRY

“Upstream timeout must always be longer than total downstream

timeout * retries delay”-Adrian Cockcroft

Page 52: Patterns & Practices of Microservices

Bad Service

Good Service

Edge Service

3s

1s 1s

Fails fast @ 2s

“Upstream timeout must always be longer than total downstream timeout * retries delay” -Adrian Cockcroft

Budgeted timeout, one retry

Page 53: Patterns & Practices of Microservices

Tools

Resiliency PaIerns

Architectural PaIerns to Consider

Don’t start with the tool.

Circuit Breaker, Retry

Event Sourcing & CQRS

TECHNICAL

These are some interesting technical considerations

with Microservices

Page 54: Patterns & Practices of Microservices

TECHNICAL Getting started, Event Sourcing, CQRS

OPERATIONALObservability, Containers, Polyglot

CULTURALDevOps Culture, Conway’s Law,

MICROSERVICESPATTERNS & PRACTICES

123

Page 55: Patterns & Practices of Microservices

OBSERVABILITY

In control theory, observability is a measure for how well internal states of a system can be inferred by knowledge of

its external outputs.

Page 56: Patterns & Practices of Microservices

At a minimum setup log stash, look at going an extra step to setup

ELK.

OBSERVABILITY: THINGS TO THINK ABOUT

LOGS

• Need consistent, structured logging

• Logs are not for humans

• Logstash, Elasticsearch, Kibania works great

Page 57: Patterns & Practices of Microservices

OBSERVABILITY: THINGS TO THINK ABOUT

PROTOCOLS/TRANSPORT

RPC/HTTP vs

JSON vs Binary

RPC vs HTTP

JSON vs Binary

• Speed/Performance vs Readability

• Measure serialization, transmission, deserialization costs

Page 58: Patterns & Practices of Microservices

OBSERVABILITY: THINGS TO THINK ABOUT

How do you Recreate a failure in a distributed resilient

environment?

TRACING

Look into:

• Zipkin

• Prometheus

Best way to understand Fanout

Page 59: Patterns & Practices of Microservices

https://www.infoq.com/presentations/uber-scalability-services

Page 60: Patterns & Practices of Microservices

CONTAINERS

“What do you feel is the most disruptive (or at least potentially disruptive technology)?”

Page 61: Patterns & Practices of Microservices

POLYGLOT

More challenges than they thought…

Page 62: Patterns & Practices of Microservices

Observability

Polyglot

Containers

Logging, Service CommunicaFon, Tracking

Containers makes it easier, but observability, logging,

tracking can be more challenging

Low fricFon deployment, Finer-grained execuFon

environments, speed, isolaFon

OPERATIONAL

Microservices simplifies code. It trades code

complexity for operational complexity

Page 63: Patterns & Practices of Microservices

TECHNICAL Getting started, Event Sourcing, CQRS

OPERATIONALObservability, Containers, Polyglot

CULTURALDevOps Culture, Conway’s Law

MICROSERVICESPATTERNS & PRACTICES

123

Page 64: Patterns & Practices of Microservices

CONWAY'S LAW

“Organizations which design systems ... are constrained to produce designs which are

copies of the communication structures of these organizations”

-Conway

Page 65: Patterns & Practices of Microservices

Model

One size does not fit all

Tradeoff

Organiza&onal Lifecycle Stages

Provides a model to think about scalability

Microservices (funcFon decomposiFon) is AN approach

Each approach has different tradeoffs (For example: as

we’ll find with Y-axis scaling, you may trade code

complexity for organizaFonal complexity)

Different needs at different points of an org’s lifecycle

CONWAY’S LAW

Organizations which design systems ... are constrained to produce designs which

are copies of the communication structures of

these organizations

Page 66: Patterns & Practices of Microservices

So what do I wish I knew before starting the push to Microservices?

Page 67: Patterns & Practices of Microservices

Proxy / Load Balancer

Web Server

Database Server

Application Server

Web Server Web Server

Application Server

Proxy / Load Balancer

Web Server

Application Server

Web Server Web Server

Application Server

Reporting Database Server

Hot

Hot

Database Server

GLB / DNS

Page 68: Patterns & Practices of Microservices

Document Repo

Recruiting

Processing

Boards

Applicants

Recruiting

Processing

Document Repo

Applicants

Applicants

API Gateway

API Gateway

Applicants

Boards

Page 69: Patterns & Practices of Microservices

https://www.infoq.com/presentations/netflix-failure-multiple-regions

Page 70: Patterns & Practices of Microservices

FIRST PRINCIPLES

MICROSERVICESPATTERNS & PRACTICES

• Have capabilities in your org to do things like rapidly provision, monitor, and use rapid deployment. (DevOPs Culture is a must)

• Accept that the service going to evolve (Evolutionary Architecture)

• Handle their own data needs (Consistency/Availablity Tradeoffs)

• Be able to scale based on it’s needs (Scale Cube)

Processing

DevOps Culture, Evolutionary Architecture,

Scale Cube & CAP Theorem

Page 71: Patterns & Practices of Microservices

TECHNICAL Spine Model

Event Sourcing/CQRS Circuit Breaker

Retry

OPERATIONALObservability

Containers Polyglot

CULTURALConway’s Law

MICROSERVICESPATTERNS & PRACTICES Processing

• Have a decision process(Spine Model)

• Consider using events where it makes sense and how you might scale commands & queries(Event Sourcing / CQRS)

• Protect against cascading failures(Circuit Breaker, Bulkhead)

• “Everything is a tradeoff, do it intentionally” Matt Ranney (RPC/HTTP, Containers, Polyglot)

• Bootstrap services with organizational capabilities (Logs, Tracing)

• Beware of Conway’s Law

Page 72: Patterns & Practices of Microservices

CONS

PROS

• Monitoring/Logging becomes more important than ever.

• You trade code complexity for operational complexity.

• Easy to chase the latest shiny technology / mix new languages & tech in.

• Another dimension to scale the application.

• Easier to have different services usedata persistence most appropriate for the use case.

• Easier to reason about moving applications to the cloud.

• Easier to have a developer understandthe portion of the system and contribute

• Easier to test & deploy a single service.

• Cascading failures can cause problems.

• Orchestration can become complex

Page 73: Patterns & Practices of Microservices

“If you don’t end up regretting your early technology decisions, you probably over engineered.” - Randy Shoup, Engineering Director @StitchFix

MICROSERVICESPATTERNS & PRACTICES

Page 74: Patterns & Practices of Microservices

Wesley Reisz@wesreisz

[email protected]