50
Speed up your data caches with Heisencache Frédéric G. Marand

Cache speedup with Heisencache for Drupal 7 and Drupal 8

Embed Size (px)

Citation preview

Page 1: Cache speedup with Heisencache for Drupal 7 and Drupal 8

Speed up your data caches with HeisencacheFrédéric G. Marand

Page 2: Cache speedup with Heisencache for Drupal 7 and Drupal 8

3/59 | heisencache-15D17 | © OSInet

Frederic MARAND

RESULTS

IMPLEMENTATION

MEASURING

CONTEXT

Page 3: Cache speedup with Heisencache for Drupal 7 and Drupal 8

4/59 | heisencache-15D17 | © OSInet

Frederic MARAND

Perception

● Front-end dominates● Downloads start after the page is served● Not all sites are equal when it comes to the

back/front performance ratio

CONTEXT

Page 4: Cache speedup with Heisencache for Drupal 7 and Drupal 8

5/59 | heisencache-15D17 | © OSInet

Frederic MARAND www.webpagetest.org

Major press site example:● simple content● below 500 msec backend● tons of extras on page● result: 23 sec front-end time● +/- 50 * backend time

Front-endCONTEXT

Page 5: Cache speedup with Heisencache for Drupal 7 and Drupal 8

6/59 | heisencache-15D17 | © OSInet

Frederic MARAND

www.webpagetest.org

Major Drupal Commerce site example:● complex backend logic● optimized front-end● +/- 4 * backend time● backend impact = 25%

Back-endCONTEXT

Page 6: Cache speedup with Heisencache for Drupal 7 and Drupal 8

7/59 | heisencache-15D17 | © OSInet

Frederic MARAND

● Level 1: page caching● Opcode cache● Varnish, CDN

● Level 2: storage tuning● MySQL slow queries● MongoDB, Redis, ...

● Level 3: cache tuning● On we go...

CC BY­NC­ND 2.0 najbo/Flickr

Battle planCONTEXT

Page 7: Cache speedup with Heisencache for Drupal 7 and Drupal 8

8/59 | heisencache-15D17 | © OSInet

Frederic MARAND

CONTEXT

LESS DB QUERIES MEANS DATA CACHINGDrupal needs caching

Cache is a good thing

«Cache is king» Steve Souders, author of High-Performance web sites http://fr.slideshare.net/souders/cache-is-king

Page 8: Cache speedup with Heisencache for Drupal 7 and Drupal 8

9/59 | heisencache-15D17 | © OSInet

Frederic MARAND Photo by Alan Light

Too much of a good thing is WONDERFUL

Page 9: Cache speedup with Heisencache for Drupal 7 and Drupal 8

10/59 | heisencache-15D17 | © OSInet

Frederic MARAND

(Not that kind of good thing)

CONTEXT

Page 10: Cache speedup with Heisencache for Drupal 7 and Drupal 8

11/59 | heisencache-15D17 | © OSInet

Frederic MARAND

CONTEXT

CACHING CAN BE BAD

● Memcached speed +/- like good MySQL

● Can be worse: clustering network issues

● A miss costs more than uncached work

Page 11: Cache speedup with Heisencache for Drupal 7 and Drupal 8

12/59 | heisencache-15D17 | © OSInet

Frederic MARAND

CONTEXT

HOW DO YOU KNOW WHEN YOU'VE HAD

ENOUGH?MEASURE!

CC BY­NC­ND 3.0 http://saintgasoline.com

Page 12: Cache speedup with Heisencache for Drupal 7 and Drupal 8

14/59 | heisencache-15D17 | © OSInet

Frederic MARAND

Introducing HEISENCACHE8.x com

ing

Page 13: Cache speedup with Heisencache for Drupal 7 and Drupal 8

15/59 | heisencache-15D17 | © OSInet

Frederic MARAND

RESULTS

IMPLEMENTATION

MEASURING

CONTEXT

