SlideShare una empresa de Scribd logo
1 de 31
Descargar para leer sin conexión
My Opera meets Varnish
                              varnish
         high performance web caching
                     cosimo@opera.com
What is Varnish?




                                   varnish




● Caching reverse proxy, like Squid          backends
● Delegates memory mgmt to OS cache

● Mainly developed at Linpro in Oslo
Two typical Varnish setups
incoming
requests




frontends




varnish




backends
VCL - Varnish Config Language
•   man vcl
•   VCL is compiled to C code
•   Injected into the running instance, without restart
•   Must define a backend or a director
•   VCL gives you several hooks:
    vcl_recv()
    vcl_hash()
    vcl_fetch()
    vcl_hit()
    vcl_miss()
    vcl_deliver()
Varnish deployment in My Opera
• In production beginning of October 2009
• 1 old recycled machine, 2 Gb of disk allocated
• Started serving avatars
  1M+ requests per day before Unite
  http://my.opera.com/<username>/avatar.pl
• Soon after, added Desktop Team RSS (very popular!)
• then user pictures, hundreds of thousands req/day
• then Unite/ASD API requests
  - friends of a user
  - groups of a user
• In total, 13,25% of all My Opera requests are «varnished»
• Around 7,2M req/day
Varnish deployment in My Opera
Problems /1
• Still using Debian Etch?
  First Varnish instance was running v1.x from Etch.
  several years old, not good


• Experienced VIPs
   – ”Very Interesting Problems”
   – User X getting User Y's session
   – Random users getting admin powers. Nightmare!

• Theory: Varnish was caching response bodies that contained
  Set-Cookie: opera_session=<session_id>
Varnish deployment in My Opera
Problems /2

• There wasn't any obvious configuration problem.
  Same config worked with 2.0.x from Backports.


• v2.0.{4,5} is highly recommended!
Varnish deployment in My Opera
Problems /3

• We tried caching the frontpage of My Opera, but had to revert the
  change due to too many different custom layouts for Opera Mobile,
  Mini, IE, Firefox, etc...

• Maybe using clever vcl_hash() tricks we can achieve that too.
My Opera configuration
Backends and Directors

• Backend
  single backend machine, or load-balanced virtual server

• Director
   – simple round-robin or random weighted “balancing” logic
   – has basic connection retries mechanism
   – has basic backend health check

• If you already have an LVS, define a single Backend
  Otherwise, go for the Director
Backends and Directors

Define a backend

# Only hit the upload servers
backend myopera {
    .host = "upload.my.opera.com";
    .port = "80";
}
Backends and Directors

Define a director

director myopera round-robin {
    .backend   {
       .host   = "b1.opera.com";
       .port   = "80";
    }
    .backend   {
       .host   = "b2.opera.com";
       .port   = "80";
    }
    ...
}
Backends and Directors

...and then use them

sub vcl_recv {
    ...
    set req.backend = myopera;
    ...
}
vcl_recv() / 1

