Sub topic 15
Monitoring cache performance and understanding cost
This chapter assumes you have a working distribution in front of a private S3 bucket, and that you know what a cache policy and a TTL are. Here you will check whether the cache is doing its job and see what drives the bill.
What to measure
CloudFront reports metrics in the CloudWatch namespace AWS/CloudFront. These metrics always appear in the us-east-1 Region, whichever Region your bucket is in. The basic metrics are free:
- Requests: the number of viewer requests.
- BytesDownloaded: bytes sent to viewers.
- 4xxErrorRate and 5xxErrorRate: the share of requests that returned an error.
The cache hit ratio is the most useful number for this setup. It is the share of viewer requests answered from an edge cache without going back to S3. CloudFront reports it as a percentage in the console's Cache statistics report. The per-request values Hit, RefreshHit, Miss, and Error appear in the x-edge-result-type field of the logs. You can also see them in the x-cache response header.
curl -sI https://dxxxxxxxxxxxx.cloudfront.net/assets/app.js | grep -i -E "x-cache|age"The first request usually returns x-cache: Miss from cloudfront. A repeat request returns Hit from cloudfront and an age header that shows how many seconds the object has been cached.
Finding out why the ratio is low
A low hit ratio means more requests reach S3, so viewers see higher latency and you pay for more origin traffic. Common causes:
- TTLs that are too short. Check the
Cache-Controlheaders you set when you uploaded the objects. - A cache key that is too large. Forwarding cookies, query strings, or headers that do not change the response creates a separate cache entry for each combination.
- Frequent invalidations. Each invalidation removes objects that the next request must fetch again.
- Rarely requested objects. Objects that few viewers ask for can expire before the next request arrives. You cannot do much about this.
To investigate, turn on standard logging to an S3 bucket or enable real-time logs. Then query the logs with Amazon Athena.
SELECT uri, COUNT(*) AS misses
FROM cloudfront_logs
WHERE result_type = 'Miss'
GROUP BY uri
ORDER BY misses DESC
LIMIT 20;This query lists the paths that miss most often. Those paths are the first candidates for a longer TTL or a smaller cache key.
What drives the cost
CloudFront bills mainly on three items:
| Item | What you pay for | How to lower it |
|---|---|---|
| Data transfer out to the internet | Gigabytes served, priced per Region of the edge location | Compress files and resize images |
| HTTP and HTTPS requests | Requests counted per 10,000 | Raise the hit ratio so fewer requests go to the origin. Viewer requests are billed either way. |
| Invalidation requests | Paths beyond the monthly free allowance | Use versioned file names |
Transfer from S3 to CloudFront is free. Only the delivery from CloudFront to viewers is billed. A higher hit ratio therefore lowers S3 request charges and origin load. It does not lower the CloudFront bill for viewer traffic.
A simple monitoring routine
- Create a CloudWatch alarm on
5xxErrorRateso you hear about origin problems early. - Review the cache hit ratio weekly after each deployment.
- Check the top missed paths in your logs.
- Set an AWS Budgets alert on CloudFront spend.
Exercise
Your cache hit ratio dropped from 95% to 60% after a release. Logs show many misses on paths such as /assets/app.js?session=abc123. What is the likely cause and the fix?
Show answer
The cache policy forwards the query string, so each unique session value creates a separate cache entry. Change the cache policy so the cache key does not include the query string, or includes only the parameters that change the response. Then confirm that the hit ratio recovers.
Exercise
Does a higher cache hit ratio reduce what S3 charges you for data transferred to CloudFront?
Show answer
No. Transfer from S3 to CloudFront is free. A higher hit ratio reduces S3 request charges and origin load, and it improves viewer latency.