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
63
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
第9回 情シス転職ミートアップ 株式会社IVRy(アイブリー)の紹介
ivry_presentationmaterials
1
190
地方に住むエンジニアの残酷な現実とキャリア論
ichimichi
2
610
Effect の双対、Coeffect
yukikurage
5
1.4k
Javaに鉄道指向プログラミング (Railway Oriented Pro gramming) のエッセンスを取り入れる/Bringing the Essence of Railway-Oriented Programming to Java
cocet33000
2
580
ktr0731/go-mcpでMCPサーバー作ってみた
takak2166
0
170
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
41
28k
Go Modules: From Basics to Beyond / Go Modulesの基本とその先へ
kuro_kurorrr
0
120
コード書くの好きな人向けAIコーディング活用tips #orestudy
77web
3
320
赤裸々に公開。 TSKaigiのオフシーズン
takezoux2
0
140
GoのWebAssembly活用パターン紹介
syumai
3
10k
F#で自在につくる静的ブログサイト - 関数型まつり2025
pizzacat83
0
310
事業戦略を理解してソフトウェアを設計する
masuda220
PRO
22
6.2k
Featured
See All Featured
Facilitating Awesome Meetings
lara
54
6.4k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
228
22k
Fireside Chat
paigeccino
37
3.5k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.8k
4 Signs Your Business is Dying
shpigford
184
22k
GraphQLの誤解/rethinking-graphql
sonatard
71
11k
The Cult of Friendly URLs
andyhume
79
6.4k
Measuring & Analyzing Core Web Vitals
bluesmoon
7
490
For a Future-Friendly Web
brad_frost
179
9.8k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
161
15k
Git: the NoSQL Database
bkeepers
PRO
430
65k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
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