Иван Чепурный - meet magento ukraine - varnish cache and its usage in the real world

34
Varnish Cache and its usage in the real world Ivan Chepurnyi CTO Interactiv4

Upload: atwix

Post on 15-Jul-2015

94 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Varnish Cache and its usage in the real world

Ivan ChepurnyiCTO

Interactiv4

Page 2: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi

About me

Meet Magento

• Technical Consultant, Owner at EcomDev B.V.

• Started as one of the first five developers in original Magento

core team

• Magento Developer Coach in Europe

• Main areas of my expertise:

– System Architecture

– Performance Optimization

– Test Driven Development

– Complex Customizations

Page 3: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi Meet Magento

Varnish

is not a cache backend

Page 4: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi Meet Magento

Varnish

is a frontend caching proxy

Page 5: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi

Simple Workflow

Meet Magento

First call to a page

Page 6: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi

Simple Workflow

Meet Magento

Subsequent requests

Page 7: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi

How Varnish Works

Meet Magento

recv

hit pass miss

hash pipe

deliver

fetch

• recv – request is received from client

• pipe – direct output of backend data

(streaming)

• hash – request is cacheable, lookup

cache entry

• pass – request is not cacheable

• hit – cache entry is found

• miss – cache entry not found

• fetch – retrieval of data from

backend

• deliver – return data to client

Page 8: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi

What can we do with it?

Meet Magento

• Cache static pages (Homepage, CMS, Contacts, etc)

• Cache catalog pages:

– Category listings

– Product search results

– Product view pages

• Cache page parts:

– CMS Blocks

– Header

– Footer

Page 9: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi Meet Magento

Is it possible to clear Varnish

cache based on product, category,

store, etc?

YES!!!

Page 10: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi Meet Magento

The Secret is in

Cache Object structure

Page 11: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi

Cached Object in Varnish

Meet Magento

Cached Object

Response Headers

Response Body

Cache Metadata

Cache Content

Page 12: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi Meet Magento

We just going to supply object ID with

its type in response headers, so it later

on can be used to flush pages

containing our object.

Page 13: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi Meet Magento

But is it possible to make cache lifetime

dynamic per product, category, etc?

YES!!!

Page 14: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi Meet Magento

You can supply a response header,

that contains a TTL of the page.

Page 15: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi Meet Magento

So what should be done to implement

Varnish in Magento with all the

benefits?

Page 16: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi

Varnish in Magento

Meet Magento

• Collect current page objects, that are shown on the page.

Also add them into response headers.

• Create a connector to a Varnish admin protocol, that will be

used for flushing of the page by object ids.

• Implement auto-updated AJAX blocks for:

– Shopping cart

– Wishlist

– Customer Account links

Page 17: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi Meet Magento

But I have good news:

I alredy developed a module that gives you

a solid foundation for using Varnish in your

project!

Page 18: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi

EcomDev_Varnish

Meet Magento

Download URL: http://bit.ly/ecomdev_varnish

Requires:

• Varnish 3.0

• Minimal changes to your theme

Supports:

• Flush of cache on update of product, category, cms page, csm block, price rules

• Client side cacheable AJAX placeholders (Cart, Wishlist, etc)

• Possibility to make a cache based on customer segment

• Cache for logged in users

Page 19: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi

Before you start using it…

Meet Magento

• Make a list of dynamic blocks in your project:

– Shopping Cart

– Login blocks

– Special Promo for Customer

• Validate possible visitor segments of your project:

– Customer group

– Language / Country

• Make a list of themes you need to modify

Page 20: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi Meet Magento

Making an element dynamic on varnish

cached page

Page 21: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi

Code Sample Dynamic Block

Meet Magento

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

Layout File

• parentBlock – name of the parent

block

• dynamicBlockAlias – alias of the

dynamic block in parent block

• dynamicBlockPlaceholder – unique

name of your placeholder

• dynamicBlock – name of the original

dynamic block

• dynamicCookie – name of the cookie

for dynamic blocks

• elementId – HTML ID for the

placeholder div, that is going to be

used as container

Page 22: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi

Available Dynamic Cookies

Meet Magento

• 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

Page 23: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi

How does it work?

Meet Magento

• Your original block gets wrapped by a custom div with some

JS code

• When customer visits a page, JS checks for a cookie value

and compares it with latest saved one in local/session

storage

• If it is different it requests /varnish/ajax/reload for retrieving

dynamic content and saves it to local/session storage

• If it is the same, it just updates block from local/session

storage

Page 24: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi Meet Magento

Adding custom TTL for a page

Page 25: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi

Code Sample Custom TTL

Meet Magento

// Somewhere in your code you just simply call it

// Varnish module will take the lowest value in array of TTL that were added

Mage::helper(‘ecomdev_varnish’)

->addTtl($timeInSeconds);

Code Block

Page 26: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi Meet Magento

Making custom page cacheable

Page 27: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi

Code Sample Custom Page

Meet Magento

<config>

<varnish>

<pages>

<layout_handle_name

translate="label"

module=”your_module">

<label>Your Page Name</label>

</layout_handle_name>

</pages>

</varnish>

<default>

<varnish>

<pages>

<layout_handle_name_time>360</layout_handle_name_time>

</pages>

</varnish>

</default>

</config>

config.xml

• layout_handle_name – full

name of the layout handle that

should be cacheable

• your_module – name of the

module used for translation of

label

• Your Page Name – name of

your pages, that will be shown

in System Configuration -

Varnish Cache

• 360 – default cache lifetime of

the page

Page 28: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi

Varnish vs Full Page Cache

Meet Magento

Varnish

• Avg. time to first byte 30ms

• Dedicated software

• Tools to monitor cache usage

• Scalable

• Requires adaptation of themes for

dynamic parts

• Possibility to flush group of pages

Magento FPC implementation

• Avg. time to first byte 300-400ms

• Magento code level

• N/A

• Only as another backend node

• Most of the time it is not required

• N/A

Page 29: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi Meet Magento

But why do I need to use

EcomDev_Varnish?

Page 30: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi

EcomDev_Varnish vs the others

Meet Magento

• Cache lifetime specified on Magento code level, without

changing VCL

• By using collectors & processors, it can be easily extended

to support additional entities

• Client-side cacheable dynamic parts

• Cache enabled for all kind of visitors

• Saves your money on hardware

Page 31: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi Meet Magento

The choice is up to you!

Page 32: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi Meet Magento

We are hiring!!! Join Us

[email protected]

Page 33: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Ivan Chepurnyi Meet Magento

Thank You!

Page 34: Иван Чепурный - Meet Magento Ukraine - Varnish Cache and its usage in the real world

Questions?

Email: [email protected]

Website: http://www.ecomdev.org

LinkedIn: http://nl.linkedin.com/in/ivanchepurnyi

Twitter: https://twitter.com/IvanChepurnyi