Introduction When CDN cache keys do not account for request variations (User-Agent, language, cookies), users receive content intended for different clients, causing broken layouts and incorrect content.
Symptoms - Mobile users seeing desktop layout - Desktop users seeing mobile layout - Content in wrong language - Logged-in users seeing anonymous content - A/B test variant shown to wrong users
Common Causes - CDN cache key not including User-Agent header - Vary header not respected by CDN - Cookie values not included in cache key - Accept-Language header ignored - CDN normalizing cache keys too aggressively
Step-by-Step Fix 1. **Check current cache key configuration': ```bash # CloudFront aws cloudfront get-distribution --id E123 \ --query 'Distribution.DistributionConfig.DefaultCacheBehavior.ForwardedValues' ```
- 1.**Configure cache variation by headers':
- 2.```bash
- 3.# Forward User-Agent to origin for cache variation
- 4.aws cloudfront update-distribution --id E123 --cli-input-json '{
- 5."DistributionConfig": {
- 6."DefaultCacheBehavior": {
- 7."ForwardedValues": {
- 8."QueryString": true,
- 9."Headers": {"Items": ["User-Agent"], "Quantity": 1}
- 10.}
- 11.}
- 12.}
- 13.}'
- 14.
`