SlideShare a Scribd company logo
Drupal 8 Caching
Subhash Yadav
https://www.drupal.org/u/subhashuyadav
A cache is a stored copy of a resource so future requests for that
resource can be served faster.
The data stored in a cache might be the result of an earlier
computation, or the duplicate of data stored elsewhere.
Caches are found at every level of a content's journey from the
original server to the browser.
What is cache?
Why Caching?
● Faster than recomputing a result or reading from a slower
data store; thus the, more requests can be served from the
cache, the faster the system performs i.e. maximum output
with minimum latency.
● Eases the load of the server
● Improves responsiveness
HTTP Headers
HTTP Headers is where caching information is exchanged between the origin
server and cdn, proxies including varnish all the way to the browser.
Example : HTTP/1.x 200 OK
Transfer-Encoding: chunked
Date: Fri, 27 Nov 2017 04:36:25 GMT
Server: LiteSpeed
Connection: close
X-Powered-By: W3 Total Cache/0.8
Expires: Fri, 27 Nov 2017 05:36:25 GMT
Etag: "pub1259380237;gz"
Cache-Control: max-age=3600, public
Content-Type: text/html; charset=UTF-8
Last-Modified: Sat, 28 Nov 2009 03:50:37 GMT
Content-Encoding: gzip
Vary: Accept-Encoding, Cookie, User-Agent
The Expires header sets a time in the future
when the content will expire. This header is
probably best used only as a fall back.
Expires
A unique hash identifier of the cacheable object that the browser
caches.
Whenever a cached response is requested again, browser checks
with server if the eTag of the response is modified. If response is
not modified server returns 304 not modified else 200 along
with new response & new eTag.
Etag
The date-time this cacheable object was last cached.
(Similar to eTag)
Last-modified
When a cache receives a request that can be satisfied by a
cached response that has a Vary header field, it must not use
that cached response unless all header fields as nominated
by the Vary header match in both the original (cached)
request and the new request.
This can be useful for serving content dynamically.
Vary
Each resource can define its caching policy via the
Cache-Control HTTP header.
Cache-Control directives control who can cache the
response, under which conditions, and for how long.
Cache-Control
no-cache: Indicates that the returned response can't be used to satisfy a
subsequent request to the same URL without first checking with the
server if the response has changed.
no-store: It simply disallows the browser and all intermediate caches
from
storing any version of the returned response.
public / private: If the response is marked as "public", then it can be cached, even
if
it has HTTP authentication associated with it, and even when the
response status code isn't normally cacheable.
private marked responses are typically intended for
a single user, so
an intermediate cache is not allowed to cache them but browser can
cache them.
Cache-Control Directives
max-age : Configures the maximum age that the content may be cached
before
it must revalidate or re-download the content from the origin server.
This replaces the Expires header for modern browsing.
This option takes its value in seconds with a maximum valid
freshness time of one year (31536000 seconds).
s-maxage : This is very similar to the max-age setting, in that it indicates the
amount of time that the content can be cached.
The difference is that this option is applied only to intermediary
caches.
Cache-Control Directives
All things that either are directly renderable or are used to determine
what to render provide cacheability metadata.
Cacheability metadata consists of three properties:
cache tags : For dependencies on data managed by
Drupal, like
entities & configuration.
cache contexts : For variations, i.e. dependencies on the request
context.
cache max-age : For time-sensitive caching, i.e. time dependencies
Cacheability metadata
Cache tags provide a declarative way to track which cache items
depend on some data managed by Drupal.
The role of the tags is to identify cache items across multiple bins for
proper invalidation.
In a typical scenario, a user may have modified a node that appears in
two views, three blocks, and on twelve pages. Without cache tags, we
couldn't know which cache items to invalidate. So we'd have to
invalidate everything and sacrifice effectiveness to achieve
correctness. With cache tags we can have both.
Cache Tags
Syntax: “Thing:identifier”
Example: node:5 -- cache tag for node entity 5
user:4 -- cache tag for user entity 4
node_list -- list cache tag for node
$tags = array('node:1', 'user:7');
Drupal::cache()->set($cid, $data,
CacheBackendInterface::CACHE_PERMANENT, $tags);
// Invalidate all cache items with certain tags.
DrupalCoreCacheCache::invalidateTags($tags);
Cache Tags
● Cache contexts provide a declarative way to create context-
dependent variations of something that needs to be cached.
● Determines how to vary items according to request.
● Similar to D7 block constants like DRUPAL_NO_CACHE /
DRUPAL_CACHE_PER_ROLE / DRUPAL_CACHE_PER_PAGE,
but with many more options.
● Cache contexts are hierarchical in nature.
Cache Contexts
Example : theme -- vary by negotiated theme.
user.roles -- vary by the combination of
roles
$variables['#cache']['contexts'][] = 'url.path';
Cache Contexts
Cache max-age
● Cache max-age provides a declarative way to create time-
dependent caches.
● Controls how long an item may be cached by number of seconds.
● 0 means cacheable for zero seconds, i.e. not cacheable
● DrupalCoreCacheCache::PERMANENT means cacheable
forever, i.e. this will only ever be invalidated due to cache tags.
(In other words: ∞, or infinite seconds)
● Varnish is just a web-server in front of origin webserver.
● Also known as a caching HTTP reverse proxy.
● It can be used with cache tags to make cache invalidation easy.
● Every request to the origin web-server goes through varnish.
● If varnish doesn’t have a request cached, the request is passed to
the backend. Upon response, varnish reads the response
headers and if response is cacheable varnish caches the
response for next similar requests.
Varnish cache
● Varnish will only cache resources that are requested through an
idempotent HTTP verb, which are the verbs that do not change
the state of the resources.
● HTTP verbs that Varnish can handle: GET, HEAD
● PUT, POST, DELETE, TRACE, OPTIONS cannot be cached by
the varnish.
● Varnish default cache setting is 120 sec (2 min).
Varnish cache
You need to do three things to make sure Varnish works well with the
cache tags generated by Drupal:
● Update your Varnish VCL so it handles BAN requests properly.
Location : /etc/default/varnish/default.vcl
http://foshttpcache.readthedocs.io/en/stable/varnish-
configuration.html#tagging
Varnish cache
Varnish cache
● Send a cache tags header:
Drupal's Purge module automatically configures
Purge-Cache-Tags headers.
● Send a BAN request when content or configuration changes:
Add an HTTP Purger using Generic HTTP Purger module.
Configure a cron job to process the Purge queue.
Thank you

