SlideShare une entreprise Scribd logo
1  sur  41
Télécharger pour lire hors ligne
Or how to make visitor specific
content super fast.
Caching the
uncachable
I'm Daniel Rotter
@danrot90 | https://github.com/danrot
core developer and
support guru.
Passionate traveler and
soccer player.
Who knows about
HTTP Caching?
HTTP Caching Basics
A short recap
Browser
Reverse
Proxy
Symfony
Application
GET / GET /
200 OK 200 OK
Browser
GET /
200 OK
GET /
HTTP/1.1 200 OK
Cache-Control: public, max-age=3600, s-maxage=86400
<!DOCTYPE html>
<html>
<body>
Interesting content
</body>
</html>
GET /
HTTP/1.1 200 OK
Last-Modified: Sun, 4, Feb 2018 20:18:00 GMT
<!DOCTYPE html>
<html>
<body>
Quite recent Content
</body>
</html>
GET /
If-Modified-Since: Sun, 4 Feb 2018 20:18:00 GMT
HTTP/1.1 304 Not Modified
Apply more logic in the reverse proxy layer
Custom HTTP Header
200 OK
Cache-Control: max-age=240
Browser
Reverse
Proxy
Symfony
Application
GET / GET /
200 OK
Cache-Control: max-age=240
200 OK
X-Reverse-Proxy-TTL: 2400
Browser
GET /
200 OK
Cache-Control: max-age=240
200 OK
X-Reverse-Proxy-TTL: 2400
200 OK
Cache-Control: max-age=240
Browser
Reverse
Proxy
Symfony
Application
GET / GET /
Browser
GET /
PURGE /
GET /
200 OK
X-Reverse-Proxy-TTL: 2400
Caching
Implementations
Symfony Cache or Varnish?
Symfony Cache
– Easy to setup
– Implemented in PHP
– Not feature complete
// src/CacheKernel.php
<?php
namespace App;
use SymfonyBundleFrameworkBundleHttpCacheHttpCache;
class CacheKernel extends HttpCache {}
// public/index.php
<?php
use AppKernel;
use AppCacheKernel;
// ...
$kernel = new Kernel($env, $debug);
$kernel = new CacheKernel($kernel);
// ...
// src/Controller/NameController.php
<?php
namespace AppController;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
class NameController {
/**
* @Route("/name/{name}")
*/
public function index(string $name) {
$response = new Response('Your name is ' . $name);
$response->setSharedMaxAge(3600);
return $response;
}
}
– Harder to setup
– Implemented in C
– Super performant
– Many more features
– Configured using VCL
Varnish
Varnish Installation
1. Actually install varnish
2. Start varnish on port 80
3. Start your application on a different port
4. Tell varnish on which port your application is running
5. Add varnish as a trusted proxy in Symfony
6. Add cache headers to your responses
7. Configure the cache with VCL in more detail
# default.vcl
vcl 4.0;
# Default backend definition. Set this to point to your content server.
backend default {
.host = "127.0.0.1";
.port = "8080";
}
4
// public/index.php
// ...
Request::setTrustedProxies(['127.0.0.1'], Request::HEADER_X_FORWARDED_ALL);
// ...
5
// src/Controller/NameController.php
<?php
namespace AppController;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
class NameController {
/**
* @Route("/name/{name}")
*/
public function index(string $name) {
$response = new Response('Your name is ' . $name);
$response->setSharedMaxAge(3600);
return $response;
}
} 6
# default.vcl
sub vcl_recv {
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
}
sub vcl_deliver {
# Happens when we have all the pieces we need, and are about to send the
# response to the client.
#
# You can do accounting or modifying the final object here.
}
7
Audience Targeting
Show (and cache!) different content for different target
groups on the same URL
Audience Targeting
Goals
– Differentiate between different visitor target groups
– Target groups are evaluated by a ruleset
– first visit
– each browser session
– each hit
– Do not start a session on the server
– Cache the response per target group
Who knows the Vary
header?
Browser
Reverse
Proxy
Symfony
Application
GET /en
Accept-Encoding: gzip
200 OK
Vary: Accept-Encoding
GET /en
Accept-Encoding: identity
200 OK
Cache-Control: max-age=0
GET /en
Accept-Encoding: gzip
200 OK
Vary: Accept-Encoding
GET /en
Accept-Encoding: gzip
200 OK
Vary: Accept-Encoding
Browser
200 OK
Vary: Accept-Encoding
GET /en
Accept-Encoding: identity
Browser
Reverse
Proxy
Symfony
Application
GET /en
GET /_sulu_target_group
X-Sulu-Original-Url: /en
GET /en
X-Sulu-Target-Group: 1
200 OK
X-Sulu-Target-Group: 1
200 OK
Vary: X-Sulu-Target-Group
X-Reverse-Proxy-TTL: 2400
200 OK
Cache-Control: max-age=0
Set-Cookie: _svtg=1;
Set-Cookie: _svs=...;
GET /en
Cookie: _svtg=1; _svs=..
200 OK
Cache-Control: max-age=0
Reverse
Proxy
Symfony
Application
GET /about-us
Cookie: _svtg=1; _svs=..
GET /about-us
X-Sulu-Target-Group: 1
200 OK
X-Reverse-Proxy-TTL: 2400200 OK
Browser
Browser
GET /about-us
Cookie: _svtg=1; _svs=..
200 OK
Symfony Cache
Use extended version of the SymfonyCache from Sulu to support
Audience Targeting
<?php
namespace SuluComponentHttpCache;
use SymfonyBundleFrameworkBundleHttpCacheHttpCache as AbstractHttpCache;
use SymfonyComponentHttpKernelHttpKernelInterface;
class HttpCache extends AbstractHttpCache
{
public function __construct(HttpKernelInterface $kernel, $hasAudienceTargeting =
false, $cacheDir = null)
{
parent::__construct($kernel, $cacheDir);
$this->hasAudienceTargeting = $hasAudienceTargeting;
}
}
// app/WebsiteCache.php
<?php
use SuluComponentHttpCacheHttpCache;
class WebsiteCache extends HttpCache
{
}
// web/website.php
<?php
// ...
$kernel = new WebsiteKernel(SYMFONY_ENV, SYMFONY_DEBUG);
$kernel->loadClassCache();
if (SYMFONY_ENV != 'dev') {
$kernel = new WebsiteCache($kernel, true);
Request::enableHttpMethodParameterOverride();
}
// ...
Varnish
VCL to the rescue!
“I really love VCL!
Said no one,
ever.
vcl 4.0;
import header;
backend default {
.host = "127.0.0.1";
.port = "8000";
}
sub vcl_recv {
if (req.http.Cookie ~ "_svtg" && req.http.Cookie ~ "_svs") {
set req.http.X-Sulu-Target-Group = regsub(req.http.Cookie, ".*_svtg=([^;]+).*", "1");
} elseif (req.restarts == 0) {
set req.http.X-Sulu-Original-Url = req.url;
set req.http.X-Sulu-Target-Group = regsub(req.http.Cookie, ".*_svtg=([^;]+).*", "1");
set req.url = "/_sulu_target_group";
} elseif (req.restarts > 0) {
set req.url = req.http.X-Sulu-Original-Url;
unset req.http.X-Sulu-Original-Url;
}
unset req.http.Cookie;
}
sub vcl_deliver {
if (resp.http.X-Sulu-Target-Group) {
set req.http.X-Sulu-Target-Group = resp.http.X-Sulu-Target-Group;
set req.http.Set-Cookie = "_svtg=" + resp.http.X-Sulu-Target-Group + "; expires=Tue, 19 Jan 2038
03:14:07 GMT; path=/;";
return (restart);
}
if (resp.http.Vary ~ "X-Sulu-Target-Group") {
set resp.http.Cache-Control = regsub(resp.http.Cache-Control, "max-age=(d+)", "max-age=0");
set resp.http.Cache-Control = regsub(resp.http.Cache-Control, "s-maxage=(d+)", "s-maxage=0");
}
if (req.http.Set-Cookie) {
set resp.http.Set-Cookie = req.http.Set-Cookie;
header.append(resp.http.Set-Cookie, "_svs=" + now + "; path=/;");
}
}
zz
Sulu
Docs
Good ol’ Javascript
How to reevaluate
target groups on
cache hit?
Thanks for watching!
sulu.io

