What happens to a cached API response when the origin returns 503?
Test when a public API cache can return an older response, when the app’s 503 reaches clients, and how to verify recovery.
Download the shared starter and open its stale-cache folder.
A fresh cache hit can return a public API response without reaching your app. That does not mean that an expired entry will replace every 503. On Gregale, the gateway's stale-on-error fallback is for a failed wake. Treat a running application answering 503 as a separate case. This distinction matters when your product catalog can tolerate a short delay but customers still need to know when their orders or inventory cannot be served.
This drill uses one disposable app and synthetic data. It doesn't stop a runtime, exhaust capacity, modify a customer app, or need an external account. It tests a guest-generated HTTP response, not a gateway wake failure.
Cache one public GET, then let it go stale
Deploy the starter's Dockerfile to a new, disposable Gregale app. The test must reach its canonical HTTPS URL anonymously. Do not make an existing private app public. No access credentials should appear in the fixture or probe URL.
Create a cache rule for the single catalog path. Set stale-while-revalidate to zero so an immediate stale response from that mode cannot be mistaken for fallback on an error.
gregale edge rule create --app "$APP" --kind cache \
--match-host "$HOST" --match-path /catalog --match-method GET \
--cache-max-age-seconds 10 --cache-stale-while-revalidate-seconds 0 \
--cache-stale-if-error-seconds 120
node verify.mjs "https://$HOST"
The runner uses a unique query on each run to avoid prior cache entries. All before, outage and recovery probes for the primary URL use the same query string. The origin uses the requested time window to return known catalog JSON, a tagged HTTP 503, then changed catalog JSON. The synthetic failure is limited to /catalog; the hosted runner does not probe /healthz. The runner requires an edge cache hit, waits past freshness, and checks the exact response bodies. Its credentialed control uses an inert fixture cookie to bypass caching on the same URL. A second query key is deliberately not warmed before the simulated outage. No real authentication cookies are needed.
If the request returns tagged 503 after expiry, the app's failure reached the client. If it returns 200 and old data, check the cache decision and don't infer stale-on-error from the body alone: a fresh hit, stale-while-revalidate, stale-while-waking, or an outer CDN may give that same result for different reasons. At recovery, verify the exact new catalog JSON on the public URL.
Design for the failure you expect
- Fresh hit: cache a public read with an explicit freshness limit. That hit is served without consulting the application.
- Slow refresh: stale-while-revalidate permits an immediate older response while a refresh runs; it does not wait for an origin error. Use it only when the consumer can tolerate stale values.
- Application returns 503: verify what clients actually get. A valid HTTP response from a running app isn't the same as failing to wake it.
- No cached copy: there is no fallback payload. Leave personalized data, mutations, and sensitive responses out of shared caching.
See Gregale cache controls. Delete the disposable app and its rule after the drill.
What we verified on Gregale
With CLI v0.1.18-rc.391, an anonymous request returned exact catalog-v1 JSON with X-Faas-Cache: hit while the entry was fresh. After expiry, the app’s tagged 503 reached the client for both the previously warmed URL and an uncached query. After recovery, the public URL returned exact catalog-v2 JSON. This run did not exercise a gateway wake failure. The disposable app was removed.
Questions
- Does stale-if-error replace every application 503?
No. An application returning an HTTP 503 is different from a gateway being unable to wake it. Verify the actual cache decision and keep a separate uncached control.