More Related Content

PPTX
How to reduce database load using Memcache
PPTX
Performance Optimization in Drupal 8
PPTX
Drupal performance optimization Best Practices
PDF
High Performance - Joomla!Days NL 2009 #jd09nl
PDF
WordCamp RVA 2011 - Performance & Tuning
PPTX
WordPress Hosting Best Practices - Do's and Don't s | WordPress Trivandrum
PPT
Implementing High Performance Drupal Sites
PPT
How to reduce database load using Memcache
Performance Optimization in Drupal 8
Drupal performance optimization Best Practices
High Performance - Joomla!Days NL 2009 #jd09nl
WordCamp RVA 2011 - Performance & Tuning
WordPress Hosting Best Practices - Do's and Don't s | WordPress Trivandrum
Implementing High Performance Drupal Sites

What's hot (20)

PPTX
Presentation1
PPTX
RESTful Web services in Drupal 8
PDF
Php & web server performace
PPTX
Understanding Web Cache
PPT
World Wide Web Caching
PPT
Drupalcamp Estonia - High Performance Sites
PPT
Drupal caching
PPT
Performance Optimization using Caching | Swatantra Kumar
ODP
Caching Strategies
PDF
23 Ways To Speed Up WordPress
PDF
Sofia WP User Group Presentation
PPTX
PPTX
Insight on MongoDB Change Stream - Abhishek.D, Mydbops Team
PPTX
Mini-Training: To cache or not to cache
PDF
Memcached: What is it and what does it do? (PHP Version)
KEY
Memcached: What is it and what does it do?
PPTX
Артем Сильчук - Respond in 60ms. Extremal optimization with reinventing a wheel
PDF
Caching with Ruby
PPTX
Cloud Hosting Services
PDF
Postgres connections at scale
Presentation1
RESTful Web services in Drupal 8
Php & web server performace
Understanding Web Cache
World Wide Web Caching
Drupalcamp Estonia - High Performance Sites
Drupal caching
Performance Optimization using Caching | Swatantra Kumar
Caching Strategies
23 Ways To Speed Up WordPress
Sofia WP User Group Presentation
Insight on MongoDB Change Stream - Abhishek.D, Mydbops Team
Mini-Training: To cache or not to cache
Memcached: What is it and what does it do? (PHP Version)
Memcached: What is it and what does it do?
Артем Сильчук - Respond in 60ms. Extremal optimization with reinventing a wheel
Caching with Ruby
Cloud Hosting Services
Postgres connections at scale
Ad

