rest in peace @ php.to.start 07-11-2011

109
PHP.TO.START, July 11 2011 - Turin REST in peace

Upload: alessandro-nadalin

Post on 08-May-2015

3.212 views

Category:

Technology


2 download

DESCRIPTION

A brief "excursus" on the REST architectural style, with antipatterns and examples based on th

TRANSCRIPT

Page 1: REST in peace @ PHP.TO.START 07-11-2011

PHP.TO.START, July 11 2011 - Turin

RESTin peace

Page 2: REST in peace @ PHP.TO.START 07-11-2011

AGENDA

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

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

PHP.TO.START, July 11 2011 - Turin

Page 3: REST in peace @ PHP.TO.START 07-11-2011

Sorry for the ugly slide.

There will be others.Really sorry.

PHP.TO.START, July 11 2011 - Turin

Page 4: REST in peace @ PHP.TO.START 07-11-2011

REST in a nutshell:

1. Client <> ServerPHP.TO.START, July 11 2011 - Turin

Page 5: REST in peace @ PHP.TO.START 07-11-2011

REST in a nutshell:

2. Stateless

PHP.TO.START, July 11 2011 - Turin

Page 6: REST in peace @ PHP.TO.START 07-11-2011

3. Cacheable

REST in a nutshell:

PHP.TO.START, July 11 2011 - Turin

Page 7: REST in peace @ PHP.TO.START 07-11-2011

REST in a nutshell:

4. Layered systemPHP.TO.START, July 11 2011 - Turin

Page 8: REST in peace @ PHP.TO.START 07-11-2011

REST in a nutshell:

5. Uniform interface

PHP.TO.START, July 11 2011 - Turin

Page 9: REST in peace @ PHP.TO.START 07-11-2011

And obviously nobody had a clue

PHP.TO.START, July 11 2011 - Turin

Page 10: REST in peace @ PHP.TO.START 07-11-2011

ANTIPATTERNS

PHP.TO.START, July 11 2011 - Turin

Page 11: REST in peace @ PHP.TO.START 07-11-2011

1URIs

PHP.TO.START, July 11 2011 - Turin

Page 12: REST in peace @ PHP.TO.START 07-11-2011

"REST is about

cool URI design"

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

PHP.TO.START, July 11 2011 - Turin

Page 13: REST in peace @ PHP.TO.START 07-11-2011

"REST is about

cool URI design"

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

PHP.TO.START, July 11 2011 - Turin

Page 14: REST in peace @ PHP.TO.START 07-11-2011

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

is OK too

PHP.TO.START, July 11 2011 - Turin

Page 15: REST in peace @ PHP.TO.START 07-11-2011

but

PHP.TO.START, July 11 2011 - Turin

Page 16: REST in peace @ PHP.TO.START 07-11-2011

cool URIs help youthink in term of resources

David Zuelke

PHP.TO.START, July 11 2011 - Turin

Page 17: REST in peace @ PHP.TO.START 07-11-2011

2URIs (bis)

PHP.TO.START, July 11 2011 - Turin

Page 18: REST in peace @ PHP.TO.START 07-11-2011

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

REST follows a URI schema

PHP.TO.START, July 11 2011 - Turin

Page 19: REST in peace @ PHP.TO.START 07-11-2011

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

REST follows a URI schema

PHP.TO.START, July 11 2011 - Turin

Page 20: REST in peace @ PHP.TO.START 07-11-2011

what if you change yourURL?

PHP.TO.START, July 11 2011 - Turin

Page 21: REST in peace @ PHP.TO.START 07-11-2011

Yeah, client is broken

PHP.TO.START, July 11 2011 - Turin

Page 22: REST in peace @ PHP.TO.START 07-11-2011

RESTful clients shouldbe driven by service'shypermedia controls

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

PHP.TO.START, July 11 2011 - Turin

Page 23: REST in peace @ PHP.TO.START 07-11-2011

HATEOAS

PHP.TO.START, July 11 2011 - Turin

Page 24: REST in peace @ PHP.TO.START 07-11-2011

3POST is cool

PHP.TO.START, July 11 2011 - Turin

Page 25: REST in peace @ PHP.TO.START 07-11-2011

said SOAP 1.1

