AlgoViz

Cloudflare CDN

medium 24 min

Cloudflare is a reverse proxy that runs in hundreds of cities behind the same anycast addresses.

See it run

Follow a request through anycast, filters, Workers and two cache tiers, then change the content and break parts of it.

Operations

Miss, tier, hit

Two cities ask for the same file. The origin answers once.

Step 1 of 6

Paris asks

CloudflareUsers in ParisUsers in TokyoParis edgeMISSTokyo edgeemptyUpper tieremptyYour origin0 requests

Miss in Paris

Anycast brings the visitor to the Paris data centre. It has no copy.

Step 1: Paris asks

lookup(edge)if miss: lookup(upper tier)if miss: fetch from origin, oncestore on the way back
When
First request
You leave with
Two warm tiers

Say which tier absorbs which miss. That is the whole calculation of origin load.

In the real world

The same cloudflare cdn doing real jobs. Pick one and step through it.

What visitors see when your server fails.

Step 1 of 4

Healthy

CloudflareUsers in ParisUsers in TokyoParis edgeHITTokyo edgeHITUpper tiercopyYour originhealthy

Normal

Cached files are hits; pages come from the origin.

Step 1: Healthy

Common mistakes

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

  • Expecting HTML to be cached

    major

    Eligibility follows the file extension. Pages are DYNAMIC until a rule includes them.

    Wrong
    cf-cache-status: DYNAMIC
    Right
    cache rule: /blog/*edge TTL 1 day, tag per post
  • Flexible encryption

    critical

    Visitors see a lock, but the traffic to your server is plain HTTP.

    Wrong
    mode = flexible
    Right
    mode = full (strict)valid certificate on the origin
  • Purging everything on deploy

    critical

    Every URL becomes a miss at once, and the origin carries the whole site.

    Wrong
    purge_everything: true
    Right
    versioned filespurge tags for changed pages
  • Leaving the origin open

    critical

    Anyone who learns its address goes around every filter.

    Wrong
    allow 0.0.0.0/0
    Right
    allow Cloudflare ranges onlyor a tunnel

Interview tips

  • How to explain it

    • One address, many cities.
    • Filter, code, cache, tier, origin.
    • Only misses cost the origin.
  • Calculate

    • Origin load is traffic times each miss ratio.
    • Batch purges: 100 operations a request.
    • Compare plans by the limit you will hit first.
  • Reach for it when

    • Readers are global.
    • Origins are in several places.
    • Security and code belong near the user.
  • Watch

    • Hit ratio per content type.
    • 522 and 524 counts.
    • Purge failures.
    • One provider, one shared failure.