Michael
Michael

Reputation: 1833

CloudFront - Unexpected caching behaviour on error

I am seeing a strange behaviour from CloudFront when my origin throws an error.

My scenario is as follows:

Initial request:

I then make a change to the origin to ensure it throws an error response as follows:

When making another request (which sends the header if-none-match: "example-etag") I would expect the following to occur:

What I am actually seeing on the second request:

Is this expected behaviour and if so how do I get CF to relay the error to the client?

Upvotes: 3

Views: 2373

Answers (1)

Michael
Michael

Reputation: 1833

Thanks to Kevin's comment I was able to understand that this actually the expected behaviour.

The documentation at https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html explains that:

If the origin is unreachable and minimum TTL is greater than 0, CloudFront serves the object that it got from the origin previously. To avoid this behavior, include the Cache-Control: stale-if-error=0 directive with the object returned from the origin. This causes CloudFront to return an error in response to future requests if the origin is unreachable, rather than returning the object that it got from the origin previously.

I can confirm that returning stale-if-error=0 in the Cache-Control header works and the error is relayed to the user.

Upvotes: 3

Related Questions