はじめに
CloudFrontのキャッシュの仕様についてちょっと調べてみたのでメモ。
Webアプリケーションを提供するに当たり、CloudFrontをフロントに置いて全てのリクエストを通過させる構成においては、キャッシュの挙動をきちんと設計する必要があります。
TL;DR
- TTL 0=常にキャッシュされない、わけではない。
- 言い方を変えると、キャッシュ可否の挙動はOriginサーバの挙動(レスポンスヘッダ)で制御できるようになる。
実際の挙動
Amazon CloudFront – Support for Dynamic Content | AWS News Blog
https://aws.amazon.com/jp/blogs/aws/amazon-cloudfront-support-for-dynamic-content/
Variable Time-To-Live (TTL) – In many cases, dynamic content is either not cacheable or cacheable for a very short period of time, perhaps just a few seconds. In the past, CloudFront’s minimum TTL was 60 minutes since all content was considered static. The new minimum TTL value is 0 seconds. If you set the TTL for a particular origin to 0, CloudFront will still cache the content from that origin. It will then make a GET request with an If-Modified-Since header, thereby giving the origin a chance to signal that CloudFront can continue to use the cached content if it hasn’t changed at the origin.
Minimum TTLを0にしても、ファイルはキャッシュされます。
If-Modified-Sinceヘッダ付きでリクエストが行われ、Originサーバから304 Not Modifiedが返却された場合は、CloudFront上のキャッシュが利用されます。
If-Modified-Since
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Modified-Since
関連資料
Specifying How Long Objects Stay in a CloudFront Edge Cache (Expiration)
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html
If-Modified-Since以外にもキャッシュ制御をするためのヘッダがありますが、これらの挙動についても気になるところです。
上記ドキュメントに、具体的、且つ網羅的にまとまっていました。
Caching Tutorial
https://www.mnot.net/cache_docs/
HTTPにおけるキャッシュの仕組みについて体系的にまとまっています。