PHP.TO.START, July 11 2011 - Turin

Page 26: REST in peace @ PHP.TO.START 07-11-2011

said SOAP 1.1

PHP.TO.START, July 11 2011 - Turin

Page 27: REST in peace @ PHP.TO.START 07-11-2011

loosing meaningful verbs at the protocol level

PHP.TO.START, July 11 2011 - Turin

Page 28: REST in peace @ PHP.TO.START 07-11-2011

loosing meaningful verbs at the protocol level

nothing cacheable by default

PHP.TO.START, July 11 2011 - Turin

Page 29: REST in peace @ PHP.TO.START 07-11-2011

loosing meaningful verbs at the protocol level

nothing cacheable by default

what about bookmarking?

PHP.TO.START, July 11 2011 - Turin

Page 30: REST in peace @ PHP.TO.START 07-11-2011

4500 is your friend

PHP.TO.START, July 11 2011 - Turin

Page 31: REST in peace @ PHP.TO.START 07-11-2011

said SOAP( again )

PHP.TO.START, July 11 2011 - Turin

Page 32: REST in peace @ PHP.TO.START 07-11-2011

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

PHP.TO.START, July 11 2011 - Turin

Page 33: REST in peace @ PHP.TO.START 07-11-2011

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

The record cannot be found

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

PHP.TO.START, July 11 2011 - Turin

Page 34: REST in peace @ PHP.TO.START 07-11-2011

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

The record cannot be found

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

PHP.TO.START, July 11 2011 - Turin

Page 35: REST in peace @ PHP.TO.START 07-11-2011

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

PHP.TO.START, July 11 2011 - Turin

Page 36: REST in peace @ PHP.TO.START 07-11-2011

404 Not Found

PHP.TO.START, July 11 2011 - Turin

Page 37: REST in peace @ PHP.TO.START 07-11-2011

404 Not Found

OMG Link is broken?

PHP.TO.START, July 11 2011 - Turin

Page 38: REST in peace @ PHP.TO.START 07-11-2011

202 Accepted

PHP.TO.START, July 11 2011 - Turin

Page 39: REST in peace @ PHP.TO.START 07-11-2011

202 Accepted

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

PHP.TO.START, July 11 2011 - Turin

Page 40: REST in peace @ PHP.TO.START 07-11-2011

Pause

PHP.TO.START, July 11 2011 - Turin

Page 41: REST in peace @ PHP.TO.START 07-11-2011

World Wide Web

PHP.TO.START, July 11 2011 - Turin

Page 42: REST in peace @ PHP.TO.START 07-11-2011

the largest data-exchange network on the planet

PHP.TO.START, July 11 2011 - Turin

Page 43: REST in peace @ PHP.TO.START 07-11-2011

And meanwhile, at Facebook...

12TB of new data every day(1 year ago)

500 million users

PHP.TO.START, July 11 2011 - Turin

Page 44: REST in peace @ PHP.TO.START 07-11-2011

And meanwhile, at Google...

1 billion unique monthly googlers

PHP.TO.START, July 11 2011 - Turin

Page 45: REST in peace @ PHP.TO.START 07-11-2011

via HTTP, baby!

PHP.TO.START, July 11 2011 - Turin

Page 46: REST in peace @ PHP.TO.START 07-11-2011

HTTP in a nutshell:

1. Client <> ServerPHP.TO.START, July 11 2011 - Turin

Page 47: REST in peace @ PHP.TO.START 07-11-2011

HTTP in a nutshell:

2. Stateless

PHP.TO.START, July 11 2011 - Turin

Page 48: REST in peace @ PHP.TO.START 07-11-2011

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

3. widespread cache spec

HTTP in a nutshell:

PHP.TO.START, July 11 2011 - Turin

Page 49: REST in peace @ PHP.TO.START 07-11-2011

HTTP in a nutshell:

4. Layered systemOrigin server

Reverse proxy

Great chinese (fire)wall

Company proxy

Lao Tze Song using Firefox 5

PHP.TO.START, July 11 2011 - Turin

Page 50: REST in peace @ PHP.TO.START 07-11-2011

HTTP in a nutshell:

5. it is the uniform interfacebetween clients and servers

PHP.TO.START, July 11 2011 - Turin

