All responses* will now be fastβ¦
*Except the very first
Slide 67
Slide 67 text
When to invalidate?
Slide 68
Slide 68 text
Targeted
All at once Time based
Tags
π₯ β³
π― π·
Slide 69
Slide 69 text
Time based expiry allows us to
keep stale caches around
Slide 70
Slide 70 text
But, no one wants to wait an
hour for edits to show up
Slide 71
Slide 71 text
What if we brought the cache
duration down to⦠one second?
Slide 72
Slide 72 text
OK β /home
(expired) GET /home
Visitors Nginx CMS
GET /home
OK β /home
GET /home
GET /home
OK β /home
(expired)
OK β /home
(expired)
GET /home
OK β /home
(expired on arrival)
πβ³
πβ³
GET /home
Slide 73
Slide 73 text
OK β /home
(expired) GET /home
Visitors Nginx CMS
GET /home
OK β /home
GET /home
GET /home
OK β /home
(expired)
OK β /home
(expired)
GET /home
OK β /home
(expired on arrival)
πβ³
πβ³
GET /home
Every request gets a fast
response, no waiting
Slide 74
Slide 74 text
OK β /home
(expired) GET /home
Visitors Nginx CMS
GET /home
OK β /home
GET /home
GET /home
OK β /home
(expired)
OK β /home
(expired)
GET /home
OK β /home
(expired on arrival)
πβ³
πβ³
GET /home
Every request gets a fast
response, no waiting
CMS handles a
fraction of the tra
ff
ic
Slide 75
Slide 75 text
This is called Microcaching
π
Slide 76
Slide 76 text
Strategy
Store
Filter Invalidate
FastCGI Cache
Slide 77
Slide 77 text
Strategy
Store
Filter Invalidate
FastCGI Cache Every Second
Slide 78
Slide 78 text
Strategy
Store
Filter Invalidate
FastCGI Cache Every Second*
*Background updates
Slide 79
Slide 79 text
Strategy
Store
Filter Invalidate
FastCGI Cache Every Second*
*Background updates
Dynamic
Static
Contextual
Private
e.g. contact form e.g. Control Panel
e.g. news article e.g. mobile / desktop variants
Slide 83
Slide 83 text
1. Static β same response for each request
Enable FastCGI Cache in the PHP
location block
e.g. news article
Slide 84
Slide 84 text
1. Static β same response for each request
Enable FastCGI Cache in the PHP
location block
e.g. news article
fastcgi_cache_valid 200 301 404 1s;
Slide 85
Slide 85 text
2. Dynamic β unique response for each request
FastCGI automatically ignores
responses with cookies
e.g. contact form
Slide 86
Slide 86 text
2. Dynamic β unique response for each request
FastCGI automatically ignores
responses with cookies
e.g. contact form
fastcgi_ignore_headers Cache-Contro
l
Expires Set-Cookie
;
fastcgi_hide_header Set-Cookie;
Slide 87
Slide 87 text
2. Dynamic β unique response for each request
Also, bypass cache by sending a
custom header from your CMS
e.g. page that shows current time, contact form
Slide 88
Slide 88 text
2. Dynamic β unique response for each request
Also, bypass cache by sending a
custom header from your CMS
e.g. page that shows current time, contact form
# eg. (in Twig
)
{% header "X-Accel-Expires: 0" %}
Slide 89
Slide 89 text
3. Contextual β di
ff
er based on request params
Add relevant request parameters to
the cache key
e.g. desktop / mobile version
Slide 90
Slide 90 text
3. Contextual β di
y
"$scheme$request_method$host$request_uri";
Slide 91
Slide 91 text
3. Contextual β di
y
"$is_mobile$scheme$request_method$host$request
Average Requests per Second
Uncached
Microcached 979.63
40.47
Slide 113
Slide 113 text
All on a $40/month
β¨
(or cheaper ) VPS
Slide 114
Slide 114 text
β’ Near instant responses
β’ Exponentially scales up capacity to
handle tra
ff
ic & spikes
β’ All* requests get fast responses
β’ Can make a cheap VPS fly
*Except the very first
Advantages
Slide 115
Slide 115 text
β’ Content may be stale
β’ Nginx tinkering, not easy to debug
Drawbacks
Slide 116
Slide 116 text
Recap
Slide 117
Slide 117 text
Microcaching in Nginx
Store
Filter Invalidate
FastCGI Cache Every Second*
*Background updates
Nginx Conf
Slide 118
Slide 118 text
β’ Great #perf booster
β’ Drastically reduces infrastructure cost
β’ No negative impact on AX
β’ No user or client complaints about stale content
β’ No JavaScript usage or dependency