Top Banner
Alessandro Nadalin | NAMSHI.com REST in peace
114

REST in peace @ IPC 2012 in Mainz

May 08, 2015

Download

Technology

Presentation given at the International PHP Conference 2012 in Mainz, about the REST architectural style.
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: REST in peace @ IPC 2012 in Mainz

Alessandro Nadalin | NAMSHI.com

REST in peace

Page 2: REST in peace @ IPC 2012 in Mainz

AGENDA

●Vol. 1: REST in a nutshell○Tenets○Antipatterns

●Vol. 2: To the rescue○HTTP cache○HATEOAS

Page 3: REST in peace @ IPC 2012 in Mainz

Sorry for the ugly slide.

There will be others.Really sorry.

Page 4: REST in peace @ IPC 2012 in Mainz

REST in a nutshell:

1. Client <> Server

Page 5: REST in peace @ IPC 2012 in Mainz

REST in a nutshell:

2. Stateless

Page 6: REST in peace @ IPC 2012 in Mainz

3. Cacheable

REST in a nutshell:

Page 7: REST in peace @ IPC 2012 in Mainz

REST in a nutshell:

4. Layered system

Page 8: REST in peace @ IPC 2012 in Mainz

REST in a nutshell:

5. Uniform interface

Page 9: REST in peace @ IPC 2012 in Mainz

And obviously nobody had a clue

Page 10: REST in peace @ IPC 2012 in Mainz

ANTIPATTERNS

Page 11: REST in peace @ IPC 2012 in Mainz

1URIs

Page 12: REST in peace @ IPC 2012 in Mainz

"REST is about

cool URI design"

http://apple.com/users/1/licenses/4.json

Page 13: REST in peace @ IPC 2012 in Mainz

"REST is about

cool URI design"

http://apple.com/users/1/licenses/4.json

Page 14: REST in peace @ IPC 2012 in Mainz

http://apple.com/site/en_US/showUsers.jsp?uid=1&license=4

is OK too

Page 15: REST in peace @ IPC 2012 in Mainz

but

Page 16: REST in peace @ IPC 2012 in Mainz

cool URIs help youthink in term of resources

David Zuelke

Page 17: REST in peace @ IPC 2012 in Mainz

2URIs (bis)

Page 18: REST in peace @ IPC 2012 in Mainz

GET /users POST /users PUT /users/{id} DELETE /users/{id} ...

REST follows a URI schema

Page 19: REST in peace @ IPC 2012 in Mainz

GET /users POST /users PUT /users/{id} DELETE /users/{id} ...

REST follows a URI schema

Page 20: REST in peace @ IPC 2012 in Mainz

what if you change yourURL?

Page 21: REST in peace @ IPC 2012 in Mainz

Yeah, client is broken

Page 22: REST in peace @ IPC 2012 in Mainz

RESTful clients shouldbe driven by service'shypermedia controls

Roy Fielding : http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven

Page 23: REST in peace @ IPC 2012 in Mainz

HATEOAS

Page 24: REST in peace @ IPC 2012 in Mainz

3POST is cool

Page 25: REST in peace @ IPC 2012 in Mainz

said SOAP 1.1

Page 26: REST in peace @ IPC 2012 in Mainz

said SOAP 1.1

Page 27: REST in peace @ IPC 2012 in Mainz

loosing meaningful verbs at the protocol level

Page 28: REST in peace @ IPC 2012 in Mainz

loosing meaningful verbs at the protocol level

nothing cacheable by default

Page 29: REST in peace @ IPC 2012 in Mainz

loosing meaningful verbs at the protocol level

nothing cacheable by default

what about bookmarking?

Page 30: REST in peace @ IPC 2012 in Mainz

4500 is your friend

Page 31: REST in peace @ IPC 2012 in Mainz

said SOAP( again )

Page 32: REST in peace @ IPC 2012 in Mainz

GET /users/300Host: www.example.com

Page 33: REST in peace @ IPC 2012 in Mainz

HTTP/1.1 500 Internal Server ErrorEtag: 1234X-Powered-By: php/5.3...

The record cannot be found

GET /users/300Host: www.example.com

Page 34: REST in peace @ IPC 2012 in Mainz