Page 51: REST in peace @ PHP.TO.START 07-11-2011

HTTP bleeds REST

PHP.TO.START, July 11 2011 - Turin

Page 52: REST in peace @ PHP.TO.START 07-11-2011

1.

caching & scalability

PHP.TO.START, July 11 2011 - Turin

Page 53: REST in peace @ PHP.TO.START 07-11-2011

Local

Shared/proxy

Shared/reverse proxy

Types of cache

PHP.TO.START, July 11 2011 - Turin

Page 54: REST in peace @ PHP.TO.START 07-11-2011

Local

Shared/proxy

Shared/reverse proxy

Types of cache

PHP.TO.START, July 11 2011 - Turin

Page 55: REST in peace @ PHP.TO.START 07-11-2011

PHP.TO.START, July 11 2011 - Turin

Page 56: REST in peace @ PHP.TO.START 07-11-2011

Local

Shared/proxy

Shared/reverse proxy

Types of cache

PHP.TO.START, July 11 2011 - Turin

Page 57: REST in peace @ PHP.TO.START 07-11-2011

PHP.TO.START, July 11 2011 - Turin

Page 58: REST in peace @ PHP.TO.START 07-11-2011

Local

Shared/proxy

Shared/reverse proxy

Types of cache

PHP.TO.START, July 11 2011 - Turin

Page 59: REST in peace @ PHP.TO.START 07-11-2011

PHP.TO.START, July 11 2011 - Turin

Page 60: REST in peace @ PHP.TO.START 07-11-2011

on the server side

PHP.TO.START, July 11 2011 - Turin

Page 61: REST in peace @ PHP.TO.START 07-11-2011

Caching withExpiration(example)

PHP.TO.START, July 11 2011 - Turin

Page 62: REST in peace @ PHP.TO.START 07-11-2011

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

PHP.TO.START, July 11 2011 - Turin

Page 63: REST in peace @ PHP.TO.START 07-11-2011

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

PHP.TO.START, July 11 2011 - Turin

Page 64: REST in peace @ PHP.TO.START 07-11-2011

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

PHP.TO.START, July 11 2011 - Turin

Page 65: REST in peace @ PHP.TO.START 07-11-2011

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

PHP.TO.START, July 11 2011 - Turin

Page 66: REST in peace @ PHP.TO.START 07-11-2011

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

PHP.TO.START, July 11 2011 - Turin

Page 67: REST in peace @ PHP.TO.START 07-11-2011

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

Cacheable for 60 seconds

PHP.TO.START, July 11 2011 - Turin

Page 68: REST in peace @ PHP.TO.START 07-11-2011

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

Cacheable by both local and shared caches

PHP.TO.START, July 11 2011 - Turin

Page 69: REST in peace @ PHP.TO.START 07-11-2011

but hey, you say

PHP.TO.START, July 11 2011 - Turin

Page 70: REST in peace @ PHP.TO.START 07-11-2011

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 )

PHP.TO.START, July 11 2011 - Turin

Page 71: REST in peace @ PHP.TO.START 07-11-2011

Nope

Nope

PHP.TO.START, July 11 2011 - Turin

Page 72: REST in peace @ PHP.TO.START 07-11-2011

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

PHP.TO.START, July 11 2011 - Turin

Page 73: REST in peace @ PHP.TO.START 07-11-2011

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

PHP.TO.START, July 11 2011 - Turin

Page 74: REST in peace @ PHP.TO.START 07-11-2011

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

PHP.TO.START, July 11 2011 - Turin

Page 75: REST in peace @ PHP.TO.START 07-11-2011

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

PHP.TO.START, July 11 2011 - Turin

Page 76: REST in peace @ PHP.TO.START 07-11-2011

1s (tweets)

3600 post

PHP.TO.START, July 11 2011 - Turin

Page 77: REST in peace @ PHP.TO.START 07-11-2011

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

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

PHP.TO.START, July 11 2011 - Turin

Page 78: REST in peace @ PHP.TO.START 07-11-2011

So what does HTTP cache is meant to solve?

PHP.TO.START, July 11 2011 - Turin

Page 79: REST in peace @ PHP.TO.START 07-11-2011

Less work

PHP.TO.START, July 11 2011 - Turin

