2009 10 06 Caching JAOO
2009 10 06 Caching JAOO
innoQ Deutschland GmbH innoQ Schweiz GmbH Halskestrae 17 Gewerbestrasse 11 D-40880 Ratingen CH-6330 Cham Phone +49 21 02 77 162-100 Phone +41 41 743 0111 [email protected] www.innoq.com
http://rest-http.info
http://soa-expertenwissen.de
identication of resources
self-descriptive messages
GET /customers/1234 Host: example.com Accept: application/vnd.mycompany.customer+xml <customer>...</customer> GET /customers/1234 Host: example.com Accept: text/x-vcard
begin:vcard ... end:vcard
Tuesday, October 6, 2009
<order self='http://example.com/orders/3321'> <amount>23</amount> <product ref='http://example.com/products/4554' /> <customer ref='http://example.com/customers/1234' /> <link rel='edit ref='http://example.com/order-edit/ACDB' /> </order>
Standard Method
GET /service/customers/1234 HTTP 1.1 Host: www.example.com User-Agent: ! XYZ 1.1 Accept: ! text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Keep-Alive:! 300 Connection:! keep-alive If-Modied-Since: ! Fri, 02 Oct 2009 16:47:31 GMT If-None-Match: ! "600028c-59fb-474f6852c9dab" Cache-Control: !max-age=60 HTTP/1.1 304 Not Modied Date: ! Sun, 04 Oct 2009 19:36:25 GMT Server:! Apache/2.2.11 (Debian) Last-Modied:! Fri, 02 Oct 2009 16:47:31 GMT Etag: ! "600028c-59fb-474f6852c9dab" Cache-Control: max-age=300 Accept-Ranges: ! bytes Vary:! Accept-Encoding Content-Encoding: ! gzip Content-Length:! 7160 Keep-Alive:! timeout=15, max=91 Connection:! Keep-Alive Content-Type: ! application/xml <?xml version=1.0 encoding=utf-8 ?> ...
Media Type
Control Data
Data
getOrderDetails() ndMatchingBid()
initiateProcess() submitApplicationData()
GET listAuctions()
getUsers()
POST
PUT
updateQuote()
Tuesday, October 6, 2009
DELETE
cancelSubscription()
interface Resource { !!!! Resource(URI u) !!!! Response get() !!!! Response post(Request r) !!!! Response put(Request r) !!!! Response delete() }
generic
Any HTTP client (Firefox, IE, curl, wget) Any HTTP server Caches Proxies Google,Yahoo!, MSN
class CustomerCollection : Resource { !!!! ... !!!! Response post(Request r) { !!!! !!!! id = createCustomer(r) !!!! !!!! return new Response(201, r) } !!!! ... }
specic
Tuesday, October 6, 2009
Caching Models
Note: Thanks to Ryan Tomayko for letting me steal base some work on his diagrams from http://tomayko.com/writings/things-caches-do
Tuesday, October 6, 2009
!"#$%&
!'()$
*'(+$%,
-./012$"(34$ -./012$"(34$ (3,$0&305$%,06'(+ 78$""3093:",70)$:$ ;<<0=> 8$""3093:", ;<<0=> 8$""3093:", !"#$%& !'()$ *'(+$%,
Expiration
!"#$%& -./012$"(34$
!'()$
*'(+$%,
-./012$"(34$ 56$""30738",5 9('(:"'&#%;04'<0&3"$8'="$0';$> ?@@0AB !'()$C!3%&83"D04'<C';$EF@@ 6$""30738",G ?@@0AB !'()$C!3%&83"D04'<C';$EF@@ 6$""30738",G !"#$%& !'()$ *'(+$%,
!"# ,-./01(2&"3(
$%&'(
!%&)(*+
.'45/+"(5/*"6/'%77(*/888 888/#(&%95(/6'(/&%&'(/45/:;(5'<
Validation
!"#$% -./012%"$34%
&'$(%
)'$*%+,
-./012%"$34% 5%+%6'7%08'"#,'7369: ;<%""30=36",; >??0@A B'97CD3,#E#%,F0/G%H0>I0@$7JJJ ./'5F0'K%LMM'E, <%""30=36", >??0@A B'97CD3,#E#%,F0/G%H0>I0@$7JJJ ./'5F0'K%LMM'E, <%""30=36", !"#$% &'$(% )'$*%+,
!"# ,-./01(2&"3(
$%&'(
!%&)(*+
,-./01(2&"3( 4567"+858(+698*&(:/.;(</=>/?&@AAA 456B"*(67%@&':/%C(DEE%5+ F(*(G%@(/H%28+%@"GIJ K?/B?./F(*(G%@(/G(IL"*I( CMD/B"@/7"+858(+ =MM/?N O%I@67"+858(+:/.;(</=>/?&@AAA -.%F:/%C(DEE%5+ P(22"/Q"G2+ !"# $%&'( !%&)(*+
Combination
!"#$% -./012%"$34%
&'$(%
)'$*%+,
-./012%"$34% 5%+%6'7%08'"#,'7369: ;<%""30=36",; >??0@A &'$(%B&3+763"C04'DB'5%EF? G'97BH3,#I#%,C0/J%K0>L0@$7MMM <%""30=36", >??0@A &'$(%B&3+763"C04'DB'5%EF? G'97BH3,#I#%,C0/J%K0>L0@$7MMM <%""30=36", !"#$% &'$(% )'$*%+,
!"# ,-./01(2&"3(
$%&'(
!%&)(*+
.'45/+"(5/*"6/'%77(*/888 888/#(&%95(/6'(/&%&'(/45/:;(5'<
!"#$% -./012(%&$3(
!"&'(
)"&*(+,
-./012(%&$3( 4567$,858(,698+&(:0/;(<0=>0?&@AAA B(+(#"@(0C"%8,"@$#DE F?0G?/0B(+(#"@(0#(DH$+D( IJK0G$@07$,858(, !"&'(6!$+@#$%:03"L6"B(MNJ =JJ0?O !"&'(6!$+@#$%:03"L6"B(MNJ P"D@67$,858(,:0/;(<0=>0?&@AAA Q(%%$0R$#%, !"#$% !"&'( )"&*(+,
Implementation
!"#$%&
!'()$
*$+,-$./$.
0#"$,-12&$3
4'(5,6%7
*.#&$,0#"$ 869,:;$"(<3$ 869,:;$"(<3$ =$'7,0#"$ =$&>.%,0#"$,!<%&$%&2 =$&>.%,=$2?<%2$ ,@,!'()#%A,)$'7$.2 =$&>.%,=$2?<%2$ !"#$%& !'()$ *$+,-$./$. 0#"$,-12&$3 4'(5,6%7
Apache FileETag
FileETag INode MTime Size
number of bytes in the le date and time the le was last modied le's i-node number
ETag Depth
!"#$%& *-.,45$"(06$
!'()$
*$%$+#(,-.'/,*$%$+'&0+
1'(2,-%3
!"#$%& 203-45$"(6+$
!'()$
*+',&-.'(/-0%1
!"#$%& 203-45$"(6+$
!'()$
*+',&-.'(/-0%1
!'()$
*+',&-.'(/-0%1
!"#$%
&$' 0-1,23&
!"()*
&"(+,-./
0-1,23& 4*56#.,4*78$.7* 4*56#.,4*78$.7* 0-1,23! 0-1,23! 4*56#.,4*78$.7* 4*56#.,4*78$.7* !"#$% &$' !"()* &"(+,-./
!"# .+/*01!
$%&'(
!%&)*+,-
!"# .+/*01!
$%&'(
!%&)*+,-
.+/*01*-"(2*,"3*'%44(,*555 555*#(&%62(*3'(*&%&'(*72*89(2':
.+/*0$ 1;;9(;%3(*1*<*$
Cache Topologies
Client
Cache
Client
Client
Cache
Server
Client
Cache
Server
Client
Cache
Client
Client only
Client
Proxy Cache
Cache
Client
Cache
Cache
Server
Client
Complex Topology
Server Client
Client
Server
Cache
Server
Client
Server
"Cache as ESB"
Tuesday, October 6, 2009
Application Layers
Presentation/UI Data Formatting Control Flow Aggregate Logic Business Rules Relations
(create, select, update, delete)
Data
REST Client
Lib
Utility Functions
Data
Application
Resource Resource Resource
Intermediaries
Squid
Full proxy cache w/ reverse proxy option Mature/stable/old, widely used Complicated conguration Support for ESI Support for external invalidation (PURGE) (Experimental) support for cache channels (much, much more)
mod_cache
mod_cache module for Apache HTTPD 2.x (production-ready in 2.2) backends: mod_mem_cache and mod_disk_cache Runs within Apache process Simple solution in conjunction with Passenger No support for ESI or explicit invalidation
<VirtualHost 1.2.3.4> ServerName example.com <Location /images> ExpiresActive On ExpiresDefault A3600 </Location> <Location /user> ExpiresActive On ExpiresDefault "access plus 1 month" </Location> CacheEnable disk /images CacheRoot /var/www/cache ProxyPass / http://localhost:3000 ProxyPassReverse / http://localhost:3000 </VirtualHost>
Varnish
Pure in-memory reverse proxy Disk storage through OS swap mechanism (Partial) ESI support External invalidation VCL conguration language Easy conguration
sub vcl_recv { if (req.request != "GET" && req.request != "HEAD" && req.request != "PUT" && req.request != "POST" && req.request != "TRACE" && req.request != "OPTIONS" && req.request != "DELETE") { /* Non-RFC2616 or CONNECT which is weird. */ return (pipe); } if (req.request != "GET" && req.request != "HEAD") { /* We only deal with GET and HEAD by default */ return (pass); } if (req.http.Authorization || req.http.Cookie) { /* Not cacheable by default */ return (pass); } return (lookup); }
VCL
Hook vcl_recv vcl_pipe vcl_pass vcl_hit vcl_miss vcl_fetch vcl_deliver vcl_timeout vcl_discard Meaning Called at the beginning of a request Called upon entering pipe mode Called upon entering pass mode Called after a cache lookup if the requested document was found in the cache Called after a cache lookup if the requested document was not found in the cache Called after a document has been successfully retrieved from the backend Called before a cached object is delivered to the client Called by the reaper thread shortly before a cached document reaches its expiry time Called by the reaper thread when a cached document is about to be discarded
Rack::Cache
Runs within Ruby process Cross-process synchronization via memcached Absurdly simple + transparent
config.middleware.use(Rack::Cache, :verbose => true, :metastore => 'file:/var/cache/rack/meta', :entitystore => 'file:/var/cache/rack/body')
Clients
So what?
I do think the REST-afarians are missing an opportunity by not driving home the secret sauce that is HTTP GET. [] GET is one of the most optimized pieces of distributed systems plumbing in the world."It's an absolute/objective slam dunk. No arguing/evangelism needed IMO. GET is the classic the rst bag is free kind of feature a platform builder dreams about.
Stefan Tilkov
http://www.innoq.com/blog/st/
Architectural Consulting SOA MDA J(2)EE WS-* MDSD RoR REST MDE .NET
Backup
R1
Server
R1 R2 Rn
Client 2
C1 R1
C1
Server
C1 R1
C2
C1 R2
C2
Rn
Client 2
R2
C2 C2
Resources
Nothingham, Mark: The State of Proxy Caching http://www.mnot.net/blog/2007/06/20/proxy_caching Nottingham, Mark: The State of Browser Caching http://www.mnot.net/blog/2006/05/11/browser_caching Nottingham, Mark: Caching Tutorial http://www.mnot.net/cache_docs/ Squid, http://www.squid-cache.org/ Nottingham, Mark: Cache Channels for Squid http://www.mnot.net/cache_channels/ The Varnish Project, http://varnish.projects.linpro.no/ Varnish: Notes from the Architect http://varnish.projects.linpro.no/wiki/ArchitectNotes Tomoyako, Ryan: Things Caches Do http://tomayko.com/writings/things-caches-do W3C, ESI Language Specication 1.0 http://www.w3.org/TR/esi-lang Apache HTTP 2.2 Caching Guide http://httpd.apache.org/docs/2.2/caching.html
Tuesday, October 6, 2009