A Fastly Compute service (Rust) that demonstrates ETag-based cache revalidation for media assets. Every request is checked against the origin to ensure cached content is still valid, while gracefully handling errors and deleted assets.
We allow users of our API to cache the data from Giphy API and serve content from their CDN. However, they must never serve a deleted or replaced asset. This Fastly Compute service shows how a CDN can revalidate on every request using standard HTTP conditional headers.
Client ──▸ Fastly Compute Service ──▸ Fastly Cache ──▸ Origin (media.giphy.com)
-
Route check — Only paths matching
/media/{id}/{filename}.{ext}are intercepted. All other requests pass through to origin. -
Cache lookup — The service checks Fastly's core cache for a stored response.
-
Revalidation (cache hit) — Sends
If-None-Match(ETag) andIf-Modified-Sinceheaders to origin:Origin Status Action 304 Serve cached response (still valid) 2xx Replace cached copy, serve fresh response 404 / 410 Purge cache, return 404 (asset deleted) 5xx Fail-open — serve stale cached response Network error Fail-open — serve stale cached response -
Fresh fetch (cache miss) — Fetches from origin, caches 2xx responses, and applies the same status-code rules.
Every response includes debug headers for observability:
| Header | Values |
|---|---|
X-Debug-Cache |
HIT, MISS, STALE, UPDATED, PURGED |
X-Debug-Origin |
Origin status code (for example 200, 304, 404, 410, 503) or ORIGIN-ERROR |
# Install Fastly CLI
brew install fastly/tap/fastly
# Authenticate profile
fastly profile create
# Deploy
fastly compute deploy
# Alternatively you may run this locally
fastly compute serveSee fastly.toml for service and backend configuration.
├── src/main.rs # Fastly Compute service source
├── fastly.toml # Fastly Compute package/service configuration
├── Cargo.toml # Rust package configuration
└── README.md # This file
Open-source reference implementation. See repository for license details.