Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cache Me If You Can

Cache Me If You Can

Lightning Talk at JSConf Japan 2019

Maxi Ferreira

November 30, 2019
Tweet

Other Decks in Programming

Transcript

  1. Maxi Ferreira JS Engineer @ Help Scout JSConf Japan !

    November 30, 2019 Cache Me If You Can @charca
  2. –Phil Karlton “There are only two hard things in Computer

    Science: cache invalidation and naming things.”
  3. Cache-Control Directives no-store no-cache max-age s-maxage immutable no-transform public private

    must-revalidate proxy-revalidate stale-while-revalidate stale-if-error
  4. Cache-Control Directives no-store no-cache max-age s-maxage immutable no-transform public private

    must-revalidate proxy-revalidate stale-while-revalidate stale-if-error
  5. Cache-Control: no-store ✅ Guarantees the resource is up to date

    ❌ Downloads the full resource on every request
  6. –Phil Karlton “There are only two hard things in Computer

    Science: cache invalidation and naming things.”
  7. GET /main.js 304 Not Modified Cache-Control: no-cache ETag: abc123 main.js

    no-cache abc123 GET /main.js If-None-Match: abc123 Cache-Control: no-cache
  8. GET /main.js 304 Not Modified Cache-Control: no-cache ETag: abc123 main.js

    no-cache abc123 GET /main.js If-None-Match: abc123 Cache-Control: no-cache
  9. Cache-Control: no-cache ✅ Guarantees the resource is up to date

    ✅ Doesn’t download the resource unnecessarily
  10. Cache-Control: no-cache ✅ Guarantees the resource is up to date

    ✅ Doesn’t download the resource unnecessarily ❌ Has to check with the server on every request
  11. GET /main.js 304 Not Modified Cache-Control: max-age=100 ETag: abc123 GET

    /main.js If-None-Match: abc123 Cache-Control: max-age main.js max-age=100 age=120 abc123
  12. GET /main.js 304 Not Modified Cache-Control: max-age=100 ETag: abc123 GET

    /main.js If-None-Match: abc123 Cache-Control: max-age main.js max-age=100 age=0 abc123
  13. GET /main.js 304 Not Modified Cache-Control: max-age=100 ETag: abc123 GET

    /main.js If-None-Match: abc123 Cache-Control: max-age main.js max-age=100 age=0 abc123
  14. Cache-Control: max-age ✅ Uses the resource directly from the cache

    ✅ Doesn’t need to check with the server
  15. Cache-Control: max-age ✅ Uses the resource directly from the cache

    ✅ Doesn’t need to check with the server How do we guarantee that the resource is up to date?
  16. Cache-Control: max-age ✅ Uses the resource directly from the cache

    ✅ Doesn’t need to check with the server How do we guarantee that the resource is up to date? max-age=???
  17. GET /main.def.js 200 OK Cache-Control: max-age=31536000 ETag: def456 Cache-Control: max-age

    main.def.js max-age= 31536000 age=0 def456 main.abc.js max-age= 31536000 age=7200 abc123
  18. GET /main.def.js 200 OK Cache-Control: max-age=31536000 ETag: def456 Cache-Control: max-age

    main.def.js max-age= 31536000 age=0 def456 main.abc.js max-age= 31536000 age=7200 abc123
  19. Cache-Control: max-age=31536000 + Revision Stamping ✅ Uses the resource directly

    from the cache ✅ Doesn’t need to check with the server
  20. Cache-Control: max-age=31536000 + Revision Stamping ✅ Uses the resource directly

    from the cache ✅ Doesn’t need to check with the server ✅ Guarantees the resource is up to date
  21. Final Notes Use the cache to prevent roundtrips to the

    server. Whenever possible, use immutable resources with caches that don’t expire
  22. Final Notes Use the cache to prevent roundtrips to the

    server. Whenever possible, use immutable resources with caches that don’t expire Check the docs of your CDNs
  23. Final Notes Use the cache to prevent roundtrips to the

    server. Whenever possible, use immutable resources with caches that don’t expire Check the docs of your CDNs If you need more flexibility, use a Service Worker