Contenu connexe

Tendances

Site Performance - From Pinto to Ferrari
Site Performance - From Pinto to FerrariSite Performance - From Pinto to Ferrari
Site Performance - From Pinto to Ferrari
Joseph Scott
 
Apache installation and configurations
Apache installation and configurationsApache installation and configurations
Apache installation and configurations
Nikhil Jain
 
Php psr standard 2014 01-22
Php psr standard 2014 01-22Php psr standard 2014 01-22
Php psr standard 2014 01-22
Võ Duy Tuấn
 

Tendances (20)

Apache and PHP: Why httpd.conf is your new BFF!
Apache and PHP: Why httpd.conf is your new BFF!Apache and PHP: Why httpd.conf is your new BFF!
Apache and PHP: Why httpd.conf is your new BFF!
 
Ansible Network Automation session1
Ansible Network Automation session1Ansible Network Automation session1
Ansible Network Automation session1
 
Ex407
Ex407Ex407
Ex407
 
DEF CON 27- ALBINOWAX - http desync attacks
DEF CON 27- ALBINOWAX - http desync attacksDEF CON 27- ALBINOWAX - http desync attacks
DEF CON 27- ALBINOWAX - http desync attacks
 
Apache Presentation
Apache PresentationApache Presentation
Apache Presentation
 
