Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Varnish Cache and its usage in the real world

No description
by

Ivan Chepurnyi

on 5 August 2015

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Varnish Cache and its usage in the real world

Varnish Cache
and its usage in the real world
#MMNY14
@IvanChepurnyi
Independent Consultant
What is Varnish?
About me
Started in Magento Core Team in 2007
Fan of Test Driven Development
CTO at Interactiv4
Trainer at MageCampus
Varnish is an HTTP accelerator designed for content-heavy dynamic web sites.
Wikipedia: http://en.wikipedia.org/wiki/Varnish_(software)
There is nothing in cache
There is a cached version of page
Varnish is all about VCL*
* Varnish Configuration Language
vcl_recv
vcl_hit
vcl_miss
vcl_pass
vcl_fetch
vcl_deliver
vcl_pipe
vcl_hash
lookup
cached
not cached
pass
no ttl hit_for_pass
ttl is set
pipe
deliver cached
content
And this flow is
simplified
But we are not going to use all of these VCL subs...
vcl_recv
vcl_hash
vcl_fetch
vcl_deliver
Control the access to admin or cache refresh
Detect a customer segment based on cookies
Remove redundant query parameters and cookies:
Google Adwords
Newsletter Campaign parameters
Detect SSL offload, an external load balancer, etc
Building cache hash for page
Gives possibility to serve different content based on:
Customer device type (tablet, mobile, desktop)
Detected Customer Segment
SSL Offload parameters, etc
Processes response headers from Magento backend
The entry point for a customer request
Check if request has to be cached
Removes redundant headers for a cached content
Sets appropriate flags for processing ESI tags
Supply additional response headers
The last point where you can modify response that is send to client
Used to set cookies, depending on customer segment
Here you can hide headers from end client, that are used only for communications between Varnish and Magento backend
The secret of successful Magento Varnish integration is in 4 key components
1. HTTP Headers
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
X-Cache-Ttl: 21600s
X-Cache-Objects: :CMSB:,:CMSP2:,:CMSBwhy_best
Specify cache TTL from Magento
Specify object ids, that are present on the page
Segmentation of request based on detected country or device
Make smart rules for ESI processing
2. Cookies
Connect dynamic actions to a cached segment: store code, customer group
Store a dynamic action hash, for triggering dynamic block update on the page
4. Browser Storage
Cache dynamic blocks with customer related data, retrieved by AJAX calls
3. JavaScript
Making AJAX calls for retrieving dynamic page parts
Set Cookies on Dymanic Actions from static pages
For all of these I developed a framework...
Varnish Chef Cookbook
Magento Varnish Integration
Sample Vagrant Configuration
https://github.com/EcomDev/EcomDev_Varnish
https://github.com/EcomDev/chef-magento_varnish
https://github.com/EcomDev/vagrant-magento
By default it caches such pages:
Category View + Layered Navigation
Product View
CMS Pages
Home Pages
But adding a new page type is very simple...
Add a page to the list of known handles
<config>
<varnish>
<pages>
<
page_layout_handle
translate="label" module="you_module_name">
<label>
Page Name
</label>
</
page_layout_handle>
</pages>
</varnish>
</config>
Layout handle that is used for your custom page
Label, that will appear in Magento Configuration
Specify default TTL for this page
<config>
<default>
<varnish>
<pages>
<
page_layout_handle
_time>360</
page_layout_handle
_time>
</pages>
</varnish>
</default>
</config>
Layout handle + _time suffix
TTL value in seconds
So you will need to make some very small adjustments to your theme.
1. Find your dynamic block on the page
2. Find appropriate cookie for it
3. Specify your dynamic block via
Layout XML
<default_varnish>
<reference name=”
parentBlock
”>
<action method="unsetChild”><block>
dynamicBlockAlias
</block></action>
<block as="
dynamicBlockAlias
” name=”
dynamicBlockPlaceholder
"
template="ecomdev/varnish/wrapper/placeholder.phtml" type="core/template">
<action method="append"><block>
dynamicBlock
</block></action>
<action method="setBlockName"><block>
dynamicBlock
</block></action>
<action method="setCookie"><cookie>
dynamicCookie
</cookie></action>
<action method="setWrapperId"><htmlId>
elementId
</htmlId></action>
</block>
</reference>
</default_varnish>

Custom Layout XML
But what if you need to add some custom object into varnish?
It doesn't make your cached website dynamic by default, because every theme is different.
By default it purges cache for such objects:
Category
Product
CMS Page
CMS Block
1. Create a Collector
2. Create a Processor
3. Register them in config.xml
Collects objects from a block where it is rendered
Processes collected objects and returns list of object tags
class Your_Module_Model_Collector
extends
EcomDev_Varnish_Model_AbstractApplicable
implements
EcomDev_Varnish_Model_CollectorInterface
{
protected
$_applicableClasses
= array(
'
Your_Module_Block_Object
'
);

public function
collect
($block)
{
// Your logic to retrieve objects
return $objects;
}
}
Collector Class
class Your_Module_Model_Processor
extends EcomDev_Varnish_Model_AbstractProcessor
{
const
TAG_PREFIX
= 'YM';

protected
$_applicableClasses
= array(
'
Your_Module_Model_Object
'
);

protected function
_collectTags
($object)
{
return self::
TAG_PREFIX
.
$object
-
>getId
();
}
}
Processor Class
Available Dynamic Cookies
quote_checksum
– checksum of the current quote contents

customer_checksum
– checksum based on the customer identification, if logged in customer gets changed

is_logged_in
– boolean flag of the logged in state of the visitor

segment_checksum
– checksum of the current customer segment:
customer group id
store view

<config>
<varnish>
<object>
<collectors>
<your_object>
your_module/collector
</your_object>
</collectors>
<processors>
<your_object>
your_module/processor
</your_object>
</processors>
</object>
</varnish>
</config>
Config XML
There is also some events available for you
ecomdev_varnish_cookie_apply_before
When dynamic cookies are being updated:
ecomdev_varnish_cookie_apply_after
ecomdev_varnish_customer_segment
When customer segment gets generated
But that is not all...
Additionally to extension you also get an awesome chef coookbook for configuring of your Varnish instance!
Questions?
@IvanChepurnyi
Full transcript