Page 80: REST in peace @ PHP.TO.START 07-11-2011

because the hard work is delegated to the browser/proxy

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

PHP.TO.START, July 11 2011 - Turin

Page 81: REST in peace @ PHP.TO.START 07-11-2011

evolve

PHP.TO.START, July 11 2011 - Turin

Page 82: REST in peace @ PHP.TO.START 07-11-2011

because cache is abstracted from the application

PHP.TO.START, July 11 2011 - Turin

Page 83: REST in peace @ PHP.TO.START 07-11-2011

loose coupling

PHP.TO.START, July 11 2011 - Turin

Page 84: REST in peace @ PHP.TO.START 07-11-2011

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

PHP.TO.START, July 11 2011 - Turin

Page 85: REST in peace @ PHP.TO.START 07-11-2011

2.

adaptability & durability

PHP.TO.START, July 11 2011 - Turin

Page 86: REST in peace @ PHP.TO.START 07-11-2011

Hypermediaanother long-time friend

PHP.TO.START, July 11 2011 - Turin

Page 87: REST in peace @ PHP.TO.START 07-11-2011

Linksoutrageously semplifying

PHP.TO.START, July 11 2011 - Turin

Page 88: REST in peace @ PHP.TO.START 07-11-2011

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

PHP.TO.START, July 11 2011 - Turin

Page 89: REST in peace @ PHP.TO.START 07-11-2011

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

PHP.TO.START, July 11 2011 - Turin

Page 90: REST in peace @ PHP.TO.START 07-11-2011

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

PHP.TO.START, July 11 2011 - Turin

Page 91: REST in peace @ PHP.TO.START 07-11-2011

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

PHP.TO.START, July 11 2011 - Turin

Page 92: REST in peace @ PHP.TO.START 07-11-2011

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

POST /usersHost: www.example.com

PHP.TO.START, July 11 2011 - Turin

Page 93: REST in peace @ PHP.TO.START 07-11-2011

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

POST /usersHost: www.example.com

PHP.TO.START, July 11 2011 - Turin

Page 94: REST in peace @ PHP.TO.START 07-11-2011

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

POST /usersHost: www.example.com

PHP.TO.START, July 11 2011 - Turin

Page 95: REST in peace @ PHP.TO.START 07-11-2011

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

POST /usersHost: www.example.com

PHP.TO.START, July 11 2011 - Turin

Page 96: REST in peace @ PHP.TO.START 07-11-2011

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

PHP.TO.START, July 11 2011 - Turin

Page 97: REST in peace @ PHP.TO.START 07-11-2011

everything seems cool

But why REST?

PHP.TO.START, July 11 2011 - Turin

Page 98: REST in peace @ PHP.TO.START 07-11-2011

Pros

PerformancesPHP.TO.START, July 11 2011 - Turin

Page 99: REST in peace @ PHP.TO.START 07-11-2011

Pros

Scalability

PHP.TO.START, July 11 2011 - Turin

Page 100: REST in peace @ PHP.TO.START 07-11-2011
Page 101: REST in peace @ PHP.TO.START 07-11-2011

Pros

Durability

PHP.TO.START, July 11 2011 - Turin

Page 102: REST in peace @ PHP.TO.START 07-11-2011

amazon.it

PHP.TO.START, July 11 2011 - Turin

Page 103: REST in peace @ PHP.TO.START 07-11-2011

PHP.TO.START, July 11 2011 - Turin

REST-IThttps://groups.google.com/forum/#!forum/rest-it

Page 104: REST in peace @ PHP.TO.START 07-11-2011

Alessandro Nadalin

Page 105: REST in peace @ PHP.TO.START 07-11-2011

Alessandro Nadalinodino.org

Page 106: REST in peace @ PHP.TO.START 07-11-2011

Alessandro Nadalinodino.org

@_odino_

Page 107: REST in peace @ PHP.TO.START 07-11-2011

Alessandro Nadalinodino.org

@_odino_

Page 108: REST in peace @ PHP.TO.START 07-11-2011

Thanks

Alessandro Nadalinodino.org

@_odino_

Page 109: REST in peace @ PHP.TO.START 07-11-2011

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/

PHP.TO.START, July 11 2011 - Turin