Cache Max-Age in Drupal defines how long cached content remains valid before it expires.
If Cache Tags answer when to invalidate and Cache Contexts answer when to vary, then Max-Age answers when to expire.
In modern Drupal architecture, Max-Age is critical for:
- controlling freshness of content
- balancing performance vs accuracy
- coordinating with CDN caching
- enabling predictable cache lifecycles
As a Drupal developer, you design Max-Age with both system performance and business needs in mind.
Core Concept
Max-Age is the time-to-live (TTL) for cached content (in seconds).
Basic flow:
Cache Created
↓
Max-Age Set (e.g., 3600 seconds)
↓
Time passes
↓
Expires → Rebuild cache
Max-Age Values
Common values:
0 → no caching (always dynamic)
3600 → 1 hour
86400 → 1 day
-1 → permanent (until invalidated by tags)
Example:
$build['#cache']['max-age'] = 3600;
Cache Max-Age Architecture
Request
↓
Check Cache Entry
↓
If not expired → serve
If expired → rebuild + store
Real Project Example
Scenario: Homepage shows alerts + static content.
Strategy:
- Alerts → max-age: 300 (5 minutes)
- Static blocks → max-age: 86400 (1 day)
Result:
- critical updates refresh quickly
- static content remains highly cached
- overall performance optimized
Decision Framework
Use low Max-Age when:
- content changes frequently
- real-time accuracy is needed
Use high Max-Age when:
- content is mostly static
- high traffic expected
- performance is priority
Use permanent (-1) when:
- content controlled by Cache Tags
Developer Usage (Code Example)
Render array:
$build['#cache']['max-age'] = 600;
Combine with tags and contexts:
$build['#cache'] = [
'max-age' => 600,
'tags' => ['node:1'],
'contexts' => ['user.roles']
];
Integration with Views
Views allow cache lifetime configuration.
Example:
View: Latest News
Cache Time: 15 minutes
This reduces repeated database queries.
Frontend / React Mapping
Drupal Max-Age
↓
API Response TTL
↓
CDN Cache (CloudFront)
↓
React App receives cached data
Example:
- API cached for 10 minutes
- React fetches fast response
Platform / DevOps Layer
Max-Age aligns with infrastructure:
- Drupal sets TTL
- CDN respects Cache-Control headers
- Redis stores backend cache
Example:
Cache-Control: max-age=600
CI/CD consideration:
- deployments may override TTL
- combine with cache invalidation strategy
Performance Considerations
Best practices:
- avoid max-age = 0 unless necessary
- combine with cache tags for smarter invalidation
- tune TTL per content type
Trade-off:
Higher Max-Age → better performance
Lower Max-Age → fresher content
SEO / Accessibility Considerations
- ensures fresh metadata when needed
- prevents stale pages in search results
- improves page speed (SEO ranking factor)
Common Production Issues
- setting max-age too low → poor performance
- setting too high → stale content
- not combining with cache tags
- mismatch between Drupal and CDN TTL
Cache Max-Age in Drupal defines how long cached content remains valid before expiring. It is part of Drupal’s caching strategy along with cache tags and contexts. By setting appropriate max-age values, developers can balance performance and content freshness while integrating with CDN caching and backend systems like Redis.
- What is Cache Max-Age in Drupal?
- How does Max-Age differ from Cache Tags?
- When should you use max-age = 0?
- How does Max-Age affect CDN caching?
- What is the trade-off between performance and freshness?
Memory Trick
Max-Age = Expire Time
Tags = Invalidate
Contexts = Vary