The document discusses using Varnish as a cache accelerator in front of Symfony applications. It provides an overview of Varnish configuration using VCL, the request flow through Varnish, and new features in Varnish 4. It also covers specific techniques for caching Symfony applications, including normalizing URLs and cookies, load balancing backends, handling internationalization, and invalidating caches.
PSGI is a Perl port of Python's WSGI and Ruby's Rack that defines a common interface between web servers and frameworks. Plack provides reference implementations of PSGI servers as well as middleware and utilities. This allows frameworks to run on many servers like standalone, FastCGI, and Apache using a common PSGI application interface. Plack is fast, supports many frameworks through adapters, and provides tools like Plackup and middleware to help build and test PSGI applications.
modern module development - Ken Barber 2012 Edinburgh Puppet CampPuppet
The document provides information on modern Puppet module development best practices. It discusses what modules are and common patterns like package, config, service that address 80% of module needs. It also covers validation of module parameters using Kwalify schemas, testing modules with rspec-puppet, and packaging modules for release on the Puppet Forge using the puppet-module tool. The document emphasizes the importance of coding style, linting with puppet-lint, and following patterns and best practices to create high quality, reusable modules.
Why work with Ansible to deliver software in a secure and reliable way? Gain insight quickly, this deck shows the strenghts of the IT automation tool that does it all.
Bas Meijer is an Ansible Ambassador co-hosting the Ansible Benelux Meetup since 2014. He introduced the tool to major corporate clients for use in mission critical infrastructure provisioning, application construction, container orchestration, security operations, and more.
EC2 AMI Factory with Chef, Berkshelf, and PackerGeorge Miranda
Presentation accompanying a Live Demo at the AWS Pop-Up Loft in San Francisco on using Chef + Berks + Packer to create an AWS EC2 AMI Factory.
Demo Repo available here -- https://github.com/gmiranda23/chef-ami-factory
This document discusses the history and future of operations (ops) and infrastructure management. It outlines how infrastructure has evolved from single manually configured servers to cloud-based infrastructure with immutable servers. Immutable infrastructure involves replacing servers instead of modifying them, using pre-built machine images. Tools like Packer, Vagrant, and Serf help enable immutable infrastructure by automating the creation of machine images and handling service orchestration outside of images. This approach provides benefits like speed, repeatability, stability and testability compared to traditional mutable infrastructure management.
Create Development and Production Environments with VagrantBrian Hogan
Need a Linux box to test a Wordpress site or a Windows VM to test a web site on IE 10? Creating a virtual machine to test or deploy your software doesn’t have to be a manual process. Bring one up in seconds with Vagrant, software for creating and managing virtual machines. With Vagrant, you can bring up a new virtual machine with the software you need, share directories, copy files, and configure networking using a friendly DSL. You can even use shell scripts or more powerful provisioning tools to set up your software and install your apps. Whether you need a Windows machine for testing an app, or a full-blown production environment for your apps, Vagrant has you covered.
In this talk you’ll learn to script the creation of multiple local virtual machines. Then you’ll use the same strategy to provision production servers in the cloud.
I work with Vagrant, Terraform, Docker, and other provisioning systems daily and am excited to show others how to bring this into their own workflows.
This document provides an overview of Ansible, an open source automation tool. It discusses Ansible's core components like playbooks, roles, variables and modules. It also covers how to use Ansible for tasks like configuration management, deployment, security and continuous delivery. Finally, it mentions ways to get started with Ansible including using command line tools, the galaxy module to share roles and vault to protect sensitive data.
Plack basics for Perl websites - YAPC::EU 2011leo lapworth
Run a website with Perl? - you should learn how to use Plack. Most Perl web frameworks support it and it makes your life a lot easier and a lot more fun
The document discusses various caching techniques in Rails including page caching, action caching, and fragment caching. Page caching allows entire pages to be served from cache without processing by the Rails app. Action caching caches specific actions. Fragment caching caches fragments of views for dynamic parts of pages. The document also covers Memcached for distributed caching across multiple app instances.
Plack is a superglue for Perl web frameworks that provides a common interface called PSGI (Perl Web Server Gateway Interface) inspired by WSGI and Rack. PSGI allows any web application or framework to run on any web server by providing a standard way for applications to communicate with servers. Plack also includes tools like Plackup for running PSGI applications from the command line and middleware for common functionality that can be shared across frameworks. Many existing Perl web frameworks have been adapted to run under PSGI through Plack.
This document discusses asynchronous I/O in Java and Scala using the Play Framework. It describes how LinkedIn uses a service-oriented architecture with hundreds of services making requests to each other. It then covers how Play supports non-blocking I/O using asynchronous code, promises, and futures to allow parallel requests without blocking threads. Key points covered include using map and flatMap to transform promises and futures, handling errors and timeouts, and the benefits of non-blocking I/O for scalability.
Building a desktop app with HTTP::Engine, SQLite and jQueryTatsuhiko Miyagawa
This document provides a summary of a presentation on building a desktop application using HTTP::Engine, SQLite, and jQuery. The presentation discusses using HTTP::Engine as a lightweight web server, implementing RESTful APIs and backend actions with JSON responses, using SQLite for a simple and flexible local database, and manipulating the DOM with jQuery for the user interface. The goal is to create a desktop-like experience with the technologies of web applications.
The document discusses CodeIgniter, an open source PHP MVC framework, and provides information about CodeIgniter features such as controllers, models, views, helpers, libraries, and working with databases using CodeIgniter's active record functions. It also covers topics like installing CodeIgniter, creating controllers and models, and loading views, helpers, and libraries.
Tatsumaki is a non-blocking web framework for Perl built on Plack and AnyEvent. It allows building asynchronous applications that can handle thousands of concurrent connections. Tatsumaki uses psgi.streaming to enable asynchronous responses. It includes a non-blocking HTTP client and pure Perl message queue for building real-time applications like chat and comet. The framework is in a beta stage but plans include services for XMPP/IRC bots and a standard comet interface.
Cooking Perl with Chef: Real World Tutorial with JitterbugDavid Golden
This tutorial provides a command-by-command walk-through for deploying the Jitterbug continuous integration application using the Chef configuration management tool
Can you contain the future - Docker, Container Technologies, The Future, and YouColdFusionConference
Linux containers and Docker have been all the rage in sysOps for quite some time now. Come to this session to learn about how you can or if you should be using them, and to learn about container technologies, how thy differ from VMs and if they truly are the future. Take a look at Docker basics and get an understanding of how it can be used as part of your workflow, not just for development, but for production deployments as well. We will also bust some Myths surrounding container technologies and Docker.
This document discusses using Puppet roles and profiles to organize Puppet code into logical layers (roles, profiles, resources) and tie together different modules. Roles apply directly to nodes and may only include profiles. Profiles contain resources and parameters and are applied via roles. Resources are reusable components declared with defines. Data is configured separately in Hiera.
Creating and Deploying Static Sites with HugoBrian Hogan
Most web sites don’t have data that changes, so why power them with a database and take the performance hit? In this talk we’ll explore static site generation using Hugo, an open-source static site generator. You’ll learn how to make a master layout for all pages, and how to use Markdown to create your content pages quickly.
Then we’ll explore how to deploy the site we made to production. We’ll automate the entire process. When you’re done, you’ll be able to build and deploy static web sites quickly with minimal tooling.
Vagrant is a well-known tool for creating development environments in a simple and consistent way. Since we adopted in our organization we experienced several benefits: lower project setup times, better shared knowledge among team members, less wtf moments ;-)
In this session I'd like to share our experience, including but not limited to:
- advanced vagrantfile configuration
- vm configuration tips for dev environment: performance, debug, tuning
- our wtf moments
- puphet/phansilbe: hot or not?
- tips for sharing a box
CIRCUIT 2015 - AEM Infrastructure Automation with Chef CookbooksICF CIRCUIT
Drew Glass - Hero Digital
Push button deployments can automate AEM infrastructure to reduce costs and defects. Chef is a platform that enables this by transforming infrastructure into code using DevOps practices. AEM Author, Publish and Dispatcher instances can be fully configured and deployed as code with Chef. In this talk we will discuss how the open source AEM Chef Cookbook can be used to automate the deployment of AEM instances with Chef features like recipes, attributes, providers and resources. Out of the box, the AEM Chef Cookbook supports:
- Unattended installation of AEM Author, Publish, and Dispatcher nodes.
- Automatic search for and configuration of AEM cluster members using Chef searches.
- Configuration for replication agents using the replicator provider.
- Configurations for Dispatcher farms with the farm provider.
- Deploying and removing AEM packages with the package provider.
We will also discuss how AEM can be automated to supported different SSO and deployment scenarios like cold standby. Finally, we will cover how to extend the Cookbook to support your project specific needs.
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011Alessandro Nadalin
In the first part of the presentation we see how Symfony2 implements HTTP cache.
In the second one there's an explanation of why application cache layers suck, why nerly every php application does caching in the less productive way and how you benefit from HTTP cache from this point of view.
The document discusses 14 ways to kill MySQL performance, including not using EXPLAIN for queries, choosing arbitrary data types, using persistent connections, not getting the right storage engine, and not profiling or benchmarking. It provides suggestions for improving performance such as using EXPLAIN to optimize queries, choosing smaller data types, avoiding heavy database abstraction layers, using the proper storage engine for your needs, and profiling and benchmarking your database.
You Don't Know Query - WordCamp Portland 2011andrewnacin
Andrew Nacin gave a talk at WordCamp Portland 2011 about querying in WordPress. He explained that WordPress uses the WP_Query class to run queries and populate $wp_query. The "main query" is stored in $wp_the_query and copied to $wp_query. Developers can alter queries using the 'request' or 'pre_get_posts' hooks. The 'pre_get_posts' hook is more flexible but can impact any query, so developers should check that they are only modifying the main query. Proper use of wp_reset_query() and wp_reset_postdata() is also important when running additional queries.
A brief run through of the various APIs Google offers for creating free interactive and static data visualizations.
Links mentioned in this presentation: http://dev.kingkool68.com/google-charting-api/list-o-links.html
Effective Doctrine2: Performance Tips for Symfony2 DevelopersMarcin Chwedziak
How to boost performance Doctrine2 with Symfony2. How to configure metadata caching? How to optimize DQL queries for caching. How to properly setup transaction demarcation with EntityManager. How to deal with EntityManager and Listeners with Symfony2 container.
Varnish is an HTTP accelerator and caching reverse proxy that can be used to speed up websites. It works by caching responses from the web server to reduce load and serve subsequent requests from the cache. The HTTP specification defines caching mechanisms like expiration dates, validation of cached responses using headers like Last-Modified and ETag, and conditional requests to prevent generating duplicate responses. Varnish leverages these caching features to improve website performance.
Rationally boost your symfony2 application with caching tips and monitoringGiulio De Donato
Turin SymfonyDay, 5th October 2012 by @liuggio - I will try to keep the tips updated, I suggest you put this talk to your favorites. - Any comment or feedback are really appreciated.
Speed up your Symfony2 application and build awesome features with RedisRicard Clau
Redis is an extremely fast data structure server that can be easily added to your existing stack and act like a Swiss army knife to help solve many problems that would be extremely difficult to workaround with the traditional RDBMS. In this session we will focus on what Redis is, how it works, what awesome features we can build with it and how we can use it with PHP and integrate it with Symfony2 applications making them blazing fast.
You have amazing content and you want to get it to your users as fast as possible. In today’s industry, milliseconds matter and slow websites will never keep up. You can use a CDN but they are expensive, make you dependent on a third party to deliver your content, and can be notoriously inflexible. Enter Varnish, a powerful, open-source caching reverse proxy that lives in your network and lets you take control of how your content is managed and delivered. We’ll discuss how to install and configure Varnish in front of a typical web application, how to handle sessions and security, and how you can customize Varnish to your unique needs. This session will teach you how Varnish can help you give your users a better experience while saving your company and clients money at the same time.
Slide for a talk I presented internally at Opera in December 2009 about the deployment of varnish in our production environment at my.opera.com, the social network community.
Today's high-traffic web sites must implement performance-boosting measures that reduce data processing and reduce load on the database, while increasing the speed of content delivery. One such method is the use of a cache to temporarily store whole pages, database recordsets, large objects, and sessions. While many caching mechanisms exist, memcached provides one of the fastest and easiest-to-use caching servers. Coupling memcached with the alternative PHP cache (APC) can greatly improve performance by reducing data processing time. In this talk, Ben Ramsey covers memcached and the pecl/memcached and pecl/apc extensions for PHP, exploring caching strategies, a variety of configuration options to fine-tune your caching solution, and discusses when it may be appropriate to use memcached vs. APC to cache objects or data.
The document discusses the term "varnish" and provides definitions. It defines varnish as having a deceptively attractive external appearance or outward show. It further defines varnished and varnishing as giving a smooth and glossy finish to something. The rest of the document is about configuring and using the Varnish caching system.
This document discusses using Varnish Cache and Varnish Configuration Language (VCL) for A/B testing. It provides an overview of how A/B testing works by changing one element and measuring conversions. It then discusses how to implement A/B testing using Varnish - including tagging transactions with Varnish Custom Statistics (VCS), assigning users to groups, delivering different content based on group, and logging access and conversions for analysis. The document provides examples of implementing group assignment, content selection via URL rewriting, backend selection, and use of Vary headers in VCL.
How we use Varnish at Opera Software, from the beginning (2009) to now.
Presentation hold for the 5th Varnish Users Group meeting (VUG5) held in Paris on March 22nd 2012.
Varnish is a reverse proxy cache that is designed to accelerate HTTP-based websites. It uses a domain-specific configuration language called VCL to define backend servers, caching rules, and other settings. Key aspects of the Varnish configuration covered include backend definitions, directors, vcl_recv for caching rules, vcl_hash for object identification, and vcl_fetch for backend object handling. The document discusses using Varnish to cache the homepage of a website called KeezMovies, addressing challenges of geolocation, analytics cookies, and non-idempotent requests.
Johann-Peter Hartmann from Mayflower GmbH gave a presentation on practical DevOps for developers. The presentation covered three main points: (1) managing development environments with Vagrant and VeeWee, (2) managing configuration with Puppet or Chef, and (3) including the configuration as part of source code by placing Puppet/Chef files in a "configuration" folder within the code repository. The goal is to enable reliable and repeatable development environments that are consistent with production.
Site Performance - From Pinto to FerrariJoseph Scott
This document discusses ways to improve website performance from slow "Pinto" levels to faster "Ferrari" levels. It recommends using an opcode cache like APC to speed up PHP, optimizing databases with technologies like Memcached, using caching plugins in WordPress, and considering architectures with load balancing and replication. The goal is to identify bottlenecks and apply techniques that reduce page load times through an understanding of how websites and underlying technologies work.
Learn from Fastly veteran Cassandra Dixon on some of the most common customer issues we see — such as why things aren’t caching, misconfigured origins, issues with intermediary proxies, and VCL snafus — and the best ways to resolve them. We’ll also discuss our unique approach to debugging — using seemingly mundane tools to diagnose issues in creative ways — and how you can apply these methods to your own organization to get the most out of Fastly’s offerings.
The document discusses asynchronous communication using Neky, an asynchronous event-driven network application framework. It provides examples of how Neky can be used to build scalable and high performance client-server applications. Major companies like Twitter and Facebook use Neky due to its high throughput and low latency. The document also provides instructions on getting started with Neky and an overview of its key concepts and terminology.
This document provides an introduction to Varnish Configuration Language (VCL) which defines Varnish's policy for handling HTTP requests. It discusses key VCL concepts like different request states, built-in functions, objects, and the various VCL subroutines used to define logic for different phases of a request lifecycle like vcl_recv, vcl_hash, vcl_miss etc. It also covers topics like defining backends, directors, ACLs, using VMODs and banning cached content.
In this talk, we'll cover all the great built-in rails caching options and best practices for getting the most out of these. Then we'll talk about dynamic content, why it's traditionally not cached, and how you can can cache it using this thing called "edge caching".
Welcome to the future, where you can cache the uncacheable.
Johann-Peter Hartmann from Mayflower GmbH gave a presentation on practical DevOps for developers. He discussed how development used to involve maintaining golden images and static configurations, which became unreliable as applications changed. He then showed how developers can now use tools like Vagrant and Puppet/Chef to manage development environments and configurations as code. This allows for faster setup times, consistency across environments, and easier version management.
Next Generation DevOps in Drupal: DrupalCamp London 2014Barney Hanlon
In this talk, Barney will be discussing and demonstrating how to:
- Use nginx, Varnish and Apache together in a "SPDY sandwich" to support HTTP 2.0
- Setting up SSL properly to mitigate against attack vectors
- Performance improvements with mod_pagespeed and nginx
- Deploying Drupal sites with Docker containers
Barney is a Technical Team Leader at Inviqa, a Drupal Association member and writes for Techportal on using technologies to improve website performance. He first started using PHP professionally in 2003, and has over seventeen years experience in software development. He is an advocate of Scrum methodology and has an interest in performance optimization, researching and speaking on various techniques to improve user experience through faster load times.
A presentation I gave on September 26 at the Melbourne Symfony developers group on using Environment Variables (envvars) in Symfony and managing secrets in your PHP applications.
For more information on these subjects, check out the supporting piece I wrote: https://samjarrett.com.au/swipe-right
This document discusses caching strategies for Rails applications, including:
1. Using Rails caching for queries, pages, assets, and fragments to improve performance.
2. Configuring Cache-Control headers, compression, and CDNs like Fastly for efficient caching.
3. Techniques for caching dynamic content at the edge using surrogate keys and purging cached responses.
This document discusses caching strategies for Rails applications, including:
1. Using Rails caching for queries, pages, assets, and fragments to improve performance.
2. Configuring Cache-Control headers, compression, and CDNs like Fastly for efficient caching.
3. Techniques for caching dynamic content at the edge using surrogate keys and purging cached responses.
PHPCR e API Platform: cosa significa davvero sviluppare un CMF con SymfonyInnoteam Srl
Scrivere un CMF da zero: uno degli strumenti che il mondo PHP/Symfony mette a disposizione per la gestione dei contenuti è PHPCR. Nelle slide di questo talk presentato al SymfonyDay 2018 spieghiamo come abbiamo utilizzato Doctrine PHPCR ODM e API Platform per poter costruire un CMF ed esporre le sue funzionalità in maniera REST. Inoltre mostriamo gli imprevisti che ci ha lasciato e come li abbiamo superati.
Similar to Making Symofny shine with Varnish - SymfonyCon Madrid 2014 (20)
Book dating , international dating phgrathomaskurtha9
International dating programhttps: please register here and start to meet new people todayhttps://www.digistore24.com/redir/384521/godtim/.
get started. https://www.digistore24.com/redir/384521/godtim/
Megalive99 Situs Betting Online Gacor TerpercayaMegalive99
Megalive99 telah menetapkan standar tinggi untuk platform taruhan online. Berbagai macam permainan, desain ramah pengguna, dan transaksi aman menjadikannya pilihan utama para petaruh.
seo proposal | Kiyado Innovations LLP pdfdiyakiyado
Crafting a compelling SEO proposal? Learn how to structure a winning SEO proposal template with essential elements and tips for client engagement. Elevate your SEO strategy with expert insights and examples
10th International Conference on Networks, Mobile Communications and Telema...ijp2p
10th International Conference on Networks, Mobile Communications and
Telematics (NMOCT 2024)
Scope
10th International Conference on Networks, Mobile Communications and Telematics (NMOCT 2024) is a forum for presenting new advances and research results in the fields of Network, Mobile communications, and Telematics. The aim of the conference is to provide a platform to the researchers and practitioners from both academia as well as industry to meet and share cutting-edge development in the field.
Authors are solicited to contribute to the conference by submitting articles that illustrate research results, projects, surveying works, and industrial experiences that describe significant advances in the following areas but are not limited to.
Topics of interest include, but are not limited to, the following:
Mobile Communications and Telematics Mobile Network Management and Service Infrastructure Mobile Computing Integrated Mobile Marketing Communications Efficacy of Mobile Communications Mobile Communication Applications Critical Success Factors for Mobile Communication Diffusion Metric Mobile Business Enterprise Mobile Communication Security Issues and Requirements Mobile and Handheld Devices in the Education Telematics Tele-Learning Privacy and Security in Mobile Computing and Wireless Systems Cross-Cultural Mobile Communication Issues Integration and Interworking of Wired and Wireless Networks Location Management for Mobile Communications Distributed Systems Aspects of Mobile Computing Next Generation Internet Next Generation Web Architectures Network Operations and Management Adhoc and Sensor Networks Internet and Web Applications Ubiquitous Networks Wireless Multimedia Systems Wireless Communications
Heterogeneous Wireless Networks Operating System and Middleware Support for Mobile Computing Interaction and Integration in Mobile Communications Business Models for Mobile Communications E-Commerce & E-Governance
Nomadic and Portable Communication Wireless Information Assurance Mobile Multimedia Architecture and Network Management Mobile Multimedia Network Traffic Engineering & Optimization Mobile Multimedia Infrastructure Developments Mobile Multimedia Markets & Business Models Personalization, Privacy and Security in Mobile Multimedia Mobile Computing Software Architectures Network & Communications Network Protocols & Wireless Networks Network Architectures High Speed Networks Routing, Switching and Addressing Techniques Measurement and Performance Analysis Peer To Peer and Overlay Networks QOS and Resource Management Network-Based Applications Network Security Self-organizing networks and Networked Systems Mobile & Broadband Wireless Internet Recent Trends & Developments in Computer Networks
Paper Submission
Authors are invited to submit papers through the conference Submission System by July 06, 2024. Submissions must be original and
4. Making Symfony shine with Varnish
Do we need a cache accelerator?
• Symfony is FAST considering all the features it provides
• See my talk in last year’s deSymfony conference in Madrid (in
Spanish):
http://www.desymfony.com/ponencia/2013/porque-symfony2-
es-rapido
6. Making Symfony shine with Varnish
Our case: clippingbook.com
• We were able to handle 100 req/sec
• But this was not enough to handle our load, specially when
doing Facebook promotions
• We chose Symfony because of its lower costs of development
and manteinance, not for its performance
• We do not want to renounce to any Symfony features (ORM,
Twig templates, ...)
• We could have scaled vertically or horizontally but chose to
implement a caching strategy first
7. Making Symfony shine with Varnish
The solution: Varnish
• The solution: install Varnish Cache
• Varnish Cache is a web application accelerator also known as a
caching HTTP reverse proxy
• It sits in front of your HTTP server and caches its responses,
serving content from the cache whenever possible.
• Result: we can now serve 10000 req/sec, a 100x improvement
8. Making Symfony shine with Varnish
What we will not cover
• How HTTP caching works. For more information see:
http://tools.ietf.org/pdf/rfc2616.pdf (HTTP 1.1 specification, see
section 13 for caching)
http://symfony.com/doc/current/book/http_cache.html (HTTP
caching chapter in the Symfony Book)
• Basic Varnish installation and configuration. See Fabien’s talk:
http://www.desymfony.com/ponencia/2012/varnish
9. Making Symfony shine with Varnish
What we will cover
• Why Varnish
• Quick overview of Varnish configuration
• Varnish 4. What’s new
• Using Varnish with Symfony:
• Backends
• URL normalization
• Cookies and sessions
• Internacionalization
• Serving content for different devices
• Defining caching headers
• Cache invalidation
• Cache validation
• Edge Side Includes (ESI)
10. Making Symfony shine with Varnish
Why Varnish?
PROs
• Varnish is really fast and highly configurable
• It is well documented in the Symfony documentation
• There are some bundles which help you interact with it
• Fabien’s talk provided very good information on how to use it
CONs
• Varnish documentation not too good / VCL can be cryptic
• It does not handle SSL
• Only runs on 64 bit machines
11. Making Symfony shine with Varnish
Varnish configuration overview
• Varnish uses VCL, a DSL similar to C or Perl
• Configuration saved to a file, usually /etc/varnish/default.vcl
• Translated into C, compiled and linked => fast
• Uses a number of subroutines which are called at specific times
during the handling of the request. For example vcl_recv
• These functions return a value which defines the next action that
the system will take. For example fetch
• There is a default VCL code for each function which is executed if
no value is returned
• We have some objects which represent the request (req), the
response (resp), the backend request (bereq), the backend
response (beresp) and the object in the cache (obj)
sub vcl_miss {
return (fetch);
}
13. Making Symfony shine with Varnish
Request flow
• A request is received (vcl_recv) and we decide if we want to look it
up in the cache (hash) or not (pass)
• If we do not look it up in the cache (vcl_pass) we fetch the
response from the backend (fetch) and don´t store it in the cache
• If we want to look it up, we create a hash for the content
(vcl_hash) and then look it up (lookup)
• If we find it in the cache (vcl_hit) we deliver it (deliver)
• If we don’t find it in the cache (vcl_miss) we fetch the response
from the backend (fetch)
• If we need to fetch the content, we build a request for the backend
and send it (vcl_backend_fetch)
• We receive a response from the back end (vcl_backend_response),
decide if we want to cache it and deliver it (deliver)
• We finally deliver the response to the client (vcl_deliver)
14. Making Symfony shine with Varnish
Varnish 4: what’s new
• Different threads are used for serving client requests and
backend requests
• This split allows Varnish to refresh content in the background
while serving stale content quickly to the client.
• Varnish now correctly handles cache validation, sending If-
None-Match and If-Modified-Since headers and processing
Etag and Last-Modified headers
15. Making Symfony shine with Varnish
Varnish 4: what’s changed
• req.request is now req.method (for example POST)
• vcl_fetch is now vcl_backend_response
• We have a new vcl_backend_fetch function
• To mark responses as uncacheable (hit for pass) we now use
beresp.uncacheable = true
• The purge function is no longer available. You purge content by
returning purge from vcl_recv
• vcl_recv must now return hash instead of lookup
• vcl_hash must now return lookup instead of hash
• vcl_pass must now return fetch instead of pass
• Backend restart is now retry
• Logging tools like varnishlog now have a new filtering language
which means their syntax has changed (-m option => -q)
16. Making Symfony shine with Varnish
Load balancing: backends
backend back1 {
.host = "back1.clippingbook.com";
.port = "80";
}
backend back2 {
.host = "back2.clippingbook.com";
.port = "80";
}
sub vcl_init {
new backs = directors.hash();
backs.add_backend(back1,1);
backs.add_backend(back2,1);
}
sub vcl_recv {
set req.backend_hint = backs.backend(client.identity);
}
17. Making Symfony shine with Varnish
Load balancing: backends
• Varnish includes a health check mechanism and can exclude
backends which are not healthy
• There are other load balancing mechanisms: random, round-robin,
url-based (or build your own)
• BUT if you are using the standard file-based session save
mechanism of Symfony the only method safe to use is hash
based on client ip or client session cookie
• Even this can lead to problems if one server turns unhealthy
and Varnish has to redirect to another backend
• Our recommendation: switch to a shared session server using a
database (PdoSessionHandler), Memcached
(MemcachedSessionHandler) or Redis (ScnRedisBundle)
18. Making Symfony shine with Varnish
URL normalization
• In vcl_hash we calculate a hash to look up the content in the
cache. By default it uses the URL + the host (or IP)
• We want to normalize this URL/host in order to avoid having
repeated content in the cache
• Convert the host to lowercase using std.tolower
• Remove www from the host if present
• Normalize all the query parameters using std.querysort
• Use RouterUnslashBundle to redirect all URLs to the version not
ending in /
• Note that this hash does not include Vary content
sub vcl_hash {
set req.http.host = std.tolower(req.http.host);
set req.http.host = regsub(req.http.host, "^www.", "");
set req.url = std.querysort(req.url);
}
19. Making Symfony shine with Varnish
Cookies and sessions
• Varnish by default will not cache anything which has a cookie
• Symfony sets a PHPSESSID cookie in almost all responses
• By default no content will be cached!
• We want to pass the PHPSESSID cookie to the backend but still
cache some pages even if it is set
• We must not cache any page where this cookie produces a
different response: logged users, forms (CSRF), flashes
• We do not want to cache any page for logged in users
• Most cookies are used by the client side and can be ignored
• There are some cookies which produce a different response but
it is the same for all users => we can Vary on them
• We want to clear all cookies for static content
20. Making Symfony shine with Varnish
Cookies and sessions
sub vcl_recv {
set req.http.X-cookie = req.http.cookie;
if (!req.http.Cookie ~ "Logged-In") {
unset req.http.Cookie;
}
if (req.url ~ ".(png|gif|jpg|css|js|html)$") {
unset req.http.cookie;
}
}
sub vcl_hash {
set req.http.cookie = req.http.X-cookie;
if (req.http.cookie ~ "hide_newsletter=") {
set req.http.X-Newsletter = 1;
}
}
sub vcl_pass {
set req.http.cookie = req.http.X-cookie;
}
21. Making Symfony shine with Varnish
Cookies and sessions
sub vcl_backend_response {
if (!beresp.http.Vary) {
set beresp.http.Vary = "X-Newsletter";
} elseif (beresp.http.Vary !~ "X-Newsletter") {
set beresp.http.Vary = beresp.http.Vary + ", X-Newsletter";
}
if (bereq.url ~ ".(png|gif|jpg|css|js|html)$") {
unset beresp.http.set-cookie;
}
}
sub vcl_deliver {
set resp.http.Vary = regsub(resp.http.Vary, "X-Newsletter",
"Cookie");
}
22. Making Symfony shine with Varnish
Cookies and sessions
• To create the Logged-In cookie we define a kernel.response
listener, injecting the security.context and adding/removing
the cookie as needed
23. Making Symfony shine with Varnish
Cookies and sessions
public function onKernelResponse (FilterResponseEvent $event)
{
$response = $event->getResponse();
$request = $event->getRequest();
if ($this->context->getToken() && $this->context-
>isGranted('IS_AUTHENTICATED_FULLY')) {
if (!$request->cookies->has('Logged-In')) {
$cookie = new Cookie ('Logged-In','true');
$response->headers->setCookie($cookie);
}
} else {
if ($request->cookies->has('Logged-In')) {
$response->headers->clearCookie('Logged-In');
}
}
}
24. Making Symfony shine with Varnish
Internacionalization
• If you return different content depending on a header, use the
Vary header. A common case is returning different content
based on the Accept-Language header
• But you should normalize it or your cache won’t be efficient
if (req.http.Accept-Language) {
if (req.http.Accept-Language ~ "en") {
set req.http.Accept-Language = "en";
} elsif (req.http.Accept-Language ~ "es") {
set req.http.Accept-Language = "es";
} else {
unset req.http.Accept-Language
}
}
• This is a bit simplistic. Use
https://github.com/cosimo/varnish-accept-language
• Varnish will automatically take care of Accept-Encoding
25. Making Symfony shine with Varnish
Device detection
• Another case may be device detection. We want to normalize
the user-agent and Vary on it. We can use
https://github.com/varnish/varnish-devicedetect
include "devicedetect.vcl";
sub vcl_recv { call devicedetect; } #sets X-UA-Device header
sub vcl_backend_response {
if (!beresp.http.Vary) {
set beresp.http.Vary = "X-UA-Device";
} elseif (beresp.http.Vary !~ "X-UA-Device") {
set beresp.http.Vary = beresp.http.Vary + ", X-UA-Device";
}
}
sub vcl_deliver {
set resp.http.Vary = regsub(resp.http.Vary, "X-UA-Device",
"User-Agent");
}
26. Making Symfony shine with Varnish
Device detection
• We can copy this X-UA-Device header to the user-agent
header (but we are losing information)
sub vcl_backend_fetch {
set bereq.http.user-agent = bereq.http.X-UA-Device;
}
• Else we can use the X-UA-Device directly. If, for example, we
use LiipThemeBundle, we can configure it:
liip_theme:
autodetect_theme: acme.device.detector
• acme.device.director is a service which implements the
LiipThemeBundleHelperDeviceDetectionInterface
interface and which uses X-UA-Device to choose a theme
27. Making Symfony shine with Varnish
Defining caching headers
• Set them directly in the Response object
$response->setSharedMaxAge(600);
$response->setPublic();
$response->setVary('Accept-Language');
• Use SensioFrameworkExtraBundle and the @Cache annotation
use SensioBundleFrameworkExtraBundleConfigurationCache;
/**
* @Cache(smaxage="600")
* @Cache(public=true)
* @Cache(vary={"Accept-Language"})
*/
28. Making Symfony shine with Varnish
Defining caching headers
• Use FOSHttpCacheBundle to set them in your config file
fos_http_cache:
cache_control:
rules:
-
match:
attributes: {route: ^book_list$ }
headers:
cache_control: { public: true, s_maxage: 600 }
-
match:
path: ^/info/*$
headers:
cache_control: { public: true, s_maxage: 3600 }
vary: Accept-Language
29. Making Symfony shine with Varnish
Cache invalidation
• First use case: update pages when you deploy new code
• If it is a minor and non-BC breaking change, just wait for the
cache expiration headers to do their job.
• You may need to use some cache busting mechanism like the
assets_version parameter for cache validation
• If it is a major or BC-breaking change, we just bite the bullet and
clear the whole cache by restarting Varnish
service varnish restart
• Downtime is almost inexistent but you will lose all your cached
content
• If this is important, you may want to build a cache warmer
which preloads all your important urls into the cache
30. Making Symfony shine with Varnish
Cache invalidation
• Second use case: a more granular approach: invalidate
individual pages when the underlying data changes
• We can use FOSHttpCacheBundle. First configure Varnish:
acl invalidators {
"back1.clippingbook.com";
"back2.clippingbook.com";
}
sub vcl_recv {
if (req.method == "PURGE") {
if (!client.ip ~ invalidators) {
return (synth(405, "Not allowed"));
}
return (purge);
}
if (req.http.Cache-Control ~ "no-cache" && client.ip ~
invalidators) {
set req.hash_always_miss = true;
}
}
31. Making Symfony shine with Varnish
Cache invalidation
• We then need to configure a Varnish server in Symfony:
fos_http_cache:
proxy_client:
varnish:
servers: xxx.xxx.xxx.xxx #IP of Varnish server
base_url: clippingbook.com
• We can now invalidate or refresh content programatically
$cacheManager = $container ->
get('fos_http_cache.cache_manager');
$cacheManager->invalidatePath('/books');
$cacheManager->refreshRoute('book_show', array('id' =>
$bookId));
$cacheManager->flush(); //optional
32. Making Symfony shine with Varnish
Cache invalidation
• We can also use annotations:
use FOSHttpCacheBundleConfigurationInvalidatePath;
/**
* @InvalidatePath("/books")
* @InvalidateRoute("book_show", params={"id" =
{"expression"="id"}})")
*/
public function editBookAction($id)
{
}
• This needs that SensioFrameworkExtraBundle is available and, if
we use expressions, that the ExpressionLanguage component is
installed
33. Making Symfony shine with Varnish
Cache invalidation
• Finally, we can set up invalidation in our config file:
fos_http_cache:
invalidation:
rules:
-
match:
attributes:
_route: "book_edit|book_delete"
routes:
book_list: ~
book_show: ~
34. Making Symfony shine with Varnish
Cache validation
• Varnish 4 now supports cache validation
• You should be setting the Etag and/or Last-Modified headers,
which now Varnish understands and supports
• Expiration wins over validation so while the cache is not stale
Varnish will not poll your backend to validate it
• But once the content expires it will call the backend with the
If-None-Match and/or If-Modified-Since headers
• You can use these to determine if you want to send back a
304: Not Modified response
• If you do, Varnish will continue serving the content from the
cache
35. Making Symfony shine with Varnish
Cache validation
public function showBookAction($id, $request)
{
$book = ...;
$response = new Response();
$response->setETag($book->computeETag());
$response->setLastModified($book->getModified());
$response->setPublic();
if ($response->isNotModified($request)) {
return $response; //returns 304
}
... generate and return full response
}
36. Making Symfony shine with Varnish
Edge Side Includes (ESI)
• ESI allows you to have different parts of the page which have
different caching strategies. Varnish will put the page together
• To work with Symfony you have to instruct Varnish to send a
special header advertising this capability and to respond to the
header sent back by Symfony when there is ESI content
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;
}
}
37. Making Symfony shine with Varnish
Edge Side Includes (ESI)
• Now you need to tell Symfony to enable ESI
• If you are going to reference a controller when including ESI
content you need to enable the FragmentListener so that it
generates URLs for the ESI fragments
• Finally you need to list the Varnish servers as trusted proxies
framework:
esi: { enabled: true }
fragments: { path: /_fragment }
trusted_proxies: [xxx.xxx.xxx.xxx, yyy.yyy.yyy.yyy ]
#IPs of Varnish servers
38. Making Symfony shine with Varnish
Edge Side Includes (ESI)
• In the main controller for the page, set the shared max age
public function indexAction()
{
... generate response
$response->setSharedMaxAge(600);
return $response;
}
• In your template use the render_esi helper to print ESI content
{{ render_esi(controller('...:news', { ’num': 5 })) }}
{{ render_esi(url('latest_news', { ’num': 5 })) }}
• You can now specify a different cache policy for your fragment
public function newsAction()
{
... generate response
$response->setSharedMaxAge(60);
return $response;
}
39. Making Symfony shine with Varnish
Thanks!
¡Gracias! - Thanks!
Any questions?
cgranados@clippingbook.com
@carlos_granados
https://joind.in/talk/view/12942