PHP Benelux 2017 - Caching The Right Way

25
CACHING THE RIGHT WAY Increase Your Application Performance & Validate It With Profiling André Rømcke (@andrerom) VP Engineering at eZ Systems (@ezsystems) Jan. 28th 2017 - Antwerp - #PHPBenelux 2017

Transcript of PHP Benelux 2017 - Caching The Right Way

Page 1: PHP Benelux 2017 -  Caching The Right Way

CACHING THE R IGHT WAYIncrease Your Application Performance & Validate It With Profiling

André Rømcke (@andrerom)VP Engineering at eZ Systems (@ezsystems)

Jan. 28th 2017 - Antwerp - #PHPBenelux 2017

Page 2: PHP Benelux 2017 -  Caching The Right Way

www.ez.no

Who?

๏André Rømcke | @andrerom

๏Norwegian, mountains, skiing, biking, running, technology, beer, wine, foodie, ..

๏PHP for 11 years. From 96: Html, CSS, JS, VB, C#, PHP, Bash, Groovy, (Hack)

๏Contributed to Symfony, FOS, Stash, Composer, PHP-FIG, Docker, & attempts for PHP

๏VP Engineering at eZ Systems

๏eZ Systems AS | ez.no

๏Global, 70+ people across 7+ countries, partners & community in many many more

๏Maker of eZ Publish since 2001, 6th+ gen called eZ Studio (commercial) & eZ Platform

๏eZ Platform | ezplatform.com

๏“New” open source Content Management System, a super flexible Full & Headless CMS

๏Developed on Symfony (v2.x) Full Stack since 2012, upcoming major on Symfony v3.3

Page 3: PHP Benelux 2017 -  Caching The Right Way

www.ez.no

What is it with Caching?

Page 4: PHP Benelux 2017 -  Caching The Right Way

www.ez.no

So what is it with this talk? And what not?

๏Talk focusing on cache tagging in Varnish and in Symfony Cache

๏It’s benefits, practice and profiling

๏Talk is specific about eZ Platform, and move from a cache approach that

๏This is in this case using, but not going into details on:

๏FOSHttpCache: See David Buchmann’s talk for that at 11:40

๏PSR-6: See Hannes Van De Vreken’s talk on that at 14:50

Page 5: PHP Benelux 2017 -  Caching The Right Way

Bit of background of the application I work on:eZ Platform

Page 6: PHP Benelux 2017 -  Caching The Right Way

www.ez.no

eZ Platform 1.x: Overview

๏6th generation of eZ Publish, a CMS that has been around since 2001

๏Several large users around the world

๏Focus on being very extendable so it can be used in many cases

๏A tree based content model, we’ll come back to this

๏So old but “new” in the sense that it is a new product rewritten on Symfony

๏So more modern, but with less features

Page 7: PHP Benelux 2017 -  Caching The Right Way

www.ez.no

eZ Platform 1.x Architecture

Page 8: PHP Benelux 2017 -  Caching The Right Way

www.ez.no

eZ Platform 1.x: Persistance Cache

๏Currently built using Stash cache

๏A hierarchal cache, allowing you to clear trees of cache items

๏Benefit 1: Easy to get started

๏Problem 1: End up clearing to much cache

๏Problem 2: Many lookups to backend to resolve items

Page 9: PHP Benelux 2017 -  Caching The Right Way

www.ez.no

eZ Platform 1.x: HttpCache

๏Uses FOSHttpCache[Bundle] 1.x

๏Lets you vary cache by user context (rights)

๏Lets you tag the response and BAN based on this

๏Problem 1: BAN on Varnish does not support grace

๏Problem 2: Multiple tags only on Varnish

Page 10: PHP Benelux 2017 -  Caching The Right Way

Pinch of theory:Where Cache Tagging/Labeling fits

Page 11: PHP Benelux 2017 -  Caching The Right Way

www.ez.no

Problem

Page 12: PHP Benelux 2017 -  Caching The Right Way

www.ez.no

Problem

Examples:

๏Data cache (PSR-6, ..):

๏Entity can be present on several cache keys

๏eg: Item lookup by id or by identifier/remote-id, in listings, ..

๏Reverse Proxy (Varnish with FosHttpCache, ..):

๏Entity can be rendered on several different pages (url ~= key)

๏And url won’t represent the unity when it’s a “fragment” of the page

๏It can also be, or not be, on a variant of a page depending on user rights

Result: Need for knowledge between Cache items and Commands/Actions affecting it.

Page 13: PHP Benelux 2017 -  Caching The Right Way

www.ez.no

Solution: Tags/Labels for all

๏Data cache (PSR-6, ..):

๏key: item-66 tags: item-66

๏key: item-identifier-phpbenelux tags: item-66