HTTP/1.1 500 Internal Server ErrorEtag: 1234X-Powered-By: php/5.3...

The record cannot be found

GET /users/300Host: www.example.com

Page 35: REST in peace @ IPC 2012 in Mainz

The HTTP protocol is awesome,and lets you return meaningful anduniversally-understood status codes

Page 36: REST in peace @ IPC 2012 in Mainz

404 Not Found

Page 37: REST in peace @ IPC 2012 in Mainz

202 Accepted

Page 38: REST in peace @ IPC 2012 in Mainz

202 Accepted

You get an immediate OK, and the operation will hopefully complete

Page 39: REST in peace @ IPC 2012 in Mainz

Pause

Page 40: REST in peace @ IPC 2012 in Mainz

World Wide Web

Page 41: REST in peace @ IPC 2012 in Mainz

the largest data-exchange network on the planet

Page 42: REST in peace @ IPC 2012 in Mainz

And meanwhile, at Facebook...

12TB of new data every day

(2 years ago)500 million users

Page 43: REST in peace @ IPC 2012 in Mainz

And meanwhile, at Google...

1 billion unique monthly googlers

Page 44: REST in peace @ IPC 2012 in Mainz

via HTTP, baby!

Page 45: REST in peace @ IPC 2012 in Mainz

HTTP in a nutshell:

1. Client <> Server

Page 46: REST in peace @ IPC 2012 in Mainz

HTTP in a nutshell:

2. Stateless

Page 47: REST in peace @ IPC 2012 in Mainz

http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html

3. widespread cache spec

HTTP in a nutshell:

Page 48: REST in peace @ IPC 2012 in Mainz

HTTP in a nutshell:

4. Layered systemOrigin server

Reverse proxy

Great chinese (fire)wall

Company proxy

Firefox

Page 49: REST in peace @ IPC 2012 in Mainz

HTTP in a nutshell:

5. it is the uniform interfacebetween clients and servers

Page 50: REST in peace @ IPC 2012 in Mainz

HTTP bleeds REST

Page 51: REST in peace @ IPC 2012 in Mainz

1. caching & scalability

Page 52: REST in peace @ IPC 2012 in Mainz

Local

Shared/proxy

Shared/reverse proxy

Types of cache

Page 53: REST in peace @ IPC 2012 in Mainz

Local

Shared/proxy

Shared/reverse proxy

Types of cache

Page 54: REST in peace @ IPC 2012 in Mainz
Page 55: REST in peace @ IPC 2012 in Mainz

Local

Shared/proxy

Shared/reverse proxy

Types of cache

Page 56: REST in peace @ IPC 2012 in Mainz
Page 57: REST in peace @ IPC 2012 in Mainz

Local

Shared/proxy

Shared/reverse proxy

Types of cache

Page 58: REST in peace @ IPC 2012 in Mainz
Page 59: REST in peace @ IPC 2012 in Mainz

on the server side

Page 60: REST in peace @ IPC 2012 in Mainz

Caching withExpiration(example)

Page 61: REST in peace @ IPC 2012 in Mainz

HTTP/1.1 200 OKHost: www.example.comExpires: 0

Page 62: REST in peace @ IPC 2012 in Mainz

HTTP/1.1 200 OKHost: www.example.comExpires: 0

Page 63: REST in peace @ IPC 2012 in Mainz

HTTP/1.1 200 OKHost: www.example.comExpires: Tue, 31 Jan 2013 01:00 GMT

Page 64: REST in peace @ IPC 2012 in Mainz

HTTP/1.1 200 OKHost: www.example.comCache-Control: max-age=60, public

Page 65: REST in peace @ IPC 2012 in Mainz

HTTP/1.1 200 OKHost: www.example.comCache-Control: max-age=60, public

Page 66: REST in peace @ IPC 2012 in Mainz

HTTP/1.1 200 OKHost: www.example.comCache-Control: max-age=60, public

Cacheable for 60 seconds

Page 67: REST in peace @ IPC 2012 in Mainz

HTTP/1.1 200 OKHost: www.example.comCache-Control: max-age=60, public

Cacheable by both local and shared caches

Page 68: REST in peace @ IPC 2012 in Mainz

