SlideShare a Scribd company logo
Going crazy
with Symfony
and Varnish
David de Boer15 May 2015
Varnish
Public content
Most requested
Most processing
Static assets
Authenticated content?
$ varnishtop -i BeReqUrl
Leiden
Driebit, Amsterdam
FOSHttpCache 

FOSHttpCacheBundle
Break up
only one TTL
don’t cache
15 mins
1 hour
24 hours
Going crazy with Varnish and Symfony
Enable ESI
sub vcl_recv {
set req.http.Surrogate-Capability = "abc=ESI/1.0";
}
sub vcl_backend_response {
if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
unset beresp.http.Surrogate-Control;
set beresp.do_esi = true;
}
}
Plain PHP
<?php
header('Surrogate-Control: ESI/1.0');
?>
<html>
<body>
<esi:include src="/blue.php" />
Your main content.
<esi:include src="/red.php" />
</body>
</html>
<?php
// blue.php
header('Cache-Control: no-cache');
?>
Blue is not cached.
<?php
// red.php
header('Cache-Control: s-maxage=3600');
?>
Red is cached for an hour.
ESI in Symfony
# app/config/config.yml
framework:
esi:
enabled: true
fragments: { path: /_fragment }
{# template.twig #}
{{ render_esi(controller('AppBundle:Red:list', {'max': 100})) }}
ESI performance
<?php
// blue.php
sleep(2);
header('Cache-Control: no-cache');
?>
Blue is kind of sleepy.
<?php
// red.php
sleep(5);
header('Cache-Control: no-cache');
?>
And red even more so.
7 seconds
Going crazy with Varnish and Symfony
Client-side
Rich JavaScript apps
JSON data through Ajax
API design
ESI in JSON
varnishd -p esi_syntax=0x00000003
{
"id": 1,
"name": "Martian Ambassador",
"_embedded": {
"category": <esi:include src="/categories/1"/>
}
}
Invalidation
<?php
use FOSHttpCacheProxyClientVarnish;
use FOSHttpCacheCacheInvalidator;
$varnishClient = new Varnish('127.0.0.1', 'http://mars-attacks.dev');
$cacheInvalidator = new CacheInvalidator($varnishClient);
// If some data changes in your app:
$cacheInvalidator->invalidatePath('/red.php')->flush();
Invalidation
# app/config/config.yml
fos_http_cache:
proxy_client:
varnish:
servers: 127.0.0.1
base_url: mars-attacks.dev
<?php
use FOSHttpCacheBundleConfigurationInvalidateRoute;
/**
* @InvalidateRoute("red")
*/
public function editAction($id)
{
// This is where data changes
}
Going crazy with Varnish and Symfony
Individual caching?
// builtin.vcl
sub vcl_recv {
// ...
if (req.http.Authorization || req.http.Cookie) {
/* Not cacheable by default */
return (pass);
}
// ...
}
Individual caching?
sub vcl_recv {
// ...
if (req.http.Authorization || req.http.Cookie) {
/* Cache all the personal things! */
return (hash);
}
// ...
}
Going crazy with Varnish and Symfony
Individual caching?
sub vcl_recv {
// ...
if (req.http.Authorization || req.http.Cookie) {
/* Cache all the personal things! */
return (hash);
}
// ...
}
sub vcl_deliver {
// ...
set resp.http.Vary = "Cookie,Authorization";
}
Group together
User context
Varnish
hash lookup
vary on credentials
#
content request
vary on hash header
#
App
⟳
Varnish
User context
hash lookup
vary on credentials
#
content request
vary on hash header
#
⟳
Varnish
User context
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
# # #
Configure user context
sub vcl_recv {
if (req.restarts == 0
&& (req.http.cookie || req.http.authorization)
&& (req.method == "GET" || req.method == "HEAD")
) {
if (req.http.accept) {
set req.http.X-Fos-Original-Accept = req.http.accept;
}
set req.http.accept = "application/vnd.fos.user-context-hash";
set req.http.X-Fos-Original-Url = req.url;
set req.url = "/_fos_user_context_hash";
return (hash);
}
if (req.restarts > 0
&& req.http.accept == "application/vnd.fos.user-context-hash"
) {
set req.url = req.http.X-Fos-Original-Url;
unset req.http.X-Fos-Original-Url;
if (req.http.X-Fos-Original-Accept) {
set req.http.accept = req.http.X-Fos-Original-Accept;
unset req.http.X-Fos-Original-Accept;
} else {
unset req.http.accept;
}
return (hash);
}
}
Configure user context
sub vcl_backend_response {
if (bereq.http.accept ~ "application/vnd.fos.user-context-hash"
&& beresp.status >= 500
) {
return (abandon);
}
}
sub vcl_deliver {
if (req.restarts == 0
&& resp.http.content-type ~ "application/vnd.fos.user-context-hash"
) {
set req.http.X-User-Context-Hash = resp.http.X-User-Context-Hash;
return (restart);
}
set resp.http.Vary = regsub(resp.http.Vary, "(?i),? *X-User-Context-Hash *", "");
set resp.http.Vary = regsub(resp.http.Vary, "^, *", "");
if (resp.http.Vary == "") {
unset resp.http.Vary;
}
unset resp.http.X-User-Context-Hash;
}
Cleaning cookies
$ varnishlog -c -i ReqHeader -I Cookie
Cookie: __uvt=;PHPSESSID=ht8rl2tnbikpeoau7q5g677125;_ga=GA1.2.1332615739.14 

31368832;uvts=320PnWhzN5bW7V2D
sub vcl_recv {
set req.http.cookie = ";" + req.http.cookie;
set req.http.cookie = regsuball(req.http.cookie, "; +", ";");
set req.http.cookie = regsuball(req.http.cookie, ";(PHPSESSID)=", "; 1=");
set req.http.cookie = regsuball(req.http.cookie, ";[^ ][^;]*", "");
set req.http.cookie = regsuball(req.http.cookie, "^[; ]+|[; ]+$", "");
}
Cookie: PHPSESSID=ht8rl2tnbikpeoau7q5g677125
FOSHttpCacheBundle
# app/config/config.yml

fos_http_cache:

user_context:

hash_cache_ttl: 3600
hash_header: "Custom-User-Hash-Header"
role_provider: true
Custom provider
<?php
use FOSHttpCacheUserContextContextProviderInterface;
use FOSHttpCacheUserContextUserContext;
class IsAwesomeProvider implements ContextProviderInterface
{
private $userService;
public function __construct(YourUserService $userService)
{
$this->userService = $userService;
}
public function updateUserContext(UserContext $userContext)
{
$userContext->addParameter('awesome', $this->userService->isAwesome());
}
}
//
<service id="app_bundle.provider" class="AppBundleProviderIsAwesomeProvider">
<tag name="fos_http_cache.user_context_provider" />
</service>
Thanks!
david@driebit.nl
@ddeboer_nl
FOSHttpCache

FOSHttpCacheBundle
https://joind.in/14546

More Related Content

PDF
HTTP caching with Varnish
PPTX
Making Symofny shine with Varnish - SymfonyCon Madrid 2014
PDF
Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin...
PPTX
Solving anything in VCL
PDF
Tips for going fast in a slow world: Michael May at OSCON 2015
PPTX
Mitigating Security Threats with Fastly - Joe Williams at Fastly Altitude 2015
PDF
Advanced VCL: how to use restart
KEY
Puppet for dummies - ZendCon 2011 Edition
HTTP caching with Varnish
Making Symofny shine with Varnish - SymfonyCon Madrid 2014
Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin...
Solving anything in VCL
Tips for going fast in a slow world: Michael May at OSCON 2015
Mitigating Security Threats with Fastly - Joe Williams at Fastly Altitude 2015
Advanced VCL: how to use restart
Puppet for dummies - ZendCon 2011 Edition

What's hot (20)

PDF
Beyond Breakpoints: A Tour of Dynamic Analysis
PDF
Altitude SF 2017: Debugging Fastly VCL 101
PPTX
Varnish Cache and its usage in the real world!
PPTX
PyCon Russia 2014 - Auto Scale in the Cloud
KEY
modern module development - Ken Barber 2012 Edinburgh Puppet Camp
PDF
Chef Provisioning a Chef Server Cluster - ChefConf 2015
PPTX
So I Wrote a Manifest
PPTX
WordPress + NGINX Best Practices with EasyEngine
PPT
Tips for a Faster Website
PDF
Gearman and Perl
PDF
Designing net-aws-glacier
PDF
Squid for Load-Balancing & Cache-Proxy ~ A techXpress Guide
PDF
Building a better web
PDF
Zero Downtime Deployment with Ansible
PDF
Selenium sandwich-3: Being where you aren't.
PDF
Automating the Network
PDF
Managing Your Cisco Datacenter Network with Ansible
PDF
Mitchell Hashimoto, HashiCorp
PDF
Ansible new paradigms for orchestration
PDF
Common configuration with Data Bags - Fundamentals Webinar Series Part 4
Beyond Breakpoints: A Tour of Dynamic Analysis
Altitude SF 2017: Debugging Fastly VCL 101
Varnish Cache and its usage in the real world!
PyCon Russia 2014 - Auto Scale in the Cloud
modern module development - Ken Barber 2012 Edinburgh Puppet Camp
Chef Provisioning a Chef Server Cluster - ChefConf 2015
So I Wrote a Manifest
WordPress + NGINX Best Practices with EasyEngine
Tips for a Faster Website
Gearman and Perl
Designing net-aws-glacier
Squid for Load-Balancing & Cache-Proxy ~ A techXpress Guide
Building a better web
Zero Downtime Deployment with Ansible
Selenium sandwich-3: Being where you aren't.
Automating the Network
Managing Your Cisco Datacenter Network with Ansible
Mitchell Hashimoto, HashiCorp
Ansible new paradigms for orchestration
Common configuration with Data Bags - Fundamentals Webinar Series Part 4
Ad

Similar to Going crazy with Varnish and Symfony (20)

PPTX
June8 presentation
PDF
Optimizing Varnish for Magento: Advanced Techniques for Performance and Scala...
PDF
Varnish Enterprise - when you need the full power of caching
PDF
Lightning fast with Varnish
PDF
Velocity EU 2014 — Offline-first web apps
PDF
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...
PDF
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...
PDF
Caching the uncacheable with Varnish - DevDays 2021
PDF
Running PHP on a Java container
PDF
Leverage HTTP to deliver cacheable websites - Codemotion Rome 2018
PDF
Leverage HTTP to deliver cacheable websites - Thijs Feryn - Codemotion Rome 2018
PDF
Apache and PHP: Why httpd.conf is your new BFF!
PDF
HTML5: huh, what is it good for?
PDF
HTTP Caching and PHP
PDF
VUG5: Varnish at Opera Software
PPTX
Varnish
PDF
Varnish
PDF
Developing cacheable PHP applications - Confoo 2018
PDF
ApacheConNA 2015: What's new in Apache httpd 2.4
June8 presentation
Optimizing Varnish for Magento: Advanced Techniques for Performance and Scala...
Varnish Enterprise - when you need the full power of caching
Lightning fast with Varnish
Velocity EU 2014 — Offline-first web apps
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...
Caching the uncacheable with Varnish - DevDays 2021
Running PHP on a Java container
Leverage HTTP to deliver cacheable websites - Codemotion Rome 2018
Leverage HTTP to deliver cacheable websites - Thijs Feryn - Codemotion Rome 2018
Apache and PHP: Why httpd.conf is your new BFF!
HTML5: huh, what is it good for?
HTTP Caching and PHP
VUG5: Varnish at Opera Software
Varnish
Varnish
Developing cacheable PHP applications - Confoo 2018
ApacheConNA 2015: What's new in Apache httpd 2.4
Ad

Recently uploaded (20)

PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Online Work Permit System for Fast Permit Processing
PDF
Digital Strategies for Manufacturing Companies
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
ai tools demonstartion for schools and inter college
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPT
Introduction Database Management System for Course Database
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
L1 - Introduction to python Backend.pptx
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
top salesforce developer skills in 2025.pdf
ISO 45001 Occupational Health and Safety Management System
CHAPTER 2 - PM Management and IT Context
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Online Work Permit System for Fast Permit Processing
Digital Strategies for Manufacturing Companies
How to Migrate SBCGlobal Email to Yahoo Easily
PTS Company Brochure 2025 (1).pdf.......
Design an Analysis of Algorithms II-SECS-1021-03
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
ai tools demonstartion for schools and inter college
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Navsoft: AI-Powered Business Solutions & Custom Software Development
Introduction Database Management System for Course Database
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
L1 - Introduction to python Backend.pptx
Upgrade and Innovation Strategies for SAP ERP Customers
2025 Textile ERP Trends: SAP, Odoo & Oracle
top salesforce developer skills in 2025.pdf

Going crazy with Varnish and Symfony