AlgoViz

Amazon CloudFront

medium 25 min

CloudFront is the content delivery network of AWS.

See it run

Match a behavior, fall through three caches, run functions at the right event, invalidate, and lose an origin.

Operations

Three cache layers

Two continents ask for one object. S3 sends it once.

Step 1 of 6

Dublin misses

CloudFrontViewersEdge DublinMISSEdge TokyoemptyRegion EuropeemptyRegion AsiaemptyOrigin ShieldemptyS3 origin0 requests

Edge miss

A viewer in Ireland asks. The edge has no copy.

Step 1: Dublin misses

edge: copy?regional edge cache: copy?Origin Shield: copy?fetch from S3, store everywhere
When
First requests
You leave with
Three warm layers

Multiply the miss ratios of the layers. The product is what your origin must survive.

In the real world

The same amazon cloudfront doing real jobs. Pick one and step through it.

A 12 GB game patch is released at noon.

Step 1 of 3

11:50, warm up

CloudFrontViewersEdge DublinemptyEdge TokyoemptyRegion EuropeemptyRegion AsiaemptyOrigin Shieldparts readyS3 originfew requests

Warm shield

The team requests the first parts in each region, so the shield already holds them.

Step 1: 11:50, warm up

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Broad pattern first

    major

    The first match wins, so later narrow patterns never run.

    Wrong
    0: /*1: /api/*
    Right
    0: /api/*1: /static/*last: *
  • Everything in the cache key

    major

    Cookies and User-Agent give each person a private copy of a public file.

    Wrong
    key: path + all cookies + all headers
    Right
    key: path + the two query values used
  • Auth on origin request

    critical

    A hit skips origin events, so cached private content goes out unchecked.

    Wrong
    origin-request: verify(token)
    Right
    viewer-request: verify(token)
  • Default timeouts with failover

    major

    Three attempts of ten seconds mean a 30-second wait before the secondary is tried.

    Wrong
    attempts = 3, timeout = 10
    Right
    attempts = 2, timeout = 2then test it

Interview tips

  • How to explain it

    • Behaviors choose, layers answer.
    • Edge, region, shield, origin.
    • Functions by event.
  • Calculate

    • Product of miss ratios.
    • Attempts times timeout.
    • Paths invalidated a month.
  • Reach for it when

    • Origins are in AWS.
    • Buckets must be private.
    • Site and API share a domain.
  • Watch

    • Hit ratio per behavior.
    • Minimum TTL on personal pages.
    • us-east-1 for certificates and Lambda@Edge.
    • Configuration deploy time.