Rest services caching

28
REST Services: HTTP caching

description

REST Services Caching

Transcript of Rest services caching

Page 1: Rest services caching

REST Services: HTTP caching

Page 2: Rest services caching

“A distributed system is one in which the failure of a computer you didn’t even know existed can render your own computer unusable.”

- Leslie Lamport

Page 3: Rest services caching

• REST stands for REpresentational State Transfer

• Representation is just a very simple rendering of the object

• Has benefits when leverages existing HTTP protocol

What is REST?

Page 4: Rest services caching

•Up-to-date• Fresh• Stale

States of Resource Representation

Page 5: Rest services caching

Up-to-Date state means the Representation is current according to the origin server

Representation is being sent back with no expiry and no caching instructions means the client is allowed to cache for however long or not cache at all

Up-to-Date State

Page 6: Rest services caching

Fresh State considered as long as the Representation hasn’t expired at client side, proxy or browser

Fresh State

Page 7: Rest services caching

• Sending must-revalidate means the client must revalidate once the representation is Stale

• Stale means it’s gone over it’s expiration date

Stale State

Page 8: Rest services caching

• Doesn’t actually mean do not cache

• Means the client must revalidate both Fresh and Stale entries and then may use the cached copy if it’s up to date

No-cache Directive

Page 9: Rest services caching

• In plain English text format

• Starting line: Method URI HTTP/version

• Headers

• Body

HTTP Message

Page 10: Rest services caching

Request:GET /wiki HTTP/1.1Host: wikipedia.orgAccept: text/htmlConnection: close

Response:HTTP/1.1 200 OKContent-Type: text/html; charset=utf-8Content-Language: enContent-Length: 1234

Sample HTTP Request/Response

Page 11: Rest services caching

• Get• Post• Put• Delete• Options

• Head• Patch• Trace• Link• Unlink• Connect

Cache-enabled methods are in Red

HTTP Methods

Page 12: Rest services caching

Response:HTTP/1.1 200 OKContent-Type: text/htmlContent-Length: 1234Expires: Fri, 27 Jan 2012 02:33:12 GMT

•Allows only HTTP date•Good for static resources•Limited control

HTTP Expires header

Page 13: Rest services caching

• Enough for static content• For things that rarely change• Or don’t change at all like Calculator

/Calculator/Multiply?x=2&y=2/Calculator/Multiply?x=2&y=2 ClientClient

Expires is Enough?

Page 14: Rest services caching

• public• private• no-cache• no-store• max-age=[seconds]• s-maxage=[seconds]• must-revalidate• proxy-revalidate• no-transform

HTTP Cache-Control directives

Page 15: Rest services caching

Response:HTTP/1.1 200 OKContent-Type: text/htmlContent-Length: 1234Cache-Control: max-age=3600, must-revalidate

HTTP Cache-Control Sample

Page 16: Rest services caching

What if we need to update data?

Cache-Control is Enough for REST

Page 17: Rest services caching

• Optimistic is when we version our data• And do not have any transactions• Make use of ETag, If-Match and If-None-

Match headers

Optimistic Locking: ETag

Page 18: Rest services caching

/Orders/Orders ClientClientGET /Orders HTTP/1.1

HTTP/1.1 200 OKEtag: 1[content goes in body]

GET /Orders HTTP/1.1If-None-Match: 1

HTTP/1.1 304 Not Modified[no ETag][no content returned!]

ETag: GET Not Modified Resource

Page 19: Rest services caching

/Orders/Orders ClientClientGET /Orders HTTP/1.1

HTTP/1.1 200 OKEtag: 1[content goes in body]

GET /Orders HTTP/1.1If-None-Match: 1

HTTP/1.1 200 OKEtag: 2[new content goes in body!]

ETag: GET modified resource

Page 20: Rest services caching

/Orders/Orders ClientClient

HTTP/1.1 200 OKEtag: 1[content goes in body]

PUT /Orders HTTP/1.1If-Match: 1[new content goes in body]

HTTP/1.1 100 Continue[no ETag][no content!]

GET /Orders HTTP/1.1

ETag: Modify Resource (PUT)

Page 21: Rest services caching

/Orders/Orders ClientClient

HTTP/1.1 200 OKEtag: 1[content goes in body]

PUT /Orders HTTP/1.1If-Match: 1[new content goes in body]

HTTP/1.1 412 Precondition Failed[no ETag][no content!]

GET /Orders HTTP/1.1

ETag: Modify Changed Resource (PUT)

Page 22: Rest services caching

God damned HTTP!What we gonna do?

• Override changes!• Yeah! It’s possible!

• Download the latest version using GET method• We will know the new Etag then

Etag: mismatch when PUT

Page 23: Rest services caching

/Orders/Orders ClientClient

HTTP/1.1 412 Precondition Failed[no ETag][no content!]

PUT /Orders HTTP/1.1If-None-Match: 1[new content goes in body]

HTTP/1.1 100 Continue[no ETag][no content!]

PUT /Orders HTTP/1.1If-Match: 1[new content goes in body]

Etag: Override Changes When PUT

Page 24: Rest services caching

• ETag and If-* headers came with HTTP 1.1

• In HTTP 1.0 there are another set of headers:Last-Modified: Sat, 29 Oct 2011 19:43:31 GMTIf-Modified-Since: Sat, 29 Oct 2011 19:43:31

GMT

Additional HTTP Headers

Page 25: Rest services caching

int Multiply(int x, int y)int Multiply(int x, int y)

HTTPHTTP

IPC (Named Pipes)IPC (Named Pipes)

TCPTCP

ClientClient

TCPTCP

SOAPSOAP

UDPUDP

Web Services: Protocols

Page 26: Rest services caching

POST /search HTTP/1.1Host: www.domain.comContent-Type: application/x-www-form-urlencodedterm=something

HTTP/1.1 303 See OtherLocation: /search/something/1

POST-Redirect-GET pattern

Page 27: Rest services caching
Page 28: Rest services caching