Similar to Caching in Drupal 8 (20)

PPTX
The Most Frequently Used Caching Headers
PPTX
cache concepts and varnish-cache
PPTX
[Hanoi-August 13] Tech Talk on Caching Solutions
PPTX
Varnish –Http Accelerator
PDF
Content Caching with NGINX and NGINX Plus
PDF
Browser Caching
PPTX
Ror caching
PPTX
Basic Caching Terminology
PDF
Caching on the web
PPTX
Http caching
PPTX
Http Caching for the Android Aficionado
PPT
Web site optimization
PPT
Web Site Optimization
PPTX
Caching
PDF
Nginx dhruba mandal
PPTX
Caching in drupal
PPTX
Academy PRO: HTML5 Data storage
PPTX
Cdn technology overview
PPT
Drupalcamp Estonia - High Performance Sites
The Most Frequently Used Caching Headers
cache concepts and varnish-cache
[Hanoi-August 13] Tech Talk on Caching Solutions
Varnish –Http Accelerator
Content Caching with NGINX and NGINX Plus
Browser Caching
Ror caching
Basic Caching Terminology
Caching on the web
Http caching
Http Caching for the Android Aficionado
Web site optimization
Web Site Optimization
Caching
Nginx dhruba mandal
Caching in drupal
Academy PRO: HTML5 Data storage
Cdn technology overview
Drupalcamp Estonia - High Performance Sites
Ad

More from valuebound (20)

PDF
Scaling Drupal for High Traffic Websites
PDF
Drupal 7 to Drupal 10 Migration A Fintech Strategic Blueprint (1).pdf
PDF
How to Use DDEV to Streamline Your Drupal Development Process.
PDF
How to Use AWS to Automate Your IT Operation| Valuebound
PDF
How to Use Firebase to Send Push Notifications to React Native and Node.js Apps
PDF
Mastering Drupal Theming
PDF
The Benefits of Cloud Engineering
PDF
Cloud Computing
PDF
The Future of Cloud Engineering: Emerging Trends and Technologies to Watch in...
PDF
Deep dive into ChatGPT
PDF
Content Creation Solution | Valuebound
PPTX
Road ahead for Drupal 8 contributed projects
PPTX
Chatbot with RASA | Valuebound
PDF
Drupal and Artificial Intelligence for Personalization
PPTX
Drupal growth in last year | Valuebound
PPTX
BE NEW TO THE WORLD "BRAVE FROM CHROME"
PPTX
Event loop in browser
PPTX
The Basics of MongoDB
PPTX
React JS: A Secret Preview
PPTX
Dependency Injection in Drupal 8
Scaling Drupal for High Traffic Websites
Drupal 7 to Drupal 10 Migration A Fintech Strategic Blueprint (1).pdf
How to Use DDEV to Streamline Your Drupal Development Process.
How to Use AWS to Automate Your IT Operation| Valuebound
How to Use Firebase to Send Push Notifications to React Native and Node.js Apps
Mastering Drupal Theming
The Benefits of Cloud Engineering
Cloud Computing
The Future of Cloud Engineering: Emerging Trends and Technologies to Watch in...
Deep dive into ChatGPT
Content Creation Solution | Valuebound
Road ahead for Drupal 8 contributed projects
Chatbot with RASA | Valuebound
Drupal and Artificial Intelligence for Personalization
Drupal growth in last year | Valuebound
BE NEW TO THE WORLD "BRAVE FROM CHROME"
Event loop in browser
The Basics of MongoDB
React JS: A Secret Preview
Dependency Injection in Drupal 8