Page 14: Cache speedup with Heisencache for Drupal 7 and Drupal 8

16/59 | heisencache-15D17 | © OSInet

Frederic MARAND

MEASURING

WHY HEISENCACHE?

CC BY 3.0 Gerhard Hund

Page 15: Cache speedup with Heisencache for Drupal 7 and Drupal 8

17/59 | heisencache-15D17 | © OSInet

Frederic MARAND

MEASURING

● Reduce uncertainty● Minimize observer impact● Don't push the analogy too far

Page 16: Cache speedup with Heisencache for Drupal 7 and Drupal 8

18/59 | heisencache-15D17 | © OSInet

Frederic MARAND

MEASURING

THE COST OF MEASUREMENT

● Observer code runs within Drupal● Needs to be invoked → more CPU● Needs to store data → more I/O

Page 17: Cache speedup with Heisencache for Drupal 7 and Drupal 8

19/59 | heisencache-15D17 | © OSInet

Frederic MARAND

MEASURING

CACHING BEHAVIOURvs.

LOAD PROFILE

Page 18: Cache speedup with Heisencache for Drupal 7 and Drupal 8

20/59 | heisencache-15D17 | © OSInet

Frederic MARAND

● Instant peaks for top content● Fast decay● TTL works wonders● Long tail issues

MEASURING: Sports site

CC BY 2.0 Ronnie Macdonald

Page 19: Cache speedup with Heisencache for Drupal 7 and Drupal 8

21/59 | heisencache-15D17 | © OSInet

Frederic MARAND

● Many content pieces● Few repeat hits● Complex cache policy

● Avoid caching some data● Preserve cache memory for selected content

MEASURING: Regional TV

Page 20: Cache speedup with Heisencache for Drupal 7 and Drupal 8

22/59 | heisencache-15D17 | © OSInet

Frederic MARAND

MEASURING

OBSERVE CACHE IN PRODUCTION

Page 21: Cache speedup with Heisencache for Drupal 7 and Drupal 8

23/59 | heisencache-15D17 | © OSInet

Frederic MARAND

MEASURING in production

● Precision vs. velocity?● Performance module● Cannot write to the DB in real-time● Cached pages● Observing Drupal bootstrap

Page 22: Cache speedup with Heisencache for Drupal 7 and Drupal 8

24/59 | heisencache-15D17 | © OSInet

Frederic MARAND

RESULTS

IMPLEMENTATION

MEASURING

CONTEXT

Page 23: Cache speedup with Heisencache for Drupal 7 and Drupal 8

25/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATION: early hits

DRUPAL 7 BOOT SEQUENCE● Page cycle: index.php drupal_bootstrap→

– Phases: Configuration, Page Cache, DB, Variables, Session, Page Header, Language, Full

– Cache handlers declaration: settings.php– Exotic early hits : sites.php, settings.php,

drupal_settings_initialize()– Common early hits : _drupal_bootstrap_page_cache

● Need to work before DB and module system

Page 24: Cache speedup with Heisencache for Drupal 7 and Drupal 8

29/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATION: early hits

SOLUTION

D7 : Use a standalone event systemD8 : Use the SF2 EventDispatcher

Page 25: Cache speedup with Heisencache for Drupal 7 and Drupal 8

30/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATION: operation

IDEAS ● Doctrine, NodeJS and Symfony event systems

● Use dependency injection, but no DIC (on D7)

● Use the standard DIC and event system (on D8)

Page 26: Cache speedup with Heisencache for Drupal 7 and Drupal 8

31/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATION: operation

BONUS ● D7 : Composer for deployment– Before hooks, so no

composer_manager● D8 : normal service, easy to

build● Easy unit testing → decent

code coverage

Page 27: Cache speedup with Heisencache for Drupal 7 and Drupal 8

32/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATION: coverage

Page 28: Cache speedup with Heisencache for Drupal 7 and Drupal 8

