SlideShare une entreprise Scribd logo
1  sur  29
Varnish
The 9 circles of hell
By

04 Dec 2013
Who am i
●
●

●

Luis Ferro
Lead Developer in Rewards and Loyalty
Programs (Metropolis International)
When not coding, you can find me shooting
arrows or cooking, mostly...

http://www.linkedin.com/profile/view?id=73429631
Varnish before Hell Entrance
●

“Varnish is an HTTP accelerator designed for
content-heavy dynamic web sites. Varnish is
focused exclusively on HTTP.” - wikipedia

●

https://www.varnish-cache.org

●

Installing (debian/ubuntu)
sudo apt-get update
sudo apt-get install varnish
st

1 Circle - basics
●

Simple caching

●

Varnish, NGinX and PHP should be working

●

Default configurations apart from the ports
which should be:
Port 80 – Varnish, with backend to 8080
Port 8080 – NGinX, with backend to 9000
Port 9000 – PHP
st

1 – Backend in default.vcl
backend default {
.host = "127.0.0.1";
.port = "8080";
.connect_timeout = 30s;
.first_byte_timeout = 30s;
.between_bytes_timeout = 30s;
}
Check configuration with:
varnishd -C -f default.vcl
st

1 - Checking it in - NGinX
NGinX Test
ab -n 10000 -c 10 http://127.0.0.1:8080/index.php

Requests per second:

253.01 [#/sec] (mean)

Time per request:

39.524 [ms] (mean)

Time per request:

3.952 [ms] (mean, across all concurrent requests)

Transfer rate:

29931.75 [Kbytes/sec] received
st

1 - Checking it in - Varnish
Varnish Test
ab -n 10000 -c 10 http://127.0.0.1:80/index.php

Requests per second:

2624.59 [#/sec] (mean)

Time per request:

3.810 [ms] (mean)

Time per request:

0.381 [ms] (mean, across all concurrent requests)

Transfer rate:

311418.96 [Kbytes/sec] received

10x FASTER
With almost default config
nd

2 Circle – The path to cache
●

Add hit / miss information to all requests
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
set resp.http.X-Cache-Hits = obj.hits;
} else {
set resp.http.X-Cache = "MISS";
}
return (deliver);
}
nd

2 - Check
rd

3 Circle – Force march
●

Force Refresh
sub vcl_recv { [...]
if (req.http.Cache-Control ~ "no-cache") {
ban_url(req.url);
}
[…] }

●

You may want to protect / restrict who can call
this “ban”
rd

3 - Check
rd

4 Circle – Expired content
●

Add php headers to force content to expire after
a certain quantity of seconds
function headers_for_page_cache($cache_length=600){
$cache_expire_date = gmdate("D, d M Y H:i:s", time() + $cache_length);
header("Expires: $cache_expire_date");
header("Pragma: cache");
header("Cache-Control: max-age=$cache_length, s-maxage=$cache_length");
header("User-Cache-Control: max-age=$cache_length");
}
th

4 Circle - Check
th

5 Circle – All in the bag
●

Ensure cache by delete cookies on unwanted
items (images, css, js)
sub vcl_recv { […]
if (req.url ~ ".(png|gif|jpg)$") {
remove req.http.Cookie;
}
[...]}

●

Varnish doesn't cache anything with a cookie!
th

5 - Check
th

6 Circle – Watch your steps
●

Install GeoIp

●

Get GeoipUsingInlineC from:
https://www.varnish-cache.org/trac/wiki/GeoipUsingInlineC

●

Compile and install it (findable by your OS)
th

6 - Geoip.vcl
C{
#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>
static const char* (*get_country_code)(char* ip) = NULL;
__attribute__((constructor)) void
load_module()
{
const char* symbol_name = "get_country_code";
const char* plugin_name = "/usr/lib/geoip_plugin.so";
void* handle = NULL;
handle = dlopen( plugin_name, RTLD_NOW );
if (handle != NULL) {
get_country_code = dlsym( handle, symbol_name );
if (get_country_code == NULL)
fprintf( stderr, "nError: Could not load GeoIP plugin:n%snn", dlerror() );
else
printf( "GeoIP plugin loaded successfully.n");
}
else
fprintf( stderr, "nError: Could not load GeoIP plugin:n%snn", dlerror() );
}
}C
th

