Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Cache Me If You Can
Search
Maxi Ferreira
November 30, 2019
Programming
1
56
Cache Me If You Can
Lightning Talk at JSConf Japan 2019
Maxi Ferreira
November 30, 2019
Tweet
Share
Other Decks in Programming
See All in Programming
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
350
OpenTelemetryでRailsのパフォーマンス分析を始めてみよう(KoR2024)
ymtdzzz
4
1.6k
Sidekiqで実現する 長時間非同期処理の中断と再開 / Pausing and Resuming Long-Running Asynchronous Jobs with Sidekiq
hypermkt
6
2.7k
[PyCon Korea 2024 Keynote] 커뮤니티와 파이썬, 그리고 우리
beomi
0
110
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
23k
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
160
From Subtype Polymorphism To Typeclass-based Ad hoc Polymorphism- An Example
philipschwarz
PRO
0
170
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
560
生成 AI を活用した toitta 切片分類機能の裏側 / Inside toitta's AI-Based Factoid Clustering
pokutuna
0
580
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
440
Golang と Erlang
taiyow
8
1.9k
Amazon Neptuneで始めてみるグラフDB-OpenSearchによるグラフの全文検索-
satoshi256kbyte
4
330
Featured
See All Featured
A Tale of Four Properties
chriscoyier
156
23k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
A Modern Web Designer's Workflow
chriscoyier
692
190k
GraphQLの誤解/rethinking-graphql
sonatard
66
10k
How GitHub (no longer) Works
holman
311
140k
Building Adaptive Systems
keathley
38
2.2k
Fontdeck: Realign not Redesign
paulrobertlloyd
81
5.2k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Building Better People: How to give real-time feedback that sticks.
wjessup
363
19k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
107
49k
Measuring & Analyzing Core Web Vitals
bluesmoon
1
41
Transcript
Maxi Ferreira JS Engineer @ Help Scout JSConf Japan !
November 30, 2019 Cache Me If You Can @charca
Cache is King
–Phil Karlton “There are only two hard things in Computer
Science: cache invalidation and naming things.”
None
Service Worker Cache
Service Worker Cache HTTP Cache
Service Worker Cache HTTP Cache Proxy Cache
Service Worker Cache HTTP Cache Proxy Cache CDN Cache
HTTP Cache & Cache-Control
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
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
Cache-Control: no-store
Cache-Control: no-store “Don’t Cache”
Cache-Control: no-store
GET /main.js Cache-Control: no-store
GET /main.js 200 OK Cache-Control: no-store Cache-Control: no-store
GET /main.js 200 OK Cache-Control: no-store Cache-Control: no-store
Cache-Control: no-store
Cache-Control: no-store ✅ Guarantees the resource is up to date
Cache-Control: no-store ✅ Guarantees the resource is up to date
❌ Downloads the full resource on every request
Cache-Control: no-cache
Cache-Control: no-cache “Don’t Cache”
Cache-Control: no-cache “Don’t Cache” “Yes Cache, but revalidate with the
server before using”
–Phil Karlton “There are only two hard things in Computer
Science: cache invalidation and naming things.”
Cache-Control: no-cache
GET /main.js Cache-Control: no-cache
GET /main.js 200 OK Cache-Control: no-cache ETag: abc123 Cache-Control: no-cache
GET /main.js 200 OK Cache-Control: no-cache ETag: abc123 main.js no-cache
abc123 Cache-Control: no-cache
GET /main.js 200 OK Cache-Control: no-cache ETag: abc123 main.js no-cache
abc123 Cache-Control: no-cache
main.js no-cache abc123 Cache-Control: no-cache
GET /main.js main.js no-cache abc123 Cache-Control: no-cache
GET /main.js main.js no-cache abc123 GET /main.js If-None-Match: abc123 Cache-Control:
no-cache
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
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
Cache-Control: no-cache
Cache-Control: no-cache ✅ Guarantees the resource is up to date
Cache-Control: no-cache ✅ Guarantees the resource is up to date
✅ Doesn’t download the resource unnecessarily
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
Cache-Control: max-age={x}
Cache-Control: max-age={x} “Keep this resource fresh in the cache for
{x} seconds.”
Fresh <-> Stale
Cache-Control: max-age
GET /main.js Cache-Control: max-age
GET /main.js 200 OK Cache-Control: max-age=100 ETag: abc123 Cache-Control: max-age
GET /main.js 200 OK Cache-Control: max-age=100 ETag: abc123 main.js max-age=100
age=0 abc123 Cache-Control: max-age
GET /main.js 200 OK Cache-Control: max-age=100 ETag: abc123 main.js max-age=100
age=0 abc123 Cache-Control: max-age
main.js max-age=100 age=20 abc123 Cache-Control: max-age
GET /main.js main.js max-age=100 age=20 abc123 Cache-Control: max-age
GET /main.js main.js max-age=100 age=20 abc123 Cache-Control: max-age
Cache-Control: max-age main.js max-age=100 age=120 abc123
GET /main.js Cache-Control: max-age main.js max-age=100 age=120 abc123
GET /main.js GET /main.js If-None-Match: abc123 Cache-Control: max-age main.js max-age=100
age=120 abc123
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
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
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
Cache-Control: max-age
Cache-Control: max-age ✅ Uses the resource directly from the cache
Cache-Control: max-age ✅ Uses the resource directly from the cache
✅ Doesn’t need to check with the server
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?
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=???
max-age=0
max-age=600
max-age=until-it-changes
Immutable Resources + Aggressive Caching
max-age=31536000
Revision Stamping main.js vendors.js styles.css
Revision Stamping main.js vendors.js styles.css
Revision Stamping main.js vendors.js styles.css main.ee8f28.js vendors.ab2f18.js styles.f74ee2.css
Cache-Control: max-age
GET /main.abc.js Cache-Control: max-age
GET /main.abc.js 200 OK Cache-Control: max-age=31536000 ETag: abc123 Cache-Control: max-age
GET /main.abc.js 200 OK Cache-Control: max-age=31536000 ETag: abc123 main.abc.js max-age=
31536000 age=0 abc123 Cache-Control: max-age
GET /main.abc.js 200 OK Cache-Control: max-age=31536000 ETag: abc123 main.abc.js max-age=
31536000 age=0 abc123 Cache-Control: max-age
main.abc.js max-age= 31536000 age=3600 abc123 Cache-Control: max-age
GET /main.abc.js main.abc.js max-age= 31536000 age=3600 abc123 Cache-Control: max-age
GET /main.abc.js main.abc.js max-age= 31536000 age=3600 abc123 Cache-Control: max-age
Cache-Control: max-age main.abc.js max-age= 31536000 age=7200 abc123
GET /main.def.js Cache-Control: max-age main.abc.js max-age= 31536000 age=7200 abc123
GET /main.def.js 200 OK Cache-Control: max-age=31536000 ETag: def456 Cache-Control: max-age
main.abc.js max-age= 31536000 age=7200 abc123
GET /main.def.js 200 OK Cache-Control: max-age=31536000 ETag: def456 Cache-Control: max-age
main.abc.js max-age= 31536000 age=7200 abc123
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
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
Cache-Control: max-age=31536000 + Revision Stamping
Cache-Control: max-age=31536000 + Revision Stamping ✅ Uses the resource directly
from the cache
Cache-Control: max-age=31536000 + Revision Stamping ✅ Uses the resource directly
from the cache ✅ Doesn’t need to check with the server
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
Final Notes
Final Notes Use the cache to prevent roundtrips to the
server.
Final Notes Use the cache to prevent roundtrips to the
server. Whenever possible, use immutable resources with caches that don’t expire
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
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
Thank you