Soru

Zorluk: ZorAmazon CloudFront Caching, Origins, and Security

A company distributes a multilingual web application globally using an Amazon CloudFront distribution. The origin server requires the viewer's language preference and device type to return the correct localized version of the page. Currently, the CloudFront distribution has a cache policy that forwards the raw `User-Agent` and `Accept-Language` headers to the origin. A SysOps administrator reports that the cache hit ratio is less than 5% because of the high variance in these headers. Which combination of actions should the administrator take to improve the cache hit ratio while ensuring the origin still receives the correct localization and device information?

  1. A
    Move the User-Agent and Accept-Language headers from the cache policy to an origin request policy, and keep them removed from the cache policy.
  2. B
    Configure a Lambda@Edge function on the origin response event to overwrite the Cache-Control header to no-store, no-cache for all requests that include User-Agent or Accept-Language headers.
  3. Configure a CloudFront Function on the viewer request event to normalize the Accept-Language header to a set of supported language codes and write the value to a new X-Language header. Update the cache policy to cache on the X-Language header and the CloudFront device detection headers (such as CloudFront-Is-Mobile-Viewer), while removing the raw User-Agent and Accept-Language headers.Cevap
  4. D
    Replace the CloudFront distribution's DNS record with a Route 53 CNAME record at the zone apex, and configure latency-based routing policies to bypass the cache for localized headers.

Cevap

Configure a CloudFront Function on the viewer request event to normalize the Accept-Language header to a set of supported language codes and write the value to a new X-Language header. Update the cache policy to cache on the X-Language header and the CloudFront device detection headers (such as CloudFront-Is-Mobile-Viewer), while removing the raw User-Agent and Accept-Language headers.
The correct option is correct because using a CloudFront Function on the viewer request event allows the incoming Accept-Language header to be normalized to a limited set of language codes (such as 'en', 'es', or 'fr') and mapped to a custom header. Caching on this custom header and the built-in device detection headers (which are normalized to boolean values) instead of the raw headers dramatically reduces cache key cardinality and improves the cache hit ratio while preserving the origin's ability to localize content.

Adım Adım Çözüm

1
Identify the root cause of the low cache hit ratio.
The raw User-Agent and Accept-Language headers contain thousands of unique value combinations, causing CloudFront to treat almost every request as unique and forward it to the origin.
This establishes that high cardinality in the cache policy is responsible for the low cache hit ratio.
2
Select a solution for device type detection that minimizes cache key variation.
Use CloudFront's built-in device detection headers (like CloudFront-Is-Mobile-Viewer), which map complex User-Agent strings into simple boolean indicators.
This reduces the cardinality of the device-related cache key variations while still conveying the needed device information to the origin.
3
Normalize the Accept-Language header.
Write a lightweight CloudFront Function triggered on the viewer request event to normalize the Accept-Language header into a set of supported languages and store the normalized string in a new custom header (such as X-Language).
CloudFront Functions execute at the edge with sub-millisecond latency, making them ideal for simple header manipulation before the cache lookup occurs.
4
Update the cache configuration to use the normalized headers.
Modify the cache policy to include only the new X-Language header and the CloudFront device detection headers, and remove the raw User-Agent and Accept-Language headers from the cache key.
This ensures that CloudFront caches content based only on the few supported languages and device categories, dramatically increasing the cache hit ratio while ensuring the correct version is served.

Anahtar Kavram

CloudFront Cache Optimization using Header Normalization and Device Detection Headers
Bu soruyu puanla