6 - Add it to default.vcl
sub vcl_recv { […]
C{
VRT_SetHdr(sp, HDR_REQ, "017X-Country-Code:",
(*get_country_code)( VRT_IP_string(sp, VRT_r_client_ip(sp)) ),
vrt_magic_string_end);
}C
[…] }
th

6 - Check
th

7 Circle – Know your enemies
●

Device Detection

●

Download the devicedetection.vcl from:
http://github.com/varnish/varnish-devicedetect/

●

Add it to default.vcl
sub vcl_recv { […]
call detectdevice;
[…] }
th

7 - Check
th

8 Circle – Tongue in cheek
●

Accepted language

●

Download and create the accept-language.vcl from:
http://github.com/cosimo/varnish-accept-language

●

Include the generated file and call it in default.vcl
sub vcl_recv { […]
C{
vcl_rewrite_accept_language(sp);
}C
[…] }
th

8 - Check
th

9 Circle – Divide and conquer
●

To activate add in your default.vcl
sub vcl_fetch { [...]
if( beresp.http.esi-enabled == "1") {
set beresp.do_esi = true; /* Do ESI processing */
set beresp.ttl = 120s; /* Sets the TTL on the HTML
above */
unset beresp.http.esi-enabled;
} [...]
th

9 - Check

Remember, ESI has issues with non-xml like files. So, the file where you
place the <esi: instructions have to look like a xml file.
More to come... if there was time...
●

Redirects

●

ESI – Sessions / Users

●

Purge

●

SSL / SPDY / Compression

●

Load Balancing

●

Webserver (O.o really?)
Varnish Resources
●

The Book
https://www.varnish-software.com/static/book/

●

Cache Tutorial
http://www.mnot.net/cache_docs/

●

Slides (slides used on the talk)
Slides will be made available later on slideshare

●

Sourcecode (configs, test scripts, etc)
GitHub: https://github.com/lferro9000/varnish-talk
Thank you for listening and...
●

●

●

… Specially to varnish-software and everyone that
somehow contributed directly to enrich varnish.
Always check varnish-software website in case of doubt,
there is an huge amount of little tricks there.
Remember: using varnish, is good for the environment
(less power = win)!
Credits and Thanks
●

Botticelli Chart of Dante's Hell
http://www.flickr.com/photos/distan/3708997567/
by Dina Dubrovskaya (Real Distan)

●

The great people that made Varnish possible

●

PHP London for accepting this talk

Contenu connexe

Tendances

Varnish http accelerator
Varnish http acceleratorVarnish http accelerator
Varnish http acceleratorno no
 
PHP Project development with Vagrant
PHP Project development with VagrantPHP Project development with Vagrant
PHP Project development with VagrantBahattin Çiniç
 
Tribal Nova Docker workshop
Tribal Nova Docker workshopTribal Nova Docker workshop
Tribal Nova Docker workshopNicolas Degardin
 
Caching with Varnish
Caching with VarnishCaching with Varnish
Caching with Varnishschoefmax
 
New Jersey Red Hat Users Group Presentation: Provisioning anywhere
New Jersey Red Hat Users Group Presentation: Provisioning anywhereNew Jersey Red Hat Users Group Presentation: Provisioning anywhere
New Jersey Red Hat Users Group Presentation: Provisioning anywhereRodrique Heron
 
Coredns nodecache - A highly-available Node-cache DNS server
Coredns nodecache - A highly-available Node-cache DNS serverCoredns nodecache - A highly-available Node-cache DNS server
Coredns nodecache - A highly-available Node-cache DNS serverYann Hamon
 
Nodejs - Should Ruby Developers Care?
Nodejs - Should Ruby Developers Care?Nodejs - Should Ruby Developers Care?
Nodejs - Should Ruby Developers Care?Felix Geisendörfer
 
EWD 3 Training Course Part 33: Configuring QEWD to use CORS
EWD 3 Training Course Part 33: Configuring QEWD to use CORSEWD 3 Training Course Part 33: Configuring QEWD to use CORS
EWD 3 Training Course Part 33: Configuring QEWD to use CORSRob Tweed
 
Redis fundamental
Redis fundamentalRedis fundamental
Redis fundamentalYuhao Zhang
 
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 OperaCosimo Streppone
 
Solaris 11 base box for Vagrant using Packer
Solaris 11 base box for Vagrant using PackerSolaris 11 base box for Vagrant using Packer
Solaris 11 base box for Vagrant using PackerAlan Chalmers
 
Gr8conf EU 2018 - Bring you infrastructure under control with Infrastructor
Gr8conf EU 2018 - Bring you infrastructure under control with InfrastructorGr8conf EU 2018 - Bring you infrastructure under control with Infrastructor
Gr8conf EU 2018 - Bring you infrastructure under control with InfrastructorStanislav Tiurikov
 
Node.js - A practical introduction (v2)
Node.js  - A practical introduction (v2)Node.js  - A practical introduction (v2)
Node.js - A practical introduction (v2)Felix Geisendörfer
 
[Hello world 오픈세미나]varnish로 웹서버성능 향상시키기
[Hello world 오픈세미나]varnish로 웹서버성능 향상시키기[Hello world 오픈세미나]varnish로 웹서버성능 향상시키기
[Hello world 오픈세미나]varnish로 웹서버성능 향상시키기NAVER D2
 

Tendances (20)

Varnish http accelerator
Varnish http acceleratorVarnish http accelerator
Varnish http accelerator
 
Varnish
VarnishVarnish
Varnish
 
PHP Project development with Vagrant
PHP Project development with VagrantPHP Project development with Vagrant
PHP Project development with Vagrant
 
Tribal Nova Docker workshop
Tribal Nova Docker workshopTribal Nova Docker workshop
Tribal Nova Docker workshop
 
ReplacingSquidWithATS
ReplacingSquidWithATSReplacingSquidWithATS
ReplacingSquidWithATS
 
Caching with Varnish
Caching with VarnishCaching with Varnish
Caching with Varnish
 
New Jersey Red Hat Users Group Presentation: Provisioning anywhere
New Jersey Red Hat Users Group Presentation: Provisioning anywhereNew Jersey Red Hat Users Group Presentation: Provisioning anywhere
New Jersey Red Hat Users Group Presentation: Provisioning anywhere
 
Coredns nodecache - A highly-available Node-cache DNS server
Coredns nodecache - A highly-available Node-cache DNS serverCoredns nodecache - A highly-available Node-cache DNS server
Coredns nodecache - A highly-available Node-cache DNS server
 
Nodejs - Should Ruby Developers Care?
Nodejs - Should Ruby Developers Care?Nodejs - Should Ruby Developers Care?
Nodejs - Should Ruby Developers Care?
 
Nodejs - A-quick-tour-v3
Nodejs - A-quick-tour-v3Nodejs - A-quick-tour-v3
Nodejs - A-quick-tour-v3
 
EWD 3 Training Course Part 33: Configuring QEWD to use CORS
EWD 3 Training Course Part 33: Configuring QEWD to use CORSEWD 3 Training Course Part 33: Configuring QEWD to use CORS
EWD 3 Training Course Part 33: Configuring QEWD to use CORS
 
Node.js - As a networking tool
Node.js - As a networking toolNode.js - As a networking tool
Node.js - As a networking tool
 
Redis fundamental
Redis fundamentalRedis fundamental
Redis fundamental
 
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
 
Solaris 11 base box for Vagrant using Packer
Solaris 11 base box for Vagrant using PackerSolaris 11 base box for Vagrant using Packer
Solaris 11 base box for Vagrant using Packer
 
Gr8conf EU 2018 - Bring you infrastructure under control with Infrastructor
Gr8conf EU 2018 - Bring you infrastructure under control with InfrastructorGr8conf EU 2018 - Bring you infrastructure under control with Infrastructor
Gr8conf EU 2018 - Bring you infrastructure under control with Infrastructor
 
Node.js - A practical introduction (v2)
Node.js  - A practical introduction (v2)Node.js  - A practical introduction (v2)
Node.js - A practical introduction (v2)
 
[Hello world 오픈세미나]varnish로 웹서버성능 향상시키기
[Hello world 오픈세미나]varnish로 웹서버성능 향상시키기[Hello world 오픈세미나]varnish로 웹서버성능 향상시키기
[Hello world 오픈세미나]varnish로 웹서버성능 향상시키기
 
Nodejs - A quick tour (v4)
Nodejs - A quick tour (v4)Nodejs - A quick tour (v4)
Nodejs - A quick tour (v4)
 
Nodejs - A quick tour (v5)
Nodejs - A quick tour (v5)Nodejs - A quick tour (v5)
Nodejs - A quick tour (v5)
 

Similaire à PHP London Dec 2013 - Varnish - The 9 circles of hell

Challenges when building high profile editorial sites
Challenges when building high profile editorial sitesChallenges when building high profile editorial sites
Challenges when building high profile editorial sitesYann Malet
 
[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 ConstraintsAlessandro Arrichiello
 
Running Docker in Development & Production (#ndcoslo 2015)
Running Docker in Development & Production (#ndcoslo 2015)Running Docker in Development & Production (#ndcoslo 2015)
Running Docker in Development & Production (#ndcoslo 2015)Ben Hall
 
VUG5: Varnish at Opera Software
VUG5: Varnish at Opera SoftwareVUG5: Varnish at Opera Software
VUG5: Varnish at Opera SoftwareCosimo Streppone
 
Developing with-devstack
Developing with-devstackDeveloping with-devstack
Developing with-devstackDeepak Garg
 
Less and faster – Cache tips for WordPress developers
Less and faster – Cache tips for WordPress developersLess and faster – Cache tips for WordPress developers
Less and faster – Cache tips for WordPress developersSeravo
 
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 scalabilityWim Godden
 
Postgres the hardway
Postgres the hardwayPostgres the hardway
Postgres the hardwayDave Pitts
 
Automating complex infrastructures with Puppet
Automating complex infrastructures with PuppetAutomating complex infrastructures with Puppet
Automating complex infrastructures with PuppetKris Buytaert
 
Salesforce at Stacki Atlanta Meetup February 2016
Salesforce at Stacki Atlanta Meetup February 2016Salesforce at Stacki Atlanta Meetup February 2016
Salesforce at Stacki Atlanta Meetup February 2016StackIQ
 
Docker Security workshop slides
Docker Security workshop slidesDocker Security workshop slides
Docker Security workshop slidesDocker, Inc.
 
2017-03-11 02 Денис Нелюбин. Docker & Ansible - лучшие друзья DevOps
2017-03-11 02 Денис Нелюбин. Docker & Ansible - лучшие друзья DevOps2017-03-11 02 Денис Нелюбин. Docker & Ansible - лучшие друзья DevOps
2017-03-11 02 Денис Нелюбин. Docker & Ansible - лучшие друзья DevOpsОмские ИТ-субботники
 
X64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 newX64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 newYiwei Ma
 
Virtualization and automation of library software/machines + Puppet
Virtualization and automation of library software/machines + PuppetVirtualization and automation of library software/machines + Puppet
Virtualization and automation of library software/machines + PuppetOmar Reygaert
 
Hands on Docker - Launch your own LEMP or LAMP stack - SunshinePHP
Hands on Docker - Launch your own LEMP or LAMP stack - SunshinePHPHands on Docker - Launch your own LEMP or LAMP stack - SunshinePHP
Hands on Docker - Launch your own LEMP or LAMP stack - SunshinePHPDana Luther
 
Eclipse Pdt2.0 26.05.2009
Eclipse Pdt2.0 26.05.2009Eclipse Pdt2.0 26.05.2009
Eclipse Pdt2.0 26.05.2009Bastian Feder
 
DPDK in Containers Hands-on Lab
DPDK in Containers Hands-on LabDPDK in Containers Hands-on Lab
DPDK in Containers Hands-on LabMichelle Holley
 
Automating Complex Setups with Puppet
Automating Complex Setups with PuppetAutomating Complex Setups with Puppet
Automating Complex Setups with PuppetKris Buytaert
 

Similaire à PHP London Dec 2013 - Varnish - The 9 circles of hell (20)

Challenges when building high profile editorial sites
Challenges when building high profile editorial sitesChallenges when building high profile editorial sites
Challenges when building high profile editorial sites
 
[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
 
Running Docker in Development & Production (#ndcoslo 2015)
Running Docker in Development & Production (#ndcoslo 2015)Running Docker in Development & Production (#ndcoslo 2015)
Running Docker in Development & Production (#ndcoslo 2015)
 
VUG5: Varnish at Opera Software
VUG5: Varnish at Opera SoftwareVUG5: Varnish at Opera Software
VUG5: Varnish at Opera Software
 
Developing with-devstack
Developing with-devstackDeveloping with-devstack
Developing with-devstack
 
Less and faster – Cache tips for WordPress developers
Less and faster – Cache tips for WordPress developersLess and faster – Cache tips for WordPress developers
Less and faster – Cache tips for WordPress developers
 
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
 
Postgres the hardway
Postgres the hardwayPostgres the hardway
Postgres the hardway
 
Automating complex infrastructures with Puppet
Automating complex infrastructures with PuppetAutomating complex infrastructures with Puppet
Automating complex infrastructures with Puppet
 
Docker, c'est bonheur !
Docker, c'est bonheur !Docker, c'est bonheur !
Docker, c'est bonheur !
 
Pecl Picks
Pecl PicksPecl Picks
Pecl Picks
 
Salesforce at Stacki Atlanta Meetup February 2016
Salesforce at Stacki Atlanta Meetup February 2016Salesforce at Stacki Atlanta Meetup February 2016
Salesforce at Stacki Atlanta Meetup February 2016
 
Docker Security workshop slides
Docker Security workshop slidesDocker Security workshop slides
Docker Security workshop slides
 
2017-03-11 02 Денис Нелюбин. Docker & Ansible - лучшие друзья DevOps
2017-03-11 02 Денис Нелюбин. Docker & Ansible - лучшие друзья DevOps2017-03-11 02 Денис Нелюбин. Docker & Ansible - лучшие друзья DevOps
2017-03-11 02 Денис Нелюбин. Docker & Ansible - лучшие друзья DevOps
 
X64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 newX64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 new
 
Virtualization and automation of library software/machines + Puppet
Virtualization and automation of library software/machines + PuppetVirtualization and automation of library software/machines + Puppet
Virtualization and automation of library software/machines + Puppet
 
Hands on Docker - Launch your own LEMP or LAMP stack - SunshinePHP
Hands on Docker - Launch your own LEMP or LAMP stack - SunshinePHPHands on Docker - Launch your own LEMP or LAMP stack - SunshinePHP
Hands on Docker - Launch your own LEMP or LAMP stack - SunshinePHP
 
Eclipse Pdt2.0 26.05.2009
Eclipse Pdt2.0 26.05.2009Eclipse Pdt2.0 26.05.2009
Eclipse Pdt2.0 26.05.2009
 
DPDK in Containers Hands-on Lab
DPDK in Containers Hands-on LabDPDK in Containers Hands-on Lab
DPDK in Containers Hands-on Lab
 
Automating Complex Setups with Puppet
Automating Complex Setups with PuppetAutomating Complex Setups with Puppet
Automating Complex Setups with Puppet
 

Dernier

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 

Dernier (20)

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 

PHP London Dec 2013 - Varnish - The 9 circles of hell

  • 1. Varnish The 9 circles of hell By 04 Dec 2013
  • 2. Who am i ● ● ● Luis Ferro Lead Developer in Rewards and Loyalty Programs (Metropolis International) When not coding, you can find me shooting arrows or cooking, mostly... http://www.linkedin.com/profile/view?id=73429631
  • 3. Varnish before Hell Entrance ● “Varnish is an HTTP accelerator designed for content-heavy dynamic web sites. Varnish is focused exclusively on HTTP.” - wikipedia ● https://www.varnish-cache.org ● Installing (debian/ubuntu) sudo apt-get update sudo apt-get install varnish
  • 4. st 1 Circle - basics ● Simple caching ● Varnish, NGinX and PHP should be working ● Default configurations apart from the ports which should be: Port 80 – Varnish, with backend to 8080 Port 8080 – NGinX, with backend to 9000 Port 9000 – PHP
  • 5. st 1 – Backend in default.vcl backend default { .host = "127.0.0.1"; .port = "8080"; .connect_timeout = 30s; .first_byte_timeout = 30s; .between_bytes_timeout = 30s; } Check configuration with: varnishd -C -f default.vcl
  • 6. st 1 - Checking it in - NGinX NGinX Test ab -n 10000 -c 10 http://127.0.0.1:8080/index.php Requests per second: 253.01 [#/sec] (mean) Time per request: 39.524 [ms] (mean) Time per request: 3.952 [ms] (mean, across all concurrent requests) Transfer rate: 29931.75 [Kbytes/sec] received
  • 7. st 1 - Checking it in - Varnish Varnish Test ab -n 10000 -c 10 http://127.0.0.1:80/index.php Requests per second: 2624.59 [#/sec] (mean) Time per request: 3.810 [ms] (mean) Time per request: 0.381 [ms] (mean, across all concurrent requests) Transfer rate: 311418.96 [Kbytes/sec] received 10x FASTER With almost default config
  • 8. nd 2 Circle – The path to cache ● Add hit / miss information to all requests sub vcl_deliver { if (obj.hits > 0) { set resp.http.X-Cache = "HIT"; set resp.http.X-Cache-Hits = obj.hits; } else { set resp.http.X-Cache = "MISS"; } return (deliver); }
  • 10. rd 3 Circle – Force march ● Force Refresh sub vcl_recv { [...] if (req.http.Cache-Control ~ "no-cache") { ban_url(req.url); } […] } ● You may want to protect / restrict who can call this “ban”
  • 12. rd 4 Circle – Expired content ● Add php headers to force content to expire after a certain quantity of seconds function headers_for_page_cache($cache_length=600){ $cache_expire_date = gmdate("D, d M Y H:i:s", time() + $cache_length); header("Expires: $cache_expire_date"); header("Pragma: cache"); header("Cache-Control: max-age=$cache_length, s-maxage=$cache_length"); header("User-Cache-Control: max-age=$cache_length"); }
  • 13. th 4 Circle - Check
  • 14. th 5 Circle – All in the bag ● Ensure cache by delete cookies on unwanted items (images, css, js) sub vcl_recv { […] if (req.url ~ ".(png|gif|jpg)$") { remove req.http.Cookie; } [...]} ● Varnish doesn't cache anything with a cookie!
  • 16. th 6 Circle – Watch your steps ● Install GeoIp ● Get GeoipUsingInlineC from: https://www.varnish-cache.org/trac/wiki/GeoipUsingInlineC ● Compile and install it (findable by your OS)
  • 17. th 6 - Geoip.vcl C{ #include <dlfcn.h> #include <stdlib.h> #include <stdio.h> static const char* (*get_country_code)(char* ip) = NULL; __attribute__((constructor)) void load_module() { const char* symbol_name = "get_country_code"; const char* plugin_name = "/usr/lib/geoip_plugin.so"; void* handle = NULL; handle = dlopen( plugin_name, RTLD_NOW ); if (handle != NULL) { get_country_code = dlsym( handle, symbol_name ); if (get_country_code == NULL) fprintf( stderr, "nError: Could not load GeoIP plugin:n%snn", dlerror() ); else printf( "GeoIP plugin loaded successfully.n"); } else fprintf( stderr, "nError: Could not load GeoIP plugin:n%snn", dlerror() ); } }C
  • 18. th 6 - Add it to default.vcl sub vcl_recv { […] C{ VRT_SetHdr(sp, HDR_REQ, "017X-Country-Code:", (*get_country_code)( VRT_IP_string(sp, VRT_r_client_ip(sp)) ), vrt_magic_string_end); }C […] }
  • 20. th 7 Circle – Know your enemies ● Device Detection ● Download the devicedetection.vcl from: http://github.com/varnish/varnish-devicedetect/ ● Add it to default.vcl sub vcl_recv { […] call detectdevice; […] }
  • 22. th 8 Circle – Tongue in cheek ● Accepted language ● Download and create the accept-language.vcl from: http://github.com/cosimo/varnish-accept-language ● Include the generated file and call it in default.vcl sub vcl_recv { […] C{ vcl_rewrite_accept_language(sp); }C […] }
  • 24. th 9 Circle – Divide and conquer ● To activate add in your default.vcl sub vcl_fetch { [...] if( beresp.http.esi-enabled == "1") { set beresp.do_esi = true; /* Do ESI processing */ set beresp.ttl = 120s; /* Sets the TTL on the HTML above */ unset beresp.http.esi-enabled; } [...]
  • 25. th 9 - Check Remember, ESI has issues with non-xml like files. So, the file where you place the <esi: instructions have to look like a xml file.
  • 26. More to come... if there was time... ● Redirects ● ESI – Sessions / Users ● Purge ● SSL / SPDY / Compression ● Load Balancing ● Webserver (O.o really?)
  • 27. Varnish Resources ● The Book https://www.varnish-software.com/static/book/ ● Cache Tutorial http://www.mnot.net/cache_docs/ ● Slides (slides used on the talk) Slides will be made available later on slideshare ● Sourcecode (configs, test scripts, etc) GitHub: https://github.com/lferro9000/varnish-talk
  • 28. Thank you for listening and... ● ● ● … Specially to varnish-software and everyone that somehow contributed directly to enrich varnish. Always check varnish-software website in case of doubt, there is an huge amount of little tricks there. Remember: using varnish, is good for the environment (less power = win)!
  • 29. Credits and Thanks ● Botticelli Chart of Dante's Hell http://www.flickr.com/photos/distan/3708997567/ by Dina Dubrovskaya (Real Distan) ● The great people that made Varnish possible ● PHP London for accepting this talk