SlideShare a Scribd company logo
REST Services: HTTP 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
• 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?
• Up-to-date
• Fresh
• Stale
States of Resource Representation
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
Fresh State considered as long as the
Representation hasn’t expired at client side,
proxy or browser
Fresh State
• 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
• 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
• In plain English text format
• Starting line: Method URI
HTTP/version
• Headers
• Body
HTTP Message
Request:
GET /wiki HTTP/1.1
Host: wikipedia.org
Accept: text/html
Connection: close
Response:
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Language: en
Content-Length: 1234
Sample HTTP Request/Response
• Get
• Post
• Put
• Delete
• Options
• Head
• Patch
• Trace
• Link
• Unlink
• Connect
Cache-enabled methods are in Red
HTTP Methods
Response:
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 1234
Expires: Fri, 27 Jan 2012 02:33:12 GMT
•Allows only HTTP date
•Good for static resources
•Limited control
HTTP Expires header
• 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?
• public
• private
• no-cache
• no-store
• max-age=[seconds]
• s-maxage=[seconds]
• must-revalidate
• proxy-revalidate
• no-transform
HTTP Cache-Control directives
Response:
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 1234
Cache-Control: max-age=3600, must-revalidate
HTTP Cache-Control Sample
What if we need to update data?
Cache-Control is Enough for REST
• 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
/Orders/Orders ClientClient
GET /Orders HTTP/1.1
HTTP/1.1 200 OK
Etag: 1
[content goes in body]
GET /Orders HTTP/1.1
If-None-Match: 1
HTTP/1.1 304 Not Modified
[no ETag]
[no content returned!]
ETag: GET Not Modified Resource
/Orders/Orders ClientClient
GET /Orders HTTP/1.1
HTTP/1.1 200 OK
Etag: 1
[content goes in body]
GET /Orders HTTP/1.1
If-None-Match: 1
HTTP/1.1 200 OK
Etag: 2
[new content goes in body!]
ETag: GET modified resource
/Orders/Orders ClientClient
HTTP/1.1 200 OK
Etag: 1
[content goes in body]
PUT /Orders HTTP/1.1
If-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)
/Orders/Orders ClientClient
HTTP/1.1 200 OK
Etag: 1
[content goes in body]
PUT /Orders HTTP/1.1
If-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)
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
/Orders/Orders ClientClient
HTTP/1.1 412 Precondition Failed
[no ETag]
[no content!]
PUT /Orders HTTP/1.1
If-None-Match: 1
[new content goes in body]
HTTP/1.1 100 Continue
[no ETag]
[no content!]
PUT /Orders HTTP/1.1
If-Match: 1
[new content goes in body]
Etag: Override Changes When PUT
• 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 GMT
If-Modified-Since: Sat, 29 Oct 2011 19:43:31
GMT
Additional HTTP Headers
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
POST /search HTTP/1.1
Host: www.domain.com
Content-Type: application/x-www-form-
urlencoded
term=something
HTTP/1.1 303 See Other
Location: /search/something/1
POST-Redirect-GET pattern
Rest services caching
Rest services caching

More Related Content

Rest services caching

  • 2. “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
  • 3. • 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?
  • 4. • Up-to-date • Fresh • Stale States of Resource Representation
  • 5. 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
  • 6. Fresh State considered as long as the Representation hasn’t expired at client side, proxy or browser Fresh State
  • 7. • 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
  • 8. • 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
  • 9. • In plain English text format • Starting line: Method URI HTTP/version • Headers • Body HTTP Message
  • 10. Request: GET /wiki HTTP/1.1 Host: wikipedia.org Accept: text/html Connection: close Response: HTTP/1.1 200 OK Content-Type: text/html; charset=utf-8 Content-Language: en Content-Length: 1234 Sample HTTP Request/Response
  • 11. • Get • Post • Put • Delete • Options • Head • Patch • Trace • Link • Unlink • Connect Cache-enabled methods are in Red HTTP Methods
  • 12. Response: HTTP/1.1 200 OK Content-Type: text/html Content-Length: 1234 Expires: Fri, 27 Jan 2012 02:33:12 GMT •Allows only HTTP date •Good for static resources •Limited control HTTP Expires header
  • 13. • 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?
  • 14. • public • private • no-cache • no-store • max-age=[seconds] • s-maxage=[seconds] • must-revalidate • proxy-revalidate • no-transform HTTP Cache-Control directives
  • 15. Response: HTTP/1.1 200 OK Content-Type: text/html Content-Length: 1234 Cache-Control: max-age=3600, must-revalidate HTTP Cache-Control Sample
  • 16. What if we need to update data? Cache-Control is Enough for REST
  • 17. • 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
  • 18. /Orders/Orders ClientClient GET /Orders HTTP/1.1 HTTP/1.1 200 OK Etag: 1 [content goes in body] GET /Orders HTTP/1.1 If-None-Match: 1 HTTP/1.1 304 Not Modified [no ETag] [no content returned!] ETag: GET Not Modified Resource
  • 19. /Orders/Orders ClientClient GET /Orders HTTP/1.1 HTTP/1.1 200 OK Etag: 1 [content goes in body] GET /Orders HTTP/1.1 If-None-Match: 1 HTTP/1.1 200 OK Etag: 2 [new content goes in body!] ETag: GET modified resource
  • 20. /Orders/Orders ClientClient HTTP/1.1 200 OK Etag: 1 [content goes in body] PUT /Orders HTTP/1.1 If-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)
  • 21. /Orders/Orders ClientClient HTTP/1.1 200 OK Etag: 1 [content goes in body] PUT /Orders HTTP/1.1 If-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)
  • 22. 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
  • 23. /Orders/Orders ClientClient HTTP/1.1 412 Precondition Failed [no ETag] [no content!] PUT /Orders HTTP/1.1 If-None-Match: 1 [new content goes in body] HTTP/1.1 100 Continue [no ETag] [no content!] PUT /Orders HTTP/1.1 If-Match: 1 [new content goes in body] Etag: Override Changes When PUT
  • 24. • 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 GMT If-Modified-Since: Sat, 29 Oct 2011 19:43:31 GMT Additional HTTP Headers
  • 25. 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
  • 26. POST /search HTTP/1.1 Host: www.domain.com Content-Type: application/x-www-form- urlencoded term=something HTTP/1.1 303 See Other Location: /search/something/1 POST-Redirect-GET pattern