AlgoViz

CDN cache invalidation

medium 30 min

How to make a CDN stop serving an outdated copy: let it expire, purge it, mark it stale, give the new version a new name, or purge a whole group by tag.

See it run

The origin has a new version. Watch when each copy learns about it, and what the origin has to carry.

Operations

TTL expiry

A page with max-age=3600. Nobody sends a message.

Step 1 of 6

Cached

ReadersEdgev1, 3600 s leftOriginv1

The copy of version 1 may be used for one hour.

Step 1: Cached

copy = cache[path]if now() < copy.expires:    return copyanswer = origin(path, if_none_match=copy.etag)if answer.status == 304:    copy.expires = now() + 3600else:    cache[path] = Copy(answer, now() + 3600)
Delay
Up to 1 hour
Origin load
Spread
Needs
a header

Nothing to operate, and it heals every other mistake.

In the real world

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

Purge and commit in the wrong order.

Step 1 of 4

Purge first

ReadersEdgeemptyOriginprice 10

The edge is empty. The database still says 10.

Step 1: Purge first

Common mistakes

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

  • Purge before commit

    critical

    A reader in between refills the cache with the earlier value.

    Wrong
    purge(tag)db.commit()
    Right
    db.commit()purge(tag)
  • Purge everything in each deploy

    critical

    Every release becomes a load test for the origin.

    Wrong
    purge_everything()
    Right
    purge(paths=["/index.html"], soft=True)   # files have hashed names
  • Purge by hand

    major

    Somebody forgets it, on a Friday.

    Wrong
    # wiki: remember to click Purge
    Right
    outbox.add("purge", tag="product-123")   # in the same transaction
  • Deleting earlier files at release

    major

    Open pages still point to them and break.

    Wrong
    bucket.delete("app.v1.js")
    Right
    bucket.expire("app.v1.js", after_days=30)

Interview tips

  • How to explain it

    • Posters at bus shelters.
    • Wait, tear, sticker, new board, red dots.
    • The office is the origin.
  • Memory trick: STALE

    • Soft purge, TTL.
    • Always version, Labels.
    • Emergency: hard purge.
  • Order

    • Commit.
    • Inner cache, shield, edge.
    • Retry until it succeeds.
  • Check

    • What do browsers keep?
    • How many refills?
    • What if the purge is lost?