Http caching basics
Http caching basicsHttp caching basics
Http caching basics
 
Site Performance - From Pinto to Ferrari
Site Performance - From Pinto to FerrariSite Performance - From Pinto to Ferrari
Site Performance - From Pinto to Ferrari
 
php & performance
 php & performance php & performance
php & performance
 
HTTP Caching in Web Application
HTTP Caching in Web ApplicationHTTP Caching in Web Application
HTTP Caching in Web Application
 
Apache Web Server Setup 4
Apache Web Server Setup 4Apache Web Server Setup 4
Apache Web Server Setup 4
 
Apache installation and configurations
Apache installation and configurationsApache installation and configurations
Apache installation and configurations
 
WordPress Home Server with Raspberry Pi
WordPress Home Server with Raspberry PiWordPress Home Server with Raspberry Pi
WordPress Home Server with Raspberry Pi
 
Php psr standard 2014 01-22
Php psr standard 2014 01-22Php psr standard 2014 01-22
Php psr standard 2014 01-22
 
PHP & Performance
PHP & PerformancePHP & Performance
PHP & Performance
 
Advanced HTTP Caching
Advanced HTTP CachingAdvanced HTTP Caching
Advanced HTTP Caching
 
Apache Web Server Setup 1
Apache Web Server Setup 1Apache Web Server Setup 1
Apache Web Server Setup 1
 
VUG5: Varnish at Opera Software
VUG5: Varnish at Opera SoftwareVUG5: Varnish at Opera Software
VUG5: Varnish at Opera Software
 
How we use and deploy Varnish at Opera
How we use and deploy Varnish at OperaHow we use and deploy Varnish at Opera
How we use and deploy Varnish at Opera
 
ReplacingSquidWithATS
ReplacingSquidWithATSReplacingSquidWithATS
ReplacingSquidWithATS
 
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHPNortheast PHP - High Performance PHP
Northeast PHP - High Performance PHP
 

Similaire à Caching the Uncacheable