33/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATIONcache (factory, backend) driver

DECORATOR PATTERN● Original Inspiration: authcache● Read existing cache configuration● D7 : wrap settings, claim to be the sole cache provider● D8 : decorate CacheFactory, CacheBackends● Handle requests per the original configuration, but

enhance the service

Page 29: Cache speedup with Heisencache for Drupal 7 and Drupal 8

34/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATION: events

EMIT EVENTS AROUND ALL OPERATIONS

● Configuration● Initialization● Cache operations● …and page termination

Page 30: Cache speedup with Heisencache for Drupal 7 and Drupal 8

35/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATION: late hits

CATCHING LATE HITS 1/2

● D7 hook_exit ? Lots of code after that

@see drupal_page_footer()● D7 Catching page caching ?

– not triggered on AJAX callbacks @see ajax_deliver()

– poormancron can run lots of code after that

Page 31: Cache speedup with Heisencache for Drupal 7 and Drupal 8

36/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATION: late hits

CATCHING LATE HITS 2/2● D7/D8 Catching the session commit?

– Only if a session was started– Dirty interactions– Session regeneration

● D8 : kernel.terminate, kernel.response ?● => Shutdown function stack

Page 32: Cache speedup with Heisencache for Drupal 7 and Drupal 8

37/59 | heisencache-15D17 | © OSInet

Frederic MARAND

NEEDS ● minimize I/O load → aim for #writes <= 1

● «Fingers crossed»-inspired strategy– Keep data in memory during the

page lifecycle– Write it at end of page

IMPLEMENTATION: storing data

Page 33: Cache speedup with Heisencache for Drupal 7 and Drupal 8

38/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATION: storing data

CHALLENGES ● Writing after the last possible cache operation

● Write while classes are still available

● D7 : Passing information within an event-oriented procedural code base

Page 34: Cache speedup with Heisencache for Drupal 7 and Drupal 8

39/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATION: D7 events

EventEmitter (à la Node)

● narrowcast events created by sources● subscribers add/remove events on the

fly– can further tighten narrowcasting

Page 35: Cache speedup with Heisencache for Drupal 7 and Drupal 8

40/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATION: D7 events

EventSubscriberInterface● Doctrine/Symfony

● + (add | remove)Event()

EventSourceInterface● Define events a source can emit● Base source of events: Cache API

Page 36: Cache speedup with Heisencache for Drupal 7 and Drupal 8

41/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATION: events

D7/D8 SYNTHETIC EVENTS

● API Limitations: post-operation cache events do not get the operation settings

● Enable immediate event reconciliation

● Event susbcribers can also be sources

Page 37: Cache speedup with Heisencache for Drupal 7 and Drupal 8

42/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATION: events

D7/D8 SYNTHETIC EVENT EXAMPLES

● MissSubscriber: miss info for Cache::get()

● PerformanceSubscriber: timing info for all ops

● WriteSubscriber: single event for write and delete ops

Page 38: Cache speedup with Heisencache for Drupal 7 and Drupal 8

44/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATION: setup

settings.heisencache.inc D7 CONFIGURATION

● Retrieve the EventEmitter from Config instance● Create EventSubscriber instances as chosen● Register them on chosen events● DebugSubscriber listen to all events

– not in production !● Don't forget to include a WriterSubscriber● D8 : customize development.services.yml

Page 39: Cache speedup with Heisencache for Drupal 7 and Drupal 8

45/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATION: extending

HEISENCACHE IS A CODER TOOL

Page 40: Cache speedup with Heisencache for Drupal 7 and Drupal 8

46/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATION: extending

EASY TO EXTEND :● Write additional EventSubscriber classes● Add them to your configuration

ALREADY EXTENDED :● WriterSubscriber (France Télévisions)● CacheReadLogWriterSubscriber (AmazeeLabs)● Alternate loader (AmazeeLabs)

