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
60
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
オレを救った Cline を紹介する
codehex
13
13k
SwiftUI移行のためのインプレッショントラッキング基盤の構築
kokihirokawa
0
170
iOSでQRコード生成奮闘記
ktcryomm
2
120
Swift Testingのモチベを上げたい
stoticdev
2
190
Rubyと自由とAIと
yotii23
6
1.9k
Jakarta EE meets AI
ivargrimstad
0
620
Drawing Heighway’s Dragon- Recursive Function Rewrite- From Imperative Style in Pascal 64 To Functional Style in Scala 3
philipschwarz
PRO
0
150
複数のAWSアカウントから横断で 利用する Lambda Authorizer の作り方
tc3jp
0
130
1年目の私に伝えたい!テストコードを怖がらなくなるためのヒント/Tips for not being afraid of test code
push_gawa
1
650
TCAを用いたAmebaのリアーキテクチャ
dazy
0
220
やっと腹落ち「スプリント毎に動くモノをリリースする」〜ゼロから始めるメガバンクグループのアジャイル実践〜
sasakendayo
0
130
Expoによるアプリ開発の現在地とReact Server Componentsが切り開く未来
yukukotani
1
210
Featured
See All Featured
It's Worth the Effort
3n
184
28k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
Product Roadmaps are Hard
iamctodd
PRO
51
11k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Speed Design
sergeychernyshev
27
820
Java REST API Framework Comparison - PWX 2021
mraible
29
8.4k
Scaling GitHub
holman
459
140k
The Language of Interfaces
destraynor
156
24k
Typedesign – Prime Four
hannesfritz
41
2.5k
GitHub's CSS Performance
jonrohan
1030
460k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
21
2.5k
How to Think Like a Performance Engineer
csswizardry
22
1.4k
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