Skip to content
Latchkey

Stale CDN cache after deploy (missing or unawaited invalidation) in CI

The deploy succeeded but the edge still serves the old build: either no invalidation ran after the upload, or the smoke test ran before the invalidation finished. New origin content is not visible at the edge until the cache is cleared and the purge completes.

What this error means

After a green deploy, the CDN URL returns the previous asset hash or HTML. A hard refresh or waiting a while shows the new content, and a manual purge fixes it immediately.

CDN cache
# deploy succeeded, but edge still serves old bundle
$ curl -sI https://cdn.example.com/app.js | grep -iE 'etag|age|x-cache'
x-cache: HIT
age: 812
# smoke test asserted new content and failed because purge was skipped or not awaited

Common causes

No invalidation step after upload

The pipeline syncs files to origin but never purges the CDN, so the edge keeps serving cached objects until their TTL expires.

The smoke test runs before the purge completes

Invalidations are asynchronous; asserting new content immediately after firing the purge can hit an edge node that has not yet cleared.

How to fix it

Invalidate after upload and wait for completion

  1. Run the purge/invalidation step after the origin upload.
  2. Wait for the invalidation to report completed before the smoke test.
  3. Then assert the new content at the edge URL.
Terminal
INVAL_ID=$(aws cloudfront create-invalidation \
  --distribution-id "$DISTRIBUTION_ID" --paths "/*" \
  --query 'Invalidation.Id' --output text)
aws cloudfront wait invalidation-completed \
  --distribution-id "$DISTRIBUTION_ID" --id "$INVAL_ID"

Retry the smoke check briefly

Allow a short retry window so eventual edge propagation does not flake the test.

Terminal
for i in 1 2 3 4 5; do
  curl -fsS "https://cdn.example.com/app.js" | grep -q "$EXPECTED_HASH" && exit 0
  sleep 10
done
echo "edge still stale after retries"; exit 1

How to prevent it

  • Always invalidate the CDN after uploading new origin content.
  • Await invalidation completion before smoke testing the edge.
  • Add a short retry window to absorb edge propagation.

Frequently asked questions

What causes "Stale CDN cache after deploy"?
The pipeline syncs files to origin but never purges the CDN, so the edge keeps serving cached objects until their TTL expires.
How do I fix Stale CDN cache after deploy?
Invalidate after upload and wait for completion

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card