Page 41: Cache speedup with Heisencache for Drupal 7 and Drupal 8

47/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATION: extending

MOST TYPICAL● Create new subscriber for custom conditions● Create new writer classes● Target alternate stores for speed and ease

● MongoDB● K/V or data structure store (Redis)● Message queue (Beanstalkd, RabbitMQ,

ZeroMQ, etc)

Page 42: Cache speedup with Heisencache for Drupal 7 and Drupal 8

50/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATION: UI● ROLL YOUR OWN!● Use WatchdogSubscriber data

● Use admin/reports/dblog or rework it

● Views integration● Use the SqlWriterSubscriber data● Two default views provided (Sql, Watchdog)

● Symfony WebProfiler toolbar● Replace/complement existing cache report

Page 43: Cache speedup with Heisencache for Drupal 7 and Drupal 8

51/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATION: UI● Data collecting: raw data, big volume● Three-step data processing

‒ Collect → Heisencache‒ Cook → Process data based on your needs‒ Consumer → Visualize processed data

● Sweet spot‒ Use a queue (Beanstalkd, etc) instead of cron‒ Time-series database for longitudinal analysis :

RRD, InfluxDB, OpenTSDB ...

Page 44: Cache speedup with Heisencache for Drupal 7 and Drupal 8

52/59 | heisencache-15D17 | © OSInet

Frederic MARAND

IMPLEMENTATION: UI

Comparing with network analysis

● Heisencache is like ‒ libpcap / tcpdump / iptrace / snoop ...

● Someone has to design a Wireshark on top of it

Page 45: Cache speedup with Heisencache for Drupal 7 and Drupal 8

53/59 | heisencache-15D17 | © OSInet

Frederic MARAND

RESULTS

IMPLEMENTATION

MEASURING

CONTEXT

Page 46: Cache speedup with Heisencache for Drupal 7 and Drupal 8

54/59 | heisencache-15D17 | © OSInet

Frederic MARAND

RESULTS: instant data

TYPICAL USEFUL INSTANT RESULTS● Repeated misses

– Usual suspects: default Memcached and big writes (prod)

– Rewriting a variable on most pages (dev)● Many calls to same key

– Usual suspect: missing or broken static cache● Many calls to related keys

– Usual suspect: code loop instead of cache multiple

Page 47: Cache speedup with Heisencache for Drupal 7 and Drupal 8

55/59 | heisencache-15D17 | © OSInet

Frederic MARAND

RESULTS: longitudinal analysis

TYPICAL USEFUL TIME-SERIES RESULTS ● Size of known-to-be-growing keys. Usual suspects:

– Translation cache → Someone left a t($foo) somewhere– Context cache → Contexts are piled instead of refactored– Views plugins → Hard Views problem. Partial fix only.

● Miss rate shooting up from baseline on a bin

– Call for instant analysis on that bin: likely a code regression● Response time shooting up on a normally stable key

– Network/server problem, bin saturation– Call for instant analysis on the cache instance

Page 48: Cache speedup with Heisencache for Drupal 7 and Drupal 8

56/59 | heisencache-15D17 | © OSInet

Frederic MARAND

RESULTS : beyond debugging

● Storing cache hit patterns for cache warming● Pre-warm cache on keys selected with a

Heisencache WriterSubscriber● Exemple with the AmazeeLabs fork :

https://github.com/AmazeeLabs/heisencache

Page 49: Cache speedup with Heisencache for Drupal 7 and Drupal 8

58/59 | heisencache-15D17 | © OSInet

Frederic MARAND

Drupal 8?

WHEN?● When bigger sites start to deploy D8● …and need them to go faster :-) ● First steps already available :● https://github.com/FGM/heisencache

Page 50: Cache speedup with Heisencache for Drupal 7 and Drupal 8

59/59 | heisencache-15D17 | © OSInet

Frederic MARAND

Drupal, fasterhttp://www.osinet.fr/