Topic lessonTopic: CloudFront: delivery, caching and 403 errors
CloudFront caching explained: cache keys, TTLs and content updates
Requests to the same URL should not always share the same response. A CloudFront cache key distinguishes reusable responses, while TTL settings govern freshness. Follow a fictional public catalog to separate language mix-ups from content that remains cached after an update.
Choose the response-defining inputs for a cache key, trace reuse and expiry, and distinguish versioned filenames from invalidation.
An origin is the source, such as a server or S3. A cache reuses a stored response. Query parameters follow the question mark in a URL. This lesson makes no AWS requests and provisions no paid resources.
Decide which requests can share a response
A cache key identifies the stored response to look up. A hit requires an available, reusable matching entry. Include language when it changes the representation. Including tracking parameters that do not change the response fragments reuse.
- Look up key → Cache hitAn eligible entry is available
- Look up key → Contact originNo reusable entry is available
- Look up key
- For the public-catalog example
- Cache hit
- Reuse the available entry
- Contact origin
- For a miss or an expired entry
Separate forwarding from key selection
A cache policy selects headers, cookies and query parameters for the key and supplies TTL settings. Key values are forwarded to the origin. An origin request policy can forward additional inputs without putting them in the key. Forwarding therefore does not, by itself, partition cached responses.
| Input | Role in this example | Key treatment |
|---|---|---|
| Path /catalog.json | Selects the resource | Distinguish it |
| lang=en / lang=ja | Changes the response language | Distinguish it |
| utm_source | Attribution only; identical response | Exclude in this example |
C1: trace which version is returned
C1 is a reading model with one cache location. It starts empty, keys by path plus lang and has an effective TTL of 60 seconds from retrieval. Only GET requests occur; there is no eviction, failure, browser cache or stale-response reuse directive. Forwarded utm_source never changes the body. Times and responses below are stipulated for this example.
| Time | Event | Result under C1 |
|---|---|---|
| 0 s | Request lang=en | MISS: retrieve en-v1; retain for 60 seconds. |
| 20 s | Request lang=en with a different utm_source | HIT: same key, en-v1. |
| 25 s | Request lang=ja | MISS: retrieve ja-v1 under another key. |
| 30 s | Origin English version changes to en-v2 | No request occurs; the stored en-v1 remains. |
| 45 s | Request lang=en | HIT: en-v1 is still fresh. |
| 61 s | Request lang=en | Expired: origin returns 200 with en-v2, as stipulated. |
Expiry is not an automatic push of new content. A subsequent request can trigger origin validation; an unchanged representation can yield 304, while changed content can yield 200 with a new body. C1 fixes the latter response rather than predicting every live request.
For comparison, C2 omits lang from the key while the origin varies the body by language. A Japanese request matching an English cached entry can reuse the English body. A shorter TTL does not fix an incorrect partition. First align the key with the inputs that change the response.
A TTL is not a guaranteed update schedule
TTL (Time To Live) expresses a freshness lifetime. CloudFront combines minimum, maximum and default TTL settings with origin freshness headers such as Cache-Control. The default applies when the relevant origin freshness information is absent.
| Origin directive | Effective TTL in T1 | Reason |
|---|---|---|
| max-age=300 | 120 s | Bounded by the maximum. |
| max-age=5 | 10 s | Raised to the minimum. |
| No relevant freshness header | 30 s | Uses the default. |
Plan the name, references and copies involved in an update
| Mechanism | What changes | What still needs checking |
|---|---|---|
| Overwrite the same origin path | Origin body | Fresh stored copies do not vanish immediately. |
| app.v1.js → app.v2.js | A different filename | Update HTML references too; distinct from S3 Versioning. |
| CloudFront invalidation | Invalidates selected CloudFront copies | Does not clear every browser or intermediary cache. |
- Check the key against the inputs that change the body.
- Identify changed files, references and a recovery plan.
- Choose freshness, versioned filenames and any necessary invalidation.
- Separate origin, CloudFront and browser observations when locating an old copy.
- Keep personalized responses outside the public-cache example.
Check the key, time and update mechanism
Choose an option to see the result and explanation.
Check, then explain in your own words
- Trace C1 at 0, 20, 25, 45 and 61 seconds per key.
- Explain why lang differs from utm_source.
- Apply the minimum, maximum and default in T1.
- Explain versioned filenames and the scope of CloudFront invalidation.
Check your understanding: What should you separate when an origin update still yields an old response?
Separate the cache key, effective freshness and the layer holding the old copy.
An incorrect response partition calls for a different fix from reusing a still-fresh copy.
Sources and conditions checked
The official references used in this lesson are collected here with their scope, conditions and corresponding sections. Independent expert review and live AWS comparison have not been performed.
Scope and conditionsCache identity, representation variation and unnecessary fragmentation. Documentation review, not live AWS verification.
Used in this lessonDecide which requests can share a response Check the key, time and update mechanism
Scope and conditionsMinimum/maximum/default TTL, validation, early eviction and positive-minimum safety boundary. Documentation review, not live AWS verification.
Used in this lessonDecide which requests can share a response C1: trace which version is returned A TTL is not a guaranteed update schedule Check the key, time and update mechanism
Scope and conditionsCache-policy key inputs and TTL settings. Documentation review, not live AWS verification.
Used in this lessonSeparate forwarding from key selection A TTL is not a guaranteed update schedule
Scope and conditionsKey-input forwarding versus extra inputs forwarded outside the key. Documentation review, not live AWS verification.
Used in this lessonSeparate forwarding from key selection
- CloudFront: Invalidate filesChecked:
Scope and conditionsVersioned filenames versus invalidation and the separate browser-cache layer. Documentation review, not live AWS verification.
Used in this lessonPlan the name, references and copies involved in an update Check the key, time and update mechanism