3. Internal Dynamic Page CacheInternal Page Caching
3
Every request constructs the output Saves the whole HTML for a given page,
useful for a website with anonymous
visitors
Caches page components instead of the
whole page, avoids a component to be
computed in every requests if it behaves
the same for all the pages
Cache turned off
Drupal out of the box caching systems
4. 4
Drupal out of the box caching systems
What is cacheable in
Drupal?
—Every renderable element
5. Cache Max-ageCache Contexts
5
• A way of tagging a cacheable element
used for bulk invalidation.
• Used to create cache related elements
• When a cache tag is invalidated, all
elements with that tag are invalidated
• Appliable to all renderable elements
• Custom ones can be created
• Provides a way of storing cache
contextualized with the incoming
request
• Analogous to HTTP's Vary header.
• Provides the ability for different users
to see different cached content
• Appliable to all renderable elements
• Custom ones can be created
• Instructs the time to live of the
renderable element cache
Cacheability Metadata in Drupal
Cache Tags
8. Enhance Drupal with Varnish
8
What is Varnish?
- HTTP Accelerator known as HTTP Reverse
Proxy
- Stores the generated HTML for a given URL
and serves the request without hitting the
webserver
- Speeds up delivery with a factor of 300 -
1000x
9. Enhance Drupal with Varnish
9
Drupal and Varnish
- Installing Drupal Varnish Cache Contrib
module give the ability to integrate with
Cache Tags.
- Drupal + Varnish out-of-the-box work like
Drupal Internal Page Cache
- Same cache for every visitors
Icons made by Freepick from www.flaticon.com
10. Enhance Drupal with Varnish
10
Having Drupal + Varnish Installed
Contextual response example
• Our users have a attribute called “Has car”
• We want to deliver different content to the user based on this
attribute
• Cache contexts won’t work
11. Enhance Drupal with Varnish
11
Means that we cannot serve content
based on the request context?
• Out of the box no, Varnish caches pages based on the URL
• Varnish offers a Vary mechanism to make cache be contextual for
the request (Same logic as Drupal Cache-Contexts)
• Varnish can be instructed by webserver response how the cache
varies using the response Vary header
• Vary header should contain which request headers should be
used to store the cache based on their combinations
• For our example we need to add the request header that contains
the attribute value to make it vary
12. Enhance Drupal with Varnish – Initial state
12
Icons made by Freepick from www.flaticon.com
13. Enhance Drupal with Varnish – Adding the CDN
13
Icons made by Freepick from www.flaticon.com
14. Enhance Drupal with Varnish – Make Info about car available on client-side
14
Icons made by Freepick from www.flaticon.com
15. Enhance Drupal with Varnish – Add the cookie value as a Request header by CDN
15
Icons made by Freepick from www.flaticon.com
16. Enhance Drupal with Varnish – Make cache Vary by X-Has-Car header
16
Icons made by Freepick from www.flaticon.com
17. Enhance Drupal with Varnish - Summary
17
Summary
• Value needed for cache to Vary passed to client side using a
cookie
• Leverage the use of a CDN to transform a cookie into a Request
Header
• Drupal instructs Varnish to Vary cache based on that header
Icons made by Freepick from www.flaticon.com
18. Enhance Drupal with Varnish – Combine with Cache contexts
18
Combine Varnish and Drupal cache
• Cache contexts continue to make sense in reusable components
• Enhances performance on shared components
• Pages in which the content is different from the path should use
the before explained mechanism
19. Enhance Drupal with Varnish
19
Tips to handle cache problems
• Remember that the first hit is always cached
• Setup http.response.debug_cacheability_headers in services.yml
and check X-Drupal-Cache-Contexts
• For debugging Varnish you can pass all the request headers
started to X to the response