but hey, you say

Page 69: REST in peace @ IPC 2012 in Mainz

HTTP's cache fails when dealing with really dynamic pages, because consumers will always have to hit the

origin server, although a part of the page would be cacheable ( header and footer, for example )

Page 70: REST in peace @ IPC 2012 in Mainz

Nope

Nope

Page 71: REST in peace @ IPC 2012 in Mainz

ESI was built for thathttp://www.w3.org/TR/esi-lang

Page 72: REST in peace @ IPC 2012 in Mainz

<esi:include src="http://php2start.com/talks/1" />

Page 73: REST in peace @ IPC 2012 in Mainz

<esi:include src="http://php2start.com/talks/1" />

Page 74: REST in peace @ IPC 2012 in Mainz

<esi:include src="http://php2start.com/talks/1" />

Page 75: REST in peace @ IPC 2012 in Mainz

1s (tweets)

3600 post

Page 76: REST in peace @ IPC 2012 in Mainz

<esi:include src='tweets.html' />

<esi:include src='article/12.html' />

Page 77: REST in peace @ IPC 2012 in Mainz

So what does HTTP cache is meant to solve?

Page 78: REST in peace @ IPC 2012 in Mainz

Less work

Page 79: REST in peace @ IPC 2012 in Mainz

because the hard work is delegated to the browser/proxy

http://www.flickr.com/photos/snakphotography/5004775320/sizes/o/in/photostream/

Page 80: REST in peace @ IPC 2012 in Mainz

evolve

Page 81: REST in peace @ IPC 2012 in Mainz

because cache is abstracted from the application

Page 82: REST in peace @ IPC 2012 in Mainz

loose coupling

Page 83: REST in peace @ IPC 2012 in Mainz

because caching is bound to the protocol, HTTP, not to your implementation ( Sf, RoR, Django )

Page 84: REST in peace @ IPC 2012 in Mainz

2. adaptability & durability

Page 85: REST in peace @ IPC 2012 in Mainz

Hypermediaanother long-time friend

Page 86: REST in peace @ IPC 2012 in Mainz

Linksoutrageously simplifying

Page 87: REST in peace @ IPC 2012 in Mainz

<link rel="payment" href="/checkout" type="text/html" ... />

Page 88: REST in peace @ IPC 2012 in Mainz

<link rel="payment" href="/checkout" type="text/html" ... />

Page 89: REST in peace @ IPC 2012 in Mainz

<link rel="payment" href="/checkout" type="text/html" ... />

Page 90: REST in peace @ IPC 2012 in Mainz

<link rel="payment" href="/checkout" type="text/html" ... />

Page 91: REST in peace @ IPC 2012 in Mainz

HTTP/1.1 201 CreatedHost: www.example.comEtag: 1234X-Powered-By: php/5.3Location: /users/1

POST /usersHost: www.example.com

Page 92: REST in peace @ IPC 2012 in Mainz

HTTP/1.1 201 CreatedHost: www.example.comEtag: 1234X-Powered-By: php/5.3Location: /users/1

POST /usersHost: www.example.com

Page 93: REST in peace @ IPC 2012 in Mainz

HTTP/1.1 201 CreatedHost: www.example.comEtag: 1234X-Powered-By: php/5.3Location: /new-users-db/1

POST /usersHost: www.example.com

Page 94: REST in peace @ IPC 2012 in Mainz

HTTP/1.1 201 CreatedHost: www.example.comEtag: 1234X-Powered-By: php/5.3Location: /new-users-db/1

POST /usersHost: www.example.com

Page 95: REST in peace @ IPC 2012 in Mainz

consumers of your API are able to followthe changes of your design

Page 96: REST in peace @ IPC 2012 in Mainz

everything seems cool

But why REST?

Page 97: REST in peace @ IPC 2012 in Mainz

Pros

Performances

Page 98: REST in peace @ IPC 2012 in Mainz

Pros

Scalability

Page 99: REST in peace @ IPC 2012 in Mainz
Page 100: REST in peace @ IPC 2012 in Mainz

Pros

Durability

Page 101: REST in peace @ IPC 2012 in Mainz

amazon.com