sub vcl_recv {

 set req.backend = myopera;
 set req.grace = 3m;

 # URL patterns based cache.
 # Avoid possible mixups.
 if(req.http.host !~ "^my.opera.com$") {
    pass;
 }
vcl_recv() / 2

if (req.url ~ "^/community/users/avatar.pl/[0-9]+$"
  || req.url ~ "^/.+/avatar.pl$"
  || req.url ~ "^/.+/picture.pl?xscale=100$"
  || req.url ~ "^/desktopteam/xml/atom/blog/?$"
  || req.url ~ "^/desktopteam/xml/rss/blog/?$"
  || req.url ~ "^/community/api/users/friends.pl?user=.+$"
  || req.url ~ "^/community/api/users/groups.pl?user=.+$"
) {
    unset req.http.Cookie;
    unset req.http.Authorization;
    lookup;
}
vcl_recv() / 3
    ...
    # Check for cookie only after always-cache URLs
    if (req.http.Cookie ~ "(opera_session|opera_persistent_)") {
      pass;
    }

    # DANGER, Will Robinson! Caching the front-page
    # At this point, lots of Google Analytics cookies will go in.
    # No problem. It's stuff used by Javascript
    if (req.url ~ "^/community/$") {
        lookup;
    }

    pass;
}
vcl_fetch() / 1
sub vcl_fetch {

  set obj.http.X-Varnish-URL = req.url;
  set obj.grace = 3m;

  if (obj.http.Set-Cookie) {
    set obj.http.X-Varnish-Cacheable = "no, set-cookie";
    pass;
  }

  if (req.request != "GET") {
    set obj.http.X-Varnish-Cacheable = "no, !GET";
    pass;
  }
vcl_fetch() / 2
if (req.http.host !~ "^my.opera.com$") {
  set obj.http.X-Varnish-Cacheable = "no, !my.opera.com";
  pass;
}

if (req.url ~ "^/community/users/avatar.pl/[0-9]+$"
  || req.url ~ "^/[A-Za-z0-9]+/avatar.pl$"
  || ... ) {
    unset obj.http.Set-Cookie;
    set obj.http.X-Varnish-Cacheable = "yes, url";
    set obj.ttl = 24h;
    deliver;
}
vcl_hash()
sub vcl_hash {

    # Default Varnish behavior
    set req.hash += req.url;
    set req.hash += req.http.host;

    # Have a different cached frontpage per language
    if (req.url ~ "^/community/$") {
      set req.http.X-FrontPage-Language = regsub(
          req.http.Cookie,
          "^.*?language=([^;]*?);*.*$", "1"
      );
      set req.hash += "lang:";
      set req.hash += req.http.X-FrontPage-Language;
    }

    hash;
}
Testing Varnish
how to avoid nightmares...

• Developed a testing tool (varnish-test)
   – outputs a TAP stream and some debug info
   – works best if varnish is specially tuned

• Can quickly check if a test/production instance is performing
  correctly or having problems

• Invoked as a simple script:
  va rnis h-tes t --profile=tes ts .url --hos t=b1
Testing Varnish
caching test list

# Fro ntpa g e
/    N O _C O O K I E S V A R N I S H _C A C H E D
/    N O _C O O K I E S V A R N I S H _N O T _C A C H E D   H o s t: m y.c n.o pera .c o m
/    N O _C O O K I E S V A R N I S H _C A C H E D      C o o k ie:la ng ua g e=it


# B lo g s
/des k to ptea m /blo g / N O _C O O K I E S     V A R N I S H _N O T _C A C H E D


# A va ta rs
/c o m m unity/us ers /a va ta r/817271 N O _C O O K I E S V A R N I S H _C A C H E D
/c o m m unity/us ers /a va ta r/442       N O _C O O K I E S V A R N I S H _C A C H E D
/g ra phic s /a va ta r.g if        N O _C O O K I E S V A R N I S H _N O T _C A C H E D
Testing Varnish
caching test list

• We can specify exactly how the varnish instance should behave.
  – Production acceptance tests
  – Test new varnish versions, new OS distributions
  – Fine tune config changes quickly with no impact on production

• Midway through there's a request that logs in as a test user.
  From then on, we can verify what resources are cached when
  a user is logged in. Some resources should be cached in any case.
Testing Varnish
sample run
  ...
  ok 289 - Got response from backend for /community/ (from ...)
  ok 290 - Correct status line
  # Adding header [Cookie] => [language=it]
  # ----------
  # GET http://cache01.my.opera.com:6081/community/
  # Host: my.opera.com
  # ------------
  ok 291 - 2nd request: got response from backend for /community/ (from...)
  ok 292 - Correct status line
          X-Varnish: 1211283813 1211283812
  # X-Varnish: 1211283813 1211283812
          X-Varnish-Status: hit
  # X-Varnish-Status: hit
  # X-Varnish-Cacheable: yes, language cookie
          X-Varnish-Cacheable: yes, language cookie
  # X-Varnish-URL: /community/
          X-Varnish-URL: /community/
  ok 293 - URL '/community/' was handled correctly by varnish
  # cookie_header:
  ok 294 - URL '/community/' has correct cookies (or no cookies)
  1..294

All tests successful.
Monitoring Varnish
built-in tools

• varnishlog
   – Reads shared memory log info and displays it
   – Full instance log, on My Opera, 1 day is about 15 Gb
   – You can get an emulated Apache-style access.log from it

• varnishncsa
   – Displays requests to Varnish as Apache access logs
   – Can read from an archived log by varnishlog

• varnishstat
   – Displays realtime stats (hit ratio, space allocated, connections,...)
Monitoring Varnish
external tools

• Munin plugins
  – Hit ratio
  – Requests rate
  – Backend traffic

• Nagios plugins
  – Nothing special, TCP connection to port 6081
Monitoring Varnish
Monitoring Varnish
Monitoring Varnish
Next steps

• My Opera front page caching
• My Opera files server?
• Working on a prototype thumbnail server
References and more information

• Redpill-Linpro website
   – http://varnish.projects.linpro.no
   – Bug tracking, documentation and community support
   – Users and developers mailing lists

• Commercial support and training
   – http://www.varnish-cache.com
Questions?


• At Opera, there's several teams using Varnish in production
• If you want to know more, contact me: cosimo@opera.com

Más contenido relacionado

La actualidad más candente

Railsconf2011 deployment tips_for_slideshare
Railsconf2011 deployment tips_for_slideshareRailsconf2011 deployment tips_for_slideshare
Railsconf2011 deployment tips_for_slideshare
tomcopeland
 

La actualidad más candente (20)

Fabric workshop(1) - (MOSG)
Fabric workshop(1) - (MOSG)Fabric workshop(1) - (MOSG)
Fabric workshop(1) - (MOSG)
 
HTTP caching with Varnish
HTTP caching with VarnishHTTP caching with Varnish
HTTP caching with Varnish
 
Going crazy with Varnish and Symfony
Going crazy with Varnish and SymfonyGoing crazy with Varnish and Symfony
Going crazy with Varnish and Symfony
 
Ansible for beginners
Ansible for beginnersAnsible for beginners
Ansible for beginners
 
Ansible - Swiss Army Knife Orchestration
Ansible - Swiss Army Knife OrchestrationAnsible - Swiss Army Knife Orchestration
Ansible - Swiss Army Knife Orchestration
 
IT Automation with Ansible
IT Automation with AnsibleIT Automation with Ansible
IT Automation with Ansible
 
Puppet at Opera Sofware - PuppetCamp Oslo 2013
Puppet at Opera Sofware - PuppetCamp Oslo 2013Puppet at Opera Sofware - PuppetCamp Oslo 2013
Puppet at Opera Sofware - PuppetCamp Oslo 2013
 
How we used ruby to build locaweb's cloud (http://presentations.pothix.com/ru...
How we used ruby to build locaweb's cloud (http://presentations.pothix.com/ru...How we used ruby to build locaweb's cloud (http://presentations.pothix.com/ru...
How we used ruby to build locaweb's cloud (http://presentations.pothix.com/ru...
 
Making environment for_infrastructure_as_code
Making environment for_infrastructure_as_codeMaking environment for_infrastructure_as_code
Making environment for_infrastructure_as_code
 
DevOps(4) : Ansible(2) - (MOSG)
DevOps(4) : Ansible(2) - (MOSG)DevOps(4) : Ansible(2) - (MOSG)
DevOps(4) : Ansible(2) - (MOSG)
 
Shared Object images in Docker: What you need is what you want.
Shared Object images in Docker: What you need is what you want.Shared Object images in Docker: What you need is what you want.
Shared Object images in Docker: What you need is what you want.
 
Dockerizing WordPress
Dockerizing WordPressDockerizing WordPress
Dockerizing WordPress
 
Best practices for ansible
Best practices for ansibleBest practices for ansible
Best practices for ansible
 
Railsconf2011 deployment tips_for_slideshare
Railsconf2011 deployment tips_for_slideshareRailsconf2011 deployment tips_for_slideshare
Railsconf2011 deployment tips_for_slideshare
 
Learn basic ansible using docker
Learn basic ansible using dockerLearn basic ansible using docker
Learn basic ansible using docker
 
Automation with ansible
Automation with ansibleAutomation with ansible
Automation with ansible
 
Securing Prometheus exporters using HashiCorp Vault
Securing Prometheus exporters using HashiCorp VaultSecuring Prometheus exporters using HashiCorp Vault
Securing Prometheus exporters using HashiCorp Vault
 
php & performance
 php & performance php & performance
php & performance
 
Ansible tips & tricks
Ansible tips & tricksAnsible tips & tricks
Ansible tips & tricks
 
Configuration Surgery with Augeas
Configuration Surgery with AugeasConfiguration Surgery with Augeas
Configuration Surgery with Augeas
 

Destacado

Destacado (6)

IPW2008 - my.opera.com scalability
IPW2008 - my.opera.com scalabilityIPW2008 - my.opera.com scalability
IPW2008 - my.opera.com scalability
 
Italian, do you speak it?
Italian, do you speak it?Italian, do you speak it?
Italian, do you speak it?
 
NPW2009 - my.opera.com scalability v2.0
NPW2009 - my.opera.com scalability v2.0NPW2009 - my.opera.com scalability v2.0
NPW2009 - my.opera.com scalability v2.0
 
YAPC::EU::2009 - How Opera Software uses Perl
YAPC::EU::2009 - How Opera Software uses PerlYAPC::EU::2009 - How Opera Software uses Perl
YAPC::EU::2009 - How Opera Software uses Perl
 
Velocity 2012 - Learning WebOps the Hard Way
Velocity 2012 - Learning WebOps the Hard WayVelocity 2012 - Learning WebOps the Hard Way
Velocity 2012 - Learning WebOps the Hard Way
 
Velocity 2011 - Our first DDoS attack
Velocity 2011 - Our first DDoS attackVelocity 2011 - Our first DDoS attack
Velocity 2011 - Our first DDoS attack
 

Similar a My Opera meets Varnish, Dec 2009

Caching with Varnish
Caching with VarnishCaching with Varnish
Caching with Varnish
schoefmax
 
T3DD12 Caching with Varnish
T3DD12 Caching with VarnishT3DD12 Caching with Varnish
T3DD12 Caching with Varnish
AOE
 
T3DD12 Caching with Varnish
T3DD12 Caching with VarnishT3DD12 Caching with Varnish
T3DD12 Caching with Varnish
AOE
 
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
 
Roy foubister (hosting high traffic sites on a tight budget)
Roy foubister (hosting high traffic sites on a tight budget)Roy foubister (hosting high traffic sites on a tight budget)
Roy foubister (hosting high traffic sites on a tight budget)
WordCamp Cape Town
 

Similar a My Opera meets Varnish, Dec 2009 (20)

Caching with Varnish
Caching with VarnishCaching with Varnish
Caching with Varnish
 
Supercharging Content Delivery with Varnish
Supercharging Content Delivery with VarnishSupercharging Content Delivery with Varnish
Supercharging Content Delivery with Varnish
 
infra-as-code
infra-as-codeinfra-as-code
infra-as-code
 
Varnish Configuration Step by Step
Varnish Configuration Step by StepVarnish Configuration Step by Step
Varnish Configuration Step by Step
 
T3DD12 Caching with Varnish
T3DD12 Caching with VarnishT3DD12 Caching with Varnish
T3DD12 Caching with Varnish
 
Docker presentasjon java bin
Docker presentasjon java binDocker presentasjon java bin
Docker presentasjon java bin
 
Modern tooling to assist with developing applications on FreeBSD
Modern tooling to assist with developing applications on FreeBSDModern tooling to assist with developing applications on FreeBSD
Modern tooling to assist with developing applications on FreeBSD
 
T3DD12 Caching with Varnish
T3DD12 Caching with VarnishT3DD12 Caching with Varnish
T3DD12 Caching with Varnish
 
OSCP Preparation Guide @ Infosectrain
OSCP Preparation Guide @ InfosectrainOSCP Preparation Guide @ Infosectrain
OSCP Preparation Guide @ Infosectrain
 
Performance
PerformancePerformance
Performance
 
(ARC402) Deployment Automation: From Developers' Keyboards to End Users' Scre...
(ARC402) Deployment Automation: From Developers' Keyboards to End Users' Scre...(ARC402) Deployment Automation: From Developers' Keyboards to End Users' Scre...
(ARC402) Deployment Automation: From Developers' Keyboards to End Users' Scre...
 
Build Automation 101
Build Automation 101Build Automation 101
Build Automation 101
 
RichFaces - Testing on Mobile Devices
RichFaces - Testing on Mobile DevicesRichFaces - Testing on Mobile Devices
RichFaces - Testing on Mobile Devices
 
Site Performance - From Pinto to Ferrari
Site Performance - From Pinto to FerrariSite Performance - From Pinto to Ferrari
Site Performance - From Pinto to Ferrari
 
Continuous Delivery: The Next Frontier
Continuous Delivery: The Next FrontierContinuous Delivery: The Next Frontier
Continuous Delivery: The Next Frontier
 
Varnish e caching di applicazioni Rails
Varnish e caching di applicazioni RailsVarnish e caching di applicazioni Rails
Varnish e caching di applicazioni Rails
 
Apache Traffic Server
Apache Traffic ServerApache Traffic Server
Apache Traffic Server
 
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
 
PHP London Dec 2013 - Varnish - The 9 circles of hell
PHP London Dec 2013 - Varnish - The 9 circles of hellPHP London Dec 2013 - Varnish - The 9 circles of hell
PHP London Dec 2013 - Varnish - The 9 circles of hell
 
Roy foubister (hosting high traffic sites on a tight budget)
Roy foubister (hosting high traffic sites on a tight budget)Roy foubister (hosting high traffic sites on a tight budget)
Roy foubister (hosting high traffic sites on a tight budget)
 

Último

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
Safe Software
 

Último (20)

AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
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
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
"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 ...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
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
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
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...
 

My Opera meets Varnish, Dec 2009

  • 1. My Opera meets Varnish varnish high performance web caching cosimo@opera.com
  • 2. What is Varnish? varnish ● Caching reverse proxy, like Squid backends ● Delegates memory mgmt to OS cache ● Mainly developed at Linpro in Oslo
  • 3. Two typical Varnish setups incoming requests frontends varnish backends
  • 4. VCL - Varnish Config Language • man vcl • VCL is compiled to C code • Injected into the running instance, without restart • Must define a backend or a director • VCL gives you several hooks: vcl_recv() vcl_hash() vcl_fetch() vcl_hit() vcl_miss() vcl_deliver()
  • 5. Varnish deployment in My Opera • In production beginning of October 2009 • 1 old recycled machine, 2 Gb of disk allocated • Started serving avatars 1M+ requests per day before Unite http://my.opera.com/<username>/avatar.pl • Soon after, added Desktop Team RSS (very popular!) • then user pictures, hundreds of thousands req/day • then Unite/ASD API requests - friends of a user - groups of a user • In total, 13,25% of all My Opera requests are «varnished» • Around 7,2M req/day
  • 6. Varnish deployment in My Opera Problems /1 • Still using Debian Etch? First Varnish instance was running v1.x from Etch. several years old, not good • Experienced VIPs – ”Very Interesting Problems” – User X getting User Y's session – Random users getting admin powers. Nightmare! • Theory: Varnish was caching response bodies that contained Set-Cookie: opera_session=<session_id>
  • 7. Varnish deployment in My Opera Problems /2 • There wasn't any obvious configuration problem. Same config worked with 2.0.x from Backports. • v2.0.{4,5} is highly recommended!
  • 8. Varnish deployment in My Opera Problems /3 • We tried caching the frontpage of My Opera, but had to revert the change due to too many different custom layouts for Opera Mobile, Mini, IE, Firefox, etc... • Maybe using clever vcl_hash() tricks we can achieve that too.
  • 10. Backends and Directors • Backend single backend machine, or load-balanced virtual server • Director – simple round-robin or random weighted “balancing” logic – has basic connection retries mechanism – has basic backend health check • If you already have an LVS, define a single Backend Otherwise, go for the Director
  • 11. Backends and Directors Define a backend # Only hit the upload servers backend myopera { .host = "upload.my.opera.com"; .port = "80"; }
  • 12. Backends and Directors Define a director director myopera round-robin { .backend { .host = "b1.opera.com"; .port = "80"; } .backend { .host = "b2.opera.com"; .port = "80"; } ... }
  • 13. Backends and Directors ...and then use them sub vcl_recv { ... set req.backend = myopera; ... }
  • 14. vcl_recv() / 1 sub vcl_recv { set req.backend = myopera; set req.grace = 3m; # URL patterns based cache. # Avoid possible mixups. if(req.http.host !~ "^my.opera.com$") { pass; }
  • 15. vcl_recv() / 2 if (req.url ~ "^/community/users/avatar.pl/[0-9]+$" || req.url ~ "^/.+/avatar.pl$" || req.url ~ "^/.+/picture.pl?xscale=100$" || req.url ~ "^/desktopteam/xml/atom/blog/?$" || req.url ~ "^/desktopteam/xml/rss/blog/?$" || req.url ~ "^/community/api/users/friends.pl?user=.+$" || req.url ~ "^/community/api/users/groups.pl?user=.+$" ) { unset req.http.Cookie; unset req.http.Authorization; lookup; }
  • 16. vcl_recv() / 3 ... # Check for cookie only after always-cache URLs if (req.http.Cookie ~ "(opera_session|opera_persistent_)") { pass; } # DANGER, Will Robinson! Caching the front-page # At this point, lots of Google Analytics cookies will go in. # No problem. It's stuff used by Javascript if (req.url ~ "^/community/$") { lookup; } pass; }
  • 17. vcl_fetch() / 1 sub vcl_fetch { set obj.http.X-Varnish-URL = req.url; set obj.grace = 3m; if (obj.http.Set-Cookie) { set obj.http.X-Varnish-Cacheable = "no, set-cookie"; pass; } if (req.request != "GET") { set obj.http.X-Varnish-Cacheable = "no, !GET"; pass; }
  • 18. vcl_fetch() / 2 if (req.http.host !~ "^my.opera.com$") { set obj.http.X-Varnish-Cacheable = "no, !my.opera.com"; pass; } if (req.url ~ "^/community/users/avatar.pl/[0-9]+$" || req.url ~ "^/[A-Za-z0-9]+/avatar.pl$" || ... ) { unset obj.http.Set-Cookie; set obj.http.X-Varnish-Cacheable = "yes, url"; set obj.ttl = 24h; deliver; }
  • 19. vcl_hash() sub vcl_hash { # Default Varnish behavior set req.hash += req.url; set req.hash += req.http.host; # Have a different cached frontpage per language if (req.url ~ "^/community/$") { set req.http.X-FrontPage-Language = regsub( req.http.Cookie, "^.*?language=([^;]*?);*.*$", "1" ); set req.hash += "lang:"; set req.hash += req.http.X-FrontPage-Language; } hash; }
  • 20. Testing Varnish how to avoid nightmares... • Developed a testing tool (varnish-test) – outputs a TAP stream and some debug info – works best if varnish is specially tuned • Can quickly check if a test/production instance is performing correctly or having problems • Invoked as a simple script: va rnis h-tes t --profile=tes ts .url --hos t=b1
  • 21. Testing Varnish caching test list # Fro ntpa g e / N O _C O O K I E S V A R N I S H _C A C H E D / N O _C O O K I E S V A R N I S H _N O T _C A C H E D H o s t: m y.c n.o pera .c o m / N O _C O O K I E S V A R N I S H _C A C H E D C o o k ie:la ng ua g e=it # B lo g s /des k to ptea m /blo g / N O _C O O K I E S V A R N I S H _N O T _C A C H E D # A va ta rs /c o m m unity/us ers /a va ta r/817271 N O _C O O K I E S V A R N I S H _C A C H E D /c o m m unity/us ers /a va ta r/442 N O _C O O K I E S V A R N I S H _C A C H E D /g ra phic s /a va ta r.g if N O _C O O K I E S V A R N I S H _N O T _C A C H E D
  • 22. Testing Varnish caching test list • We can specify exactly how the varnish instance should behave. – Production acceptance tests – Test new varnish versions, new OS distributions – Fine tune config changes quickly with no impact on production • Midway through there's a request that logs in as a test user. From then on, we can verify what resources are cached when a user is logged in. Some resources should be cached in any case.
  • 23. Testing Varnish sample run ... ok 289 - Got response from backend for /community/ (from ...) ok 290 - Correct status line # Adding header [Cookie] => [language=it] # ---------- # GET http://cache01.my.opera.com:6081/community/ # Host: my.opera.com # ------------ ok 291 - 2nd request: got response from backend for /community/ (from...) ok 292 - Correct status line X-Varnish: 1211283813 1211283812 # X-Varnish: 1211283813 1211283812 X-Varnish-Status: hit # X-Varnish-Status: hit # X-Varnish-Cacheable: yes, language cookie X-Varnish-Cacheable: yes, language cookie # X-Varnish-URL: /community/ X-Varnish-URL: /community/ ok 293 - URL '/community/' was handled correctly by varnish # cookie_header: ok 294 - URL '/community/' has correct cookies (or no cookies) 1..294 All tests successful.
  • 24. Monitoring Varnish built-in tools • varnishlog – Reads shared memory log info and displays it – Full instance log, on My Opera, 1 day is about 15 Gb – You can get an emulated Apache-style access.log from it • varnishncsa – Displays requests to Varnish as Apache access logs – Can read from an archived log by varnishlog • varnishstat – Displays realtime stats (hit ratio, space allocated, connections,...)
  • 25. Monitoring Varnish external tools • Munin plugins – Hit ratio – Requests rate – Backend traffic • Nagios plugins – Nothing special, TCP connection to port 6081
  • 29. Next steps • My Opera front page caching • My Opera files server? • Working on a prototype thumbnail server
  • 30. References and more information • Redpill-Linpro website – http://varnish.projects.linpro.no – Bug tracking, documentation and community support – Users and developers mailing lists • Commercial support and training – http://www.varnish-cache.com
  • 31. Questions? • At Opera, there's several teams using Varnish in production • If you want to know more, contact me: cosimo@opera.com