๏key: item-list-type-article tags: item-66, item-44, (…)

๏Reverse Proxy (Varnish with FosHttpCache, ..):

๏url: Home/Articles/PHPBenelux tags: item-66

๏url: Home/Articles tags: item-23, item-66, (..)

๏url: api/item/66 tags: item-66

๏url: api/item/66?include=parent.name tags: item-66, item-23

Page 14: PHP Benelux 2017 -  Caching The Right Way

Look at real world Solutions: Symfony Cache (3.2+)

Page 15: PHP Benelux 2017 -  Caching The Right Way

www.ez.noez.no

Symfony Cache Component

๏ PSR-6 Compliant cache component

๏Aims to be fast, made by among others Blackfire CTO: Nicolas Grekas

๏ Supports multi get calls to Redis and Memcached

๏ Provides several built in cache adapters by default

๏ Is progressively being used in several places in Symfony Framework, e.g.:

๏ PropertyInfo

๏ Serializer

๏Validator

๏ (…)

๏ .. And hopefully HTTP Cache at some point

Page 16: PHP Benelux 2017 -  Caching The Right Way

www.ez.noez.no

Symfony Cache Adapters

๏Adapters:

๏APCu (per proces cache)

๏Array (in memory per request, mainly for testing)

๏Chain (chain several adapters after each-other)

๏Doctrine

๏ FileSystem (Also PHPFile and PHPArray implementation for immutable opcache cache)

๏ Proxy (To reuse other PSR-6 implementations)

๏Redis

๏Memcached

๏And TagAware..

Page 17: PHP Benelux 2017 -  Caching The Right Way

www.ez.noez.no

Symfony Cache Backend Lookups

Stash Cache* Doctrine Cache** Symfony Cache***

loadContenetInfo(66) 5 2 1

loadContenet(66) 6 2 1

findContent($query)10 items 42 2 1

*** With native adapters,

not Doctrine.

* memcached/redis, to compute keys

for hierarchical cache.

** With versioning.

Page 18: PHP Benelux 2017 -  Caching The Right Way

www.ez.noez.no

Cache Backend latency with Stash

๏AWS ElasticCache Redis instances has latency of:

๏On larger EC2 instances: 0.2-0.5ms

๏On micro/small instances: Apparently much more

๏ This means when proxy cache is cold:

๏ Simple page with 20 items shown:

๏ ~82 lookups x latency = 16-41 ms

๏ Large landing page with 200-1000 items shown:

๏ ~8.000-40.000 lookups x latency = 1.6-20 seconds

๏ In theory Symfony cache should get a 4th of that

๏And much less if its support for multiple get is taken advantage of.

Page 19: PHP Benelux 2017 -  Caching The Right Way

Look at real world Solutions: Varnish xkey VMOD (4.1+)

Page 20: PHP Benelux 2017 -  Caching The Right Way

www.ez.no

๏Formally: Surrogate keys, Hash Ninja, Secondary Hash/Hash-Two

๏Dedicated secondary hashes to objects

๏Much more efficient for tag purging then BAN is

๏Allows for expiring objects with softpurge() for use with grace

๏Deliver stale data while refresh happens in the background

๏Part of official `varnish-modules` VMOD collection package

๏Already part of Ubuntu, upcoming Debian and probably RHEL

Varnish xkey VMOD

Page 21: PHP Benelux 2017 -  Caching The Right Way

www.ez.no

๏PR in progress to support in FOSHttpCache 2.1

๏Simple configuration and VCL change to switch from BAN

Varnish xkey & FOSHttpCache

Page 22: PHP Benelux 2017 -  Caching The Right Way

Sip of demo time: Some code examples, and if time profiling ongoing move to Symfony 3.x (incl Cache Component)

Page 23: PHP Benelux 2017 -  Caching The Right Way

www.ez.no

๏For pure move from Stash to Symfony Cache just using file system as cache

๏With Reverse proxy disabled to hit backend

Demo Profiling Result

Page 24: PHP Benelux 2017 -  Caching The Right Way

www.ez.no

๏Profiling should have been done using Redis/Memcached on AWS setup

๏20% improvements would have been many times more with latency

๏The profiling was done with Blackfire,

๏For your own use you can also use xdebug profiling, tideways.io, (..)

Demo Profiling Retrospective

Page 25: PHP Benelux 2017 -  Caching The Right Way

The End, Questions?

This talk: https://joind.in/talk/66ef8 Other talks: http://www.slideshare.net/andreromcke Twitter: @andrerom FOSHttpCache: http://foshttpcache.readthedocs.io/en/latest/ Varnish xkey: https://github.com/varnish/varnish-modules/blob/master/docs/vmod_xkey.rst