Recently uploaded (20)

PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Approach and Philosophy of On baking technology
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Spectroscopy.pptx food analysis technology
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PPTX
cloud_computing_Infrastucture_as_cloud_p
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Building Integrated photovoltaic BIPV_UPV.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Reach Out and Touch Someone: Haptics and Empathic Computing
Approach and Philosophy of On baking technology
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Machine learning based COVID-19 study performance prediction
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
NewMind AI Weekly Chronicles - August'25-Week II
Assigned Numbers - 2025 - Bluetooth® Document
Heart disease approach using modified random forest and particle swarm optimi...
Empathic Computing: Creating Shared Understanding
Spectral efficient network and resource selection model in 5G networks
MIND Revenue Release Quarter 2 2025 Press Release
Spectroscopy.pptx food analysis technology
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Univ-Connecticut-ChatGPT-Presentaion.pdf
cloud_computing_Infrastucture_as_cloud_p

Caching in Drupal 8

  • 1. Drupal 8 Caching Subhash Yadav https://www.drupal.org/u/subhashuyadav
  • 2. A cache is a stored copy of a resource so future requests for that resource can be served faster. The data stored in a cache might be the result of an earlier computation, or the duplicate of data stored elsewhere. Caches are found at every level of a content's journey from the original server to the browser. What is cache?
  • 3. Why Caching? ● Faster than recomputing a result or reading from a slower data store; thus the, more requests can be served from the cache, the faster the system performs i.e. maximum output with minimum latency. ● Eases the load of the server ● Improves responsiveness
  • 4. HTTP Headers HTTP Headers is where caching information is exchanged between the origin server and cdn, proxies including varnish all the way to the browser. Example : HTTP/1.x 200 OK Transfer-Encoding: chunked Date: Fri, 27 Nov 2017 04:36:25 GMT Server: LiteSpeed Connection: close X-Powered-By: W3 Total Cache/0.8 Expires: Fri, 27 Nov 2017 05:36:25 GMT Etag: "pub1259380237;gz" Cache-Control: max-age=3600, public Content-Type: text/html; charset=UTF-8 Last-Modified: Sat, 28 Nov 2009 03:50:37 GMT Content-Encoding: gzip Vary: Accept-Encoding, Cookie, User-Agent
  • 5. The Expires header sets a time in the future when the content will expire. This header is probably best used only as a fall back. Expires
  • 6. A unique hash identifier of the cacheable object that the browser caches. Whenever a cached response is requested again, browser checks with server if the eTag of the response is modified. If response is not modified server returns 304 not modified else 200 along with new response & new eTag. Etag
  • 7. The date-time this cacheable object was last cached. (Similar to eTag) Last-modified
  • 8. When a cache receives a request that can be satisfied by a cached response that has a Vary header field, it must not use that cached response unless all header fields as nominated by the Vary header match in both the original (cached) request and the new request. This can be useful for serving content dynamically. Vary
  • 9. Each resource can define its caching policy via the Cache-Control HTTP header. Cache-Control directives control who can cache the response, under which conditions, and for how long. Cache-Control
  • 10. no-cache: Indicates that the returned response can't be used to satisfy a subsequent request to the same URL without first checking with the server if the response has changed. no-store: It simply disallows the browser and all intermediate caches from storing any version of the returned response. public / private: If the response is marked as "public", then it can be cached, even if it has HTTP authentication associated with it, and even when the response status code isn't normally cacheable. private marked responses are typically intended for a single user, so an intermediate cache is not allowed to cache them but browser can cache them. Cache-Control Directives
  • 11. max-age : Configures the maximum age that the content may be cached before it must revalidate or re-download the content from the origin server. This replaces the Expires header for modern browsing. This option takes its value in seconds with a maximum valid freshness time of one year (31536000 seconds). s-maxage : This is very similar to the max-age setting, in that it indicates the amount of time that the content can be cached. The difference is that this option is applied only to intermediary caches. Cache-Control Directives
  • 12. All things that either are directly renderable or are used to determine what to render provide cacheability metadata. Cacheability metadata consists of three properties: cache tags : For dependencies on data managed by Drupal, like entities & configuration. cache contexts : For variations, i.e. dependencies on the request context. cache max-age : For time-sensitive caching, i.e. time dependencies Cacheability metadata
  • 13. Cache tags provide a declarative way to track which cache items depend on some data managed by Drupal. The role of the tags is to identify cache items across multiple bins for proper invalidation. In a typical scenario, a user may have modified a node that appears in two views, three blocks, and on twelve pages. Without cache tags, we couldn't know which cache items to invalidate. So we'd have to invalidate everything and sacrifice effectiveness to achieve correctness. With cache tags we can have both. Cache Tags
  • 14. Syntax: “Thing:identifier” Example: node:5 -- cache tag for node entity 5 user:4 -- cache tag for user entity 4 node_list -- list cache tag for node $tags = array('node:1', 'user:7'); Drupal::cache()->set($cid, $data, CacheBackendInterface::CACHE_PERMANENT, $tags); // Invalidate all cache items with certain tags. DrupalCoreCacheCache::invalidateTags($tags); Cache Tags
  • 15. ● Cache contexts provide a declarative way to create context- dependent variations of something that needs to be cached. ● Determines how to vary items according to request. ● Similar to D7 block constants like DRUPAL_NO_CACHE / DRUPAL_CACHE_PER_ROLE / DRUPAL_CACHE_PER_PAGE, but with many more options. ● Cache contexts are hierarchical in nature. Cache Contexts
  • 16. Example : theme -- vary by negotiated theme. user.roles -- vary by the combination of roles $variables['#cache']['contexts'][] = 'url.path'; Cache Contexts
  • 17. Cache max-age ● Cache max-age provides a declarative way to create time- dependent caches. ● Controls how long an item may be cached by number of seconds. ● 0 means cacheable for zero seconds, i.e. not cacheable ● DrupalCoreCacheCache::PERMANENT means cacheable forever, i.e. this will only ever be invalidated due to cache tags. (In other words: ∞, or infinite seconds)
  • 18. ● Varnish is just a web-server in front of origin webserver. ● Also known as a caching HTTP reverse proxy. ● It can be used with cache tags to make cache invalidation easy. ● Every request to the origin web-server goes through varnish. ● If varnish doesn’t have a request cached, the request is passed to the backend. Upon response, varnish reads the response headers and if response is cacheable varnish caches the response for next similar requests. Varnish cache
  • 19. ● Varnish will only cache resources that are requested through an idempotent HTTP verb, which are the verbs that do not change the state of the resources. ● HTTP verbs that Varnish can handle: GET, HEAD ● PUT, POST, DELETE, TRACE, OPTIONS cannot be cached by the varnish. ● Varnish default cache setting is 120 sec (2 min). Varnish cache
  • 20. You need to do three things to make sure Varnish works well with the cache tags generated by Drupal: ● Update your Varnish VCL so it handles BAN requests properly. Location : /etc/default/varnish/default.vcl http://foshttpcache.readthedocs.io/en/stable/varnish- configuration.html#tagging Varnish cache
  • 21. Varnish cache ● Send a cache tags header: Drupal's Purge module automatically configures Purge-Cache-Tags headers. ● Send a BAN request when content or configuration changes: Add an HTTP Purger using Generic HTTP Purger module. Configure a cron job to process the Purge queue.