Caching System - Cache Contexts

Cache Contexts in Drupal define when cached content should vary based on request conditions.

If Cache Tags answer “when to invalidate”, Cache Contexts answer “when to create different versions of cache”.

In enterprise systems, different users may see different content on the same URL. Cache Contexts ensure Drupal serves the correct variation without disabling caching.

This enables:

  • personalized yet cached experiences
  • correct content per user, role, or language
  • high cache hit rate with safe variation

Senior Drupal developers treat Cache Contexts as the variation layer of caching.


Core Concept

A Cache Context is a dimension that changes the cached output.

Basic flow:

Request
   ↓
Evaluate Contexts (e.g., user.role, url, language)
   ↓
Select Cache Variant
   ↓
Return Cached Response

If the context changes → Drupal uses a different cache entry.


Common Cache Contexts

Drupal provides many built-in contexts:

user
user.roles
user.permissions
url
url.path
url.query_args
languages:language_interface
languages:language_content
route
timezone

Examples:

  • user.roles → different output for admin vs anonymous
  • url.path → different output per page
  • languages:language_interface → different language UI

Cache Contexts Architecture Diagram

Same URL
   ↓
Context: user.role

Anonymous → Cache A
Authenticated → Cache B
Admin → Cache C

Drupal stores multiple cached variants keyed by contexts.


Scenario: A banner shows admin controls for editors but not for public users.

Context: user.roles

Result:

  • Anonymous users → clean banner
  • Editors → banner with edit controls

Both are cached separately → no performance loss.


Decision Framework

Use Cache Contexts when:

  • output varies by user, role, or language
  • content depends on URL or route
  • personalization is required

Avoid misuse when:

  • adding too many contexts (cache fragmentation)
  • using contexts where tags or max-age are enough

Developer Usage (Code Example)

Add cache contexts to a render array:

$build['#cache']['contexts'] = ['user.roles'];

Multiple contexts:

$build['#cache']['contexts'] = [
  'user.roles',
  'url.path',
  'languages:language_interface'
];

Integration with Views

Views automatically apply contexts based on configuration.

Example:

View: Personalized Dashboard
Context: user

Each user sees their own cached version.


Frontend / React Mapping

Drupal Cache Contexts
   ↓
API Response varies (e.g., user role, language)
   ↓
CDN caches per variation key
   ↓
React UI renders correct version

For headless apps, include context signals (headers, params) to vary responses safely.


Platform / DevOps Layer

Contexts influence edge caching behavior:

  • CDN (CloudFront/Akamai) may vary by headers (e.g., language)
  • Reverse proxies (Varnish) respect cache keys
  • Redis stores multiple variants

Example:

Request (Accept-Language: es)
   ↓
Cache Context: language
   ↓
Serve Spanish cached page

In CI/CD:

  • avoid blanket cache clears
  • ensure context-aware invalidation + warmups

Performance Considerations

Best practices:

  • use the minimum necessary contexts
  • prefer broader contexts (e.g., user.roles over user when possible)
  • combine with tags and max-age

Risks:

  • too many contexts → many cache variants → lower hit rate

Senior rule:

Balance personalization with cache efficiency.


SEO / Accessibility Considerations

Ensure contexts don’t break:

  • canonical URLs
  • language-specific pages (hreflang)
  • consistent metadata per variant

Common Production Issues

  • missing contexts → wrong content shown to users
  • overusing contexts → cache fragmentation
  • mixing user-specific data without context → data leaks
  • CDN not configured to vary by required headers

AI / Future Integration

Future patterns:

  • AI-driven personalization using contexts (role, behavior)
  • smart edge variation at CDN level
  • adaptive caching based on user segments

Cache Contexts in Drupal define how cached content varies based on request conditions such as user roles, URL, or language. Instead of invalidating cache, contexts create multiple cache variations to ensure the correct version is served. They work alongside cache tags and max-age to provide a complete caching strategy for performance and personalization.


  1. What are Cache Contexts in Drupal?
  2. How do Cache Contexts differ from Cache Tags?
  3. Give examples of common cache contexts.
  4. What is cache fragmentation and how to avoid it?
  5. How do contexts affect CDN caching?

Memory Trick

Cache Tags = Invalidate
Cache Contexts = Vary
Max-Age = Expire