Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

PHP Benelux 2017 - Caching The Right Way

414 views

Published on

A look at caching in eZ Platform, and improvements being done to reach a state where it is transparent yet performant.

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

PHP Benelux 2017 - Caching The Right Way

  1. 1. 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
  2. 2. 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
  3. 3. www.ez.no What is it with Caching?
  4. 4. 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
  5. 5. Bit of background of the application I work on:
 eZ Platform
  6. 6. 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
  7. 7. www.ez.no eZ Platform 1.x Architecture
  8. 8. 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
  9. 9. 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
  10. 10. Pinch of theory:
 Where Cache Tagging/Labeling fits
  11. 11. www.ez.no Problem
  12. 12. 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.
  13. 13. 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
  14. 14. Look at real world Solutions:
 Symfony Cache (3.2+)
  15. 15. 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
  16. 16. 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..
  17. 17. 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.
  18. 18. 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.
 

  19. 19. Look at real world Solutions:
 Varnish xkey VMOD (4.1+)
  20. 20. 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
  21. 21. www.ez.no ๏PR in progress to support in FOSHttpCache 2.1 ๏Simple configuration and VCL change to switch from BAN Varnish xkey & FOSHttpCache
  22. 22. Sip of demo time:
 Some code examples, and if time profiling ongoing move to Symfony 3.x (incl Cache Component)
  23. 23. 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
  24. 24. 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
  25. 25. 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

×