Origins, Behaviors & Caching

Hits, misses, TTLs, and cache keys: how CloudFront decides what to serve.

1 min read

The pieces

  • Distribution: your CloudFront setup, with a domain like d111111abcdef8.cloudfront.net.
  • Origin: where the real content lives: an S3 bucket, a load balancer, API Gateway, or any web server.
  • Cache behavior: rules per path pattern, e.g. /api/* goes to the API origin with no caching, while * goes to S3 and is cached.

Hit or miss

  1. A user requests /logo.png.
  2. The nearest edge checks its cache. Hit: return it immediately.
  3. Miss: fetch it from the origin, return it, and keep a copy for next time.

How long things stay cached

  • The TTL (time to live) comes from your cache policy and the origin's Cache-Control header.
  • The cache key decides what counts as "the same" request. By default it's the URL path. Adding query strings, headers, or cookies to the key creates more variations and lowers your hit rate.
How it flows
  1. 1Viewer

    Requests /logo.png

  2. 2Edge location

    Cached? Return it (hit)

  3. 3Origin

    On a miss: S3 or server

  4. 4Edge caches it

    For the next viewer

Tip

Name static files with a content hash, like app.3f9a1c.js. Then cache them for a year, because a new version gets a new name. Keep index.html on a short TTL so users pick up new deployments quickly.

Key takeaways

  • A distribution routes path patterns (behaviors) to origins.
  • Hits come from the edge cache, and misses go to the origin once and are then cached.
  • Keep cache keys minimal and use hashed filenames for long-lived caching.

Check your understanding

Pick an answer to see the explanation.

  1. 1. Adding every cookie and header to the cache key will most likely…