June8 presentation
June8 presentationJune8 presentation
June8 presentation
nicobn
 
Tips
TipsTips
Tips
mclee
 

Similaire à Caching the Uncacheable (20)

Leverage HTTP to deliver cacheable websites - Codemotion Rome 2018
Leverage HTTP to deliver cacheable websites - Codemotion Rome 2018Leverage HTTP to deliver cacheable websites - Codemotion Rome 2018
Leverage HTTP to deliver cacheable websites - Codemotion Rome 2018
 
Leverage HTTP to deliver cacheable websites - Thijs Feryn - Codemotion Rome 2018
Leverage HTTP to deliver cacheable websites - Thijs Feryn - Codemotion Rome 2018Leverage HTTP to deliver cacheable websites - Thijs Feryn - Codemotion Rome 2018
Leverage HTTP to deliver cacheable websites - Thijs Feryn - Codemotion Rome 2018
 
Developing cacheable PHP applications - PHPLimburgBE 2018
Developing cacheable PHP applications - PHPLimburgBE 2018Developing cacheable PHP applications - PHPLimburgBE 2018
Developing cacheable PHP applications - PHPLimburgBE 2018
 
Developing cacheable PHP applications - Confoo 2018
Developing cacheable PHP applications - Confoo 2018Developing cacheable PHP applications - Confoo 2018
Developing cacheable PHP applications - Confoo 2018
 
Fargate 를 이용한 ECS with VPC 1부
Fargate 를 이용한 ECS with VPC 1부Fargate 를 이용한 ECS with VPC 1부
Fargate 를 이용한 ECS with VPC 1부
 
Varnish and Drupal- Accelerating Website Performance and Flexibility with Var...
Varnish and Drupal- Accelerating Website Performance and Flexibility with Var...Varnish and Drupal- Accelerating Website Performance and Flexibility with Var...
Varnish and Drupal- Accelerating Website Performance and Flexibility with Var...
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
 
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry PiGrâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
 
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.pptLecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
 
ApacheConNA 2015: What's new in Apache httpd 2.4
ApacheConNA 2015: What's new in Apache httpd 2.4ApacheConNA 2015: What's new in Apache httpd 2.4
ApacheConNA 2015: What's new in Apache httpd 2.4
 
Microsoft Windows Server AppFabric
Microsoft Windows Server AppFabricMicrosoft Windows Server AppFabric
Microsoft Windows Server AppFabric
 
WordPress At Scale. WordCamp Dhaka 2019
WordPress At Scale. WordCamp Dhaka 2019WordPress At Scale. WordCamp Dhaka 2019
WordPress At Scale. WordCamp Dhaka 2019
 
June8 presentation
June8 presentationJune8 presentation
June8 presentation
 
Learn nginx in 90mins
Learn nginx in 90minsLearn nginx in 90mins
Learn nginx in 90mins
 
AEM (CQ) Dispatcher Security and CDN+Browser Caching
AEM (CQ) Dispatcher Security and CDN+Browser CachingAEM (CQ) Dispatcher Security and CDN+Browser Caching
AEM (CQ) Dispatcher Security and CDN+Browser Caching
 
Tips
TipsTips
Tips
 
[Devconf.cz][2017] Understanding OpenShift Security Context Constraints
[Devconf.cz][2017] Understanding OpenShift Security Context Constraints[Devconf.cz][2017] Understanding OpenShift Security Context Constraints
[Devconf.cz][2017] Understanding OpenShift Security Context Constraints
 
HTML5와 모바일
HTML5와 모바일HTML5와 모바일
HTML5와 모바일
 
Nginx + PHP
Nginx + PHPNginx + PHP
Nginx + PHP
 
Vault Secrets Via API for the REST of Us
Vault Secrets Via API for the REST of UsVault Secrets Via API for the REST of Us
Vault Secrets Via API for the REST of Us
 

Dernier

%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 

Dernier (20)

Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 

Caching the Uncacheable