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
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
ヤプリ新卒SREの オンボーディング
masaki12
0
130
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.1k
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.4k
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
170
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
340
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
130
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
870
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
1
100
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
600
Featured
See All Featured
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
720
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Designing for Performance
lara
604
68k
Six Lessons from altMBA
skipperchong
27
3.5k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
YesSQL, Process and Tooling at Scale
rocio
169
14k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
The Pragmatic Product Professional
lauravandoore
31
6.3k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
KATA
mclloyd
29
14k
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