RESTful Microservices

54
Shaun Abram Shaun Abram Twitter @shaunabram Blog shaunabram.com Email [email protected] LinkedIn linkedin.com/in/sabram RESTful Microservices October 3 rd , 2015

Transcript of RESTful Microservices

Page 1: RESTful Microservices

Shaun Abram

Shaun AbramTwitter @shaunabramBlog shaunabram.comEmail [email protected] linkedin.com/in/sabram

RESTful MicroservicesOctober 3rd, 2015

Page 2: RESTful Microservices

Shaun Abram 2

Startup: widgets.com

Page 3: RESTful Microservices

MonolithDeployed as a single artifact

Scaled by replicating monolith on multiple servers

Single codebase

Adapted from http://martinfowler.com/articles/microservices.html

Page 4: RESTful Microservices

Shaun Abram 4

Monoliths

Great.

Until they’re not.

Page 5: RESTful Microservices

+Tangled Dependencies…

Page 6: RESTful Microservices

Shaun Abram 6

monoliths force everyone into the same decisions

Page 7: RESTful Microservices

Shaun Abram 7

What is a microservice?

Small

Focused

Independent

Page 8: RESTful Microservices

MonolithDeployed as a single artifact

Scaled by replicating monolith on multiple servers

All functionality in a single process

MicroservicesDeployed independentlyEach functional element

as a separate serviceScaled by replicating only as needed

JavaScala

Groovy

v11 v3

v1

+ resilient

Page 9: RESTful Microservices

Shaun Abram 9

Microservices - Not a new concept!

Unix Philosophy develop small, capable software

Do one thing well Play well with other programs Use standard interfaces

Page 10: RESTful Microservices

Shaun Abram 10

Disadvantages of microservices

MonolithMicroservice

Operational complexity Deployment, monitoring & problem detection

Page 11: RESTful Microservices

Shaun Abram 11

Disadvantages of microservices

MonolithMicroservice

Refactoring across service boundaries

Page 12: RESTful Microservices

Shaun Abram 12

Disadvantages of microservices

MonolithMicroservice

Interface changes are hardMay require versioning…

Page 13: RESTful Microservices

Shaun Abram 13

Disadvantages of microservices

MonolithMicroservice

Distributed architectures are hard!

Page 14: RESTful Microservices

Shaun Abram 15

Microservices: better practices

Separate codebases Use monitoring Built in health checks Provide standard templates Security… Versioning…

Page 15: RESTful Microservices

Shaun Abram 16

Microservice Security

Secure Perimeter HTTP(S) Basic Authentication Client Certificates HMAC

And beyond…

Page 16: RESTful Microservices

Shaun Abram 17

Microservices Versioning

Backwards compatibility

Tolerant Reader and Postel's Law

Consumer-driven contracts

Semantic Versioning

Co-existing endpoints…

Page 17: RESTful Microservices

Shaun Abram 18

Microservice versioning: coexisting endpoints

Page 18: RESTful Microservices

Shaun Abram 19

Microservice versioning: coexisting endpoints

Page 19: RESTful Microservices

Shaun Abram 20

Microservice versioning: coexisting endpoints

Page 20: RESTful Microservices

Shaun Abram 21

Microservice versioning: coexisting endpoints

An example of “expand and contract”

Page 21: RESTful Microservices

Shaun Abram 22

Microservices Versioning

Backwards compatibility

Tolerant Reader and Postel's Law

Consumer-driven contracts

Semantic Versioning

Co-existing endpoints

Co-existing servers…

Page 22: RESTful Microservices

Shaun Abram 23

Microservice versioning: coexisting servers

- Duplicated code & fixes- Directing to the right service- Data versioning?

+ Blue Green Deployments

Page 23: RESTful Microservices

Shaun Abram 24

Microservices or Monoliths?

Start with monoliths; Migrate slowly

Page 24: RESTful Microservices

Shaun Abram 25

REST

Representational State Transfer

Page 25: RESTful Microservices

Shaun Abram 26

A brief History of the WWW

HTTP URI HTML

Page 26: RESTful Microservices

Shaun Abram 27

REST: Lessons learned

Roy Fielding

packaged the lessons learned Architectural Styles and the Design of Network-based Software

Architectures

REST

Page 27: RESTful Microservices

Shaun Abram 28

REST

So, what is REST

Well…Theoretical vs Practical

Page 28: RESTful Microservices

Shaun Abram 29

REST Constraints

A set of constraints. Typically with Http.

Page 29: RESTful Microservices

Shaun Abram 30

REST Constraints

A set of constraints. Typically with Http.

1. Client Server

Page 30: RESTful Microservices

Shaun Abram 31

REST Constraints

1. Client Server2. Stateless

Page 31: RESTful Microservices

Shaun Abram 32

REST Constraints

1. Client Server2. Stateless3. Cache

Page 32: RESTful Microservices

Shaun Abram 33

REST Constraints

1. Client Server2. Stateless3. Cache4. Uniform Interface

Page 33: RESTful Microservices

Shaun Abram 34

REST Constraints

1. Client Server2. Stateless3. Cache4. Uniform Interface5. Code-On-Demand

Page 34: RESTful Microservices

Shaun Abram 35

REST Constraints

1. Client Server2. Stateless3. Cache4. Uniform Interface5. Code-On-Demand6. Layered System

Page 35: RESTful Microservices

Shaun Abram 42

REST

So, what is REST, really?

Page 36: RESTful Microservices

Shaun Abram 43

REST

Resources

Uniform interfaces

Standard methods

Page 37: RESTful Microservices

Shaun Abram 44

<soap:Envelopexmlns:soap="http://www.w3.org/2001/12/soap-envelope"soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Header> <m:Trans xmlns:m="http://www.w3.com/transaction/" soap:mustUnderstand="1">234 </m:Trans></soap:Header><soap:Body xmlns:m="http://www.example.org/stock"> <m:GetStockPrice> <m:StockName>IBM</m:StockName> </m:GetStockPrice></soap:Body>

</soap:Envelope>

Page 38: RESTful Microservices

Shaun Abram 45

myservice.com/customers/33245

Page 39: RESTful Microservices

Shaun Abram 46

HTTP Request

GET myservice.com/customers/33245 HTTP/1.1Host: www.example.com

Page 40: RESTful Microservices

Shaun Abram 47

Is it “RESTful” enough?

Richardson Maturity Model

Level 0 – Http tunnelling, POX, SOAP e.g. all requests to myhospital.com

Level 1 – Resources e.g. requests to hospital.com/drs/sabram

Level 2 - HTTP Verbs GET hospital.com/drs/sabram/slots

Level 3 - Hypermedia Controls

Page 41: RESTful Microservices

Shaun Abram 48

Is it RESTful enough?

Page 42: RESTful Microservices

Shaun Abram 49

HTTP

Idempotent

Page 43: RESTful Microservices

Shaun Abram 51

HTTP Methods

Common GET DELETE PUT POST

Uncommon HEAD OPTIONS TRACE CONNECT

PATCH

Page 44: RESTful Microservices

Shaun Abram 52

Rules of thumb

POST Create with the server deciding on the URI

PUT Update a resource Store the supplied entity under the supplied

URI– If exists, update; else create with that URI

PATCH Partial updates

Use you best judgment – or your corp standards!

Page 45: RESTful Microservices

Shaun Abram 53

Uncommon HTTP Methods

HEAD Like GET but without the body Used for obtaining meta-information about the

entity Useful for testing links, e.g. for validity,

accessibility

OPTIONS Request about the capabilities of a server e.g. request a list of supported HTTP methods Possible response:

200 OK; Allow: HEAD,GET,PUT,DELETE Useful but not widely supported

Page 46: RESTful Microservices

Shaun Abram 54

Uncommon HTTP Methods

TRACE Used to invoke a remote loop-back of the

request Plain English: Echoes back request to see what

changes have been made by intermediate servers

Often disabled for security

CONNECT For use with a proxy that can dynamically

switch to being a tunnel Typically for tunneling HTTPS through HTTP

connection

Page 47: RESTful Microservices

Shaun Abram 55

HTTP response codes

Code Meaning Plain English(From user perspective)

1xx Informational; indicates a provisional response,e.g. 100

OK so far and client should continue with the request

2xx Successful All good3xx Redirection Something moved

4xx Client Error You messed up

5xx Server Error We messed up

Page 48: RESTful Microservices

Shaun Abram 56

Hypermedia as the engine of application state (HATEOAS)

What is Hypermedia?

URI and URL Hypertext Multimedia Hypermedia

Page 49: RESTful Microservices

Shaun Abram 57

Hypermedia as the engine of application state (HATEOAS)

Clients know fixed entry points to the appTransition (states) by using those links + more

If you think of Hypermedia as simply links, then HATEOAS is simply using the links you discover to navigate (or transition state) through the application.

Applies to human or software users

Page 50: RESTful Microservices

Shaun Abram 58

Hypermedia as the engine of application state (HATEOAS)

ExampleHypermedia controls used on an album listing

<album> <name>Give Blood</name> <link rel="/artist" href="/artist/theBrakes" /> <description> Awesome, short, brutish, funny and loud. Must buy! </description> <link rel="/instantpurchase" href="/instantPurchase/1234" /></album>

Page 51: RESTful Microservices

Shaun Abram 59

Wrapping up Microservices & REST

Microservices: A small, focused, loosely coupled service Can be developed, deployed, upgraded

independently

How to communicate with and between Microservices?REST & HTTP!

REST: Proven architectural style inspired by www Resources accessed via uniform interfaces and

HTTP Stateless

Page 52: RESTful Microservices

Privileged and Confidential 60

Recommended reading

Domain Driven DesignFreeman & Pryce

Building MicroservicesSam Newman

REST in PracticeWebber, Parastatidis, Robinson

Page 53: RESTful Microservices

Privileged and Confidential 61

Recommended reading

Microservices: http://martinfowler.com/articles/microservices.html

Architectural Styles and the Design of Network-based Softwarehttps://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm

Http API Design: https://github.com/interagent/http-api-design

ParallelChange (aka expand and contract): http://martinfowler.com/bliki/ParallelChange.html

Blue-Green Deployment: http://docs.pivotal.io/pivotalcf/devguide/deploy-apps/blue-green.html

Richardson Maturity Model: http://martinfowler.com/articles/richardsonMaturityModel.html

Page 54: RESTful Microservices

Shaun Abram

Twitter @shaunabramBlog shaunabram.comEmail [email protected] linkedin.com/in/sabram

RESTful Microservices

Questions?