Page 102: REST in peace @ IPC 2012 in Mainz

Alex Nadalin

Page 103: REST in peace @ IPC 2012 in Mainz

Alex Nadalinodino.org

Page 104: REST in peace @ IPC 2012 in Mainz

Alex Nadalin

@_odino_

odino.org

Page 105: REST in peace @ IPC 2012 in Mainz

Alex Nadalin

@_odino_

odino.org

Page 106: REST in peace @ IPC 2012 in Mainz

Alex Nadalin

@_odino_

odino.org

Page 107: REST in peace @ IPC 2012 in Mainz

Alex Nadalin

@_odino_

odino.org DXB

Page 108: REST in peace @ IPC 2012 in Mainz

Alex Nadalin

@_odino_

odino.org DXB

Page 109: REST in peace @ IPC 2012 in Mainz

We're hiring!

Page 110: REST in peace @ IPC 2012 in Mainz

In Dubai.

Page 111: REST in peace @ IPC 2012 in Mainz

In Dubai.

[email protected]

Page 112: REST in peace @ IPC 2012 in Mainz

In Dubai.

[email protected]

@_odino_

Page 113: REST in peace @ IPC 2012 in Mainz

In Dubai.

[email protected]

@_odino_

TALK TO ME!

Page 114: REST in peace @ IPC 2012 in Mainz

Creditshttp://www.flickr.com/photos/larachris/16564077/sizes/o/in/photostream/

http://www.flickr.com/photos/ashatenbroeke/4367373081/sizes/z/in/photostream/http://www.flickr.com/photos/yourdon/3140270189/sizes/l/in/photostream/http://www.flickr.com/photos/jox1989/4964706072/sizes/l/in/photostream/http://www.flickr.com/photos/brainfg/168506259/sizes/o/in/photostream/

http://www.flickr.com/photos/norte_it/3897091546/sizes/o/in/photostream/http://www.zdnet.com/blog/service-oriented/soap-versus-rest-a-matter-of-style/3568

http://www.flickr.com/photos/turtlemom_nancy/2046347762/sizes/l/in/photostream/http://www.flickr.com/photos/juanpg/3333385784/sizes/z/in/photostream/http://www.flickr.com/photos/congvo/301678287/sizes/l/in/photostream/

http://www.flickr.com/photos/ihasb33r/2573196546/sizes/z/in/photostream/http://www.flickr.com/photos/martin_heigan/4544138976/sizes/o/in/photostream/

http://www.flickr.com/photos/cknara/4195099999/sizes/o/in/photostream/http://www.flickr.com/photos/1080p/3076529265/sizes/l/in/photostream/

http://www.flickr.com/photos/adamrice/280300202/sizes/l/in/photostream/http://www.flickr.com/photos/tomer_a/541411897/sizes/o/in/photostream/http://www.flickr.com/photos/subpra/4514008262/sizes/l/in/photostream/

http://www.flickr.com/photos/lippincott/2539720043/sizes/l/in/photostream/http://www.flickr.com/photos/rawryder/5086090931/sizes/l/in/photostream/http://www.flickr.com/photos/robboudon/5312731161/sizes/l/in/photostream/

http://www.flickr.com/photos/bc-burnslibrary/4158243488/sizes/o/in/photostream/http://www.flickr.com/photos/13606325@N08/2416993706/sizes/o/in/photostream/

http://www.flickr.com/photos/neothezion/5135841069/sizes/l/in/photostream/http://www.flickr.com/photos/planetschwa/2494067809/http://www.flickr.com/photos/thomasthomas/258931782/

http://www.flickr.com/photos/rustyboxcars/2629631562/sizes/l/in/photostream/http://www.flickr.com/photos/ell-r-brown/4138727474/sizes/l/in/photostream/http://www.flickr.com/photos/noah123/5082076630/sizes/z/in/photostream/http://www.flickr.com/photos/jungle_boy/220181177/sizes/l/in/photostream/

http://www.flickr.com/photos/prettydaisies/872539081/sizes/l/in/photostream/http://www.flickr.com/photos/kaptainkobold/76256150/sizes/o/in/photostream/

http://www.flickr.com/photos/uomoincravatta/1438372865/sizes/z/in/photostream/