Building the Fastest Drupal in the Galaxy - Mateu Aguilo

Embed Size (px)

Citation preview

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    1/37

    BUILDING THE

    FASTEST

    DRUPAL OF THEGALAXY

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    2/37

    Hello!

    I AM MATEU AGUILÓI am a senior developer at Lullabot

    You can find me at @e0ipso

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    3/37

    Hi!

    I AM PEDRO GONZÁLEZI am a sysadmin at sbit.io

    You can find me at @NITEMAN_es

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    4/37

    DISCLAIMERThese are our experiences and what

    we learned so far.

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    5/37

    Forget Drupal < 8Why?

    Drupal’s 1.001 cachesHow?

    Performance by design

    What?

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    6/37

    Drupal’s 1.001

    caches

    Drupal 8’s cache system

    overview

    Swappablecache

    backends

    Cachestrategy

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    7/37

    SWAPPABLE CACHE BACKENDS

    MemCache

    Redis

    Database

    etc.

    CacheBackendInterface

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    8/37

    CACHE STRATEGY I: How to cache

    cache it permanently

    avoid doing the work at all

    avoid doing the work during the critical

    path

    defer executing it after the main content

    cache it temporarily

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    9/37

    “  There are only two hard things inComputer Science: cacheinvalidation and naming things.

    — Phil Karlton

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    10/37

    CACHE STRATEGY II:

    Cache hit ratio

    Cache invalidation complexity

    Content as current as possible

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    11/37

    Batteries includedDrupal 8 comes with cachingenabled by default

    PageCache

    DynamicPageCache

    https://www.drupal.org/documentation/modules/internal_page_cachehttps://www.drupal.org/documentation/modules/dynamic_page_cachehttps://www.drupal.org/documentation/modules/internal_page_cachehttps://www.drupal.org/documentation/modules/dynamic_page_cachehttps://www.drupal.org/documentation/modules/internal_page_cachehttps://www.drupal.org/documentation/modules/dynamic_page_cachehttps://www.drupal.org/documentation/modules/dynamic_page_cachehttps://www.drupal.org/documentation/modules/internal_page_cachehttps://www.drupal.org/documentation/modules/internal_page_cache

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    12/37

    Old good Page Cache: Upgraded!

    Almost a “poor man’s Varnish”

    1

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    13/37

    Pros

    ◦ It’s super fast

    ◦ It shortcuts

    bootstrap◦ It’s URL based

    ◦ New! Instantly

    updated when

    something ischanged

    PAGE CACHE

    Cons

    ◦ Only for

    anonymous users

    ◦ Assumes pages areidentical for all

    anonymous users

    ◦ Does not use

    Authentication API◦ Poor extensibility

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    14/37

    Dynamic Page Cache

    Built upon render cache, powered by the cacheability

    metadata.

    Personalized parts are excluded automatically: they are

    turned into placeholders.

    2

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    15/37

    Pros

    ◦ Still quite fast

    ◦ Works for all users

    ◦ Personalized partsare turned into

    placeholders

    automatically

    ◦ Instantly updatedwhen something is

    changed

    DYNAMIC PAGE CACHE

    Cons

    ◦ Slower than Page

    Cache

    ◦ Require devs to beaware of it

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    16/37

    Render API:

    CachingDrupal needs to be aware of how dynamic yourcode is.

    metadata placeholders

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    17/37

    Cacheability Metadata

    Keep Drupal informed about dependencies

    1

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    18/37

    CACHE TAGSAvoid having stale content by using

    the appropriate cache tags.

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    19/37

    CACHECONTEXTSHave different versions of a cache

    entry depending on the context

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    20/37

    MAX-AGEHow old can your cache entry be

    before it’s considered stale

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    21/37

    Drupal 8 requires

    developers, tothink about

    caching

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    22/37

    Mindset

    If it varies depending on the situation, I'll use

    contexts

    I’ll always think about cacheability whenrendering anything

    If it’s expensive I’ll cache it using cache keys

    If it may become stale I’ll use max-age

    If anything will cause it to be outdated I’ll use

    tags

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    23/37

    EXAMPLEOf cacheability metadata

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    24/37

    A “REAL LIFE” PROBLEM

    First node in the site:

    ◦ A block in the sidebar

    ◦ Contains a greeting to the user

    You want this block to be cached!

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    25/37

    Cache TagsThe first node

    Max-AgePermanent

    Cache ContextThe user display name

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    26/37

    Common PitfallsWhen caches go sour

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    27/37

    BUBBLINGParents get children’s cacheability

    metadata

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    28/37

    Cacheability metadata in the Black box is surfaced tothe Maroon one, and then to the Pink one, and then tothe Orange one, and then to the Blue one.

    The Blue one (page) contains all that, plus the Green box, etc. Every box inherits and adds its own.

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    29/37

    Placeholdering

    Drupal’s learning magic

    2

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    30/37

    PLACEHOLDERING FOR LAZY BUILDING

    Automatic - Manual?

    Enables Small & Big Pipes

    It all comes down to setting:

    #create_placeholder = TRUE

    When there is:

    #lazy_builder = […, …]

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    31/37

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    32/37

    Manually

    Via the alter

    hooks:

    ◦ hook_block_build◦ …

    SETTING #create_placeholder to TRUE

    Automagically

    Detects

    configured

    conditions incache metadata.

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    33/37

    Perceived

    performanceHow all we have seen will

    make Drupal feels faster

    Today:BigPipe

    Tomorrow:RefreshLess

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    34/37

    ◦ A placeholdering strategy.

    ◦ Applies when there is a session

    ◦ Works with and without

     javascript.

    Big Pipe2.1

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    35/37

    RefreshLess

    Inspired by RoR’s turbolinks.

    2.2

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    36/37

    “ For us, the caching system alone

     justifies choosing the Drupal 8

    platform

    — NITEMAN & e0ipso

  • 8/16/2019 Building the Fastest Drupal in the Galaxy - Mateu Aguilo

    37/37

    Thanks!

    ANY QUESTIONS?You can find me at

    @e0ipso

    @NITEMAN_es