Mixing beresp.ttl and Expires header




  • Andrew Betts

    Hi Ilya,

    This actually should not be necessary. If you are using Expires as your cache-freshness indicator, then that is an absolute moment in time, and is unaffected by how long the object has already been in cache upstream. For example, if you send this with your object from origin:

    http Expires: Friday, 26 October 2018 12:00:00 UTC

    ... Fastly will set a TTL such that the object expires at that time. Then if sometime later, the object is fetched again from an edge location, the shield will serve it from cache, and the edge will set a shorter TTL, again ensuring that it expires at the specified time.

    However, we actually recommend that you prefer the Cache-Control header for specifying freshness. It is more modern and more powerful and flexible. You would set the following header at your origin:

    http Cache-Control: max-age=3600

    And the shield would store the object for an hour. Now, imagine 30 mins later, an edge needs to fetch the object. The shield will send the cached version, retaining the Cache-Control header that you set, but it will add an Age header as well:

    http Cache-Control: max-age=3600 Age: 1200

    The edge will set a TTL of 1200, by taking the desired TTL expressed in the Cache-Control header, and deducting the Age, ie. the amount of time that the object has already spent in cache.

    Finally, a few points in relation to your VCL code:

    • In the vcl_fetch subroutine, adjusting headers such as beresp.http.Expires or besrep.http.Cache-Control does not affect the amount of time that the object is cached within Fastly. It only controls the behaviour of downstream caches (including Fastly edge nodes if the current node is a sheild) and the browser client.
    • Adjusting beresp.ttl does change the amount of time that we store the object, but doesn't have any effect on the headers, so is the compliment to setting the headers
    • Two minutes is our platform default TTL, so if we can't work out what TTL you want, you get 2 mins!

    If you haven't already, you might find it useful to try our fiddle tool:


    Hope this helps!


    Comment actions Permalink
  • Ilya Kaplan

    Wow, that's great idea substracting age from my ttl. I assume I'd need std.atoi for that, but it seems easy. Also I'd need to do this math again on edge's vcl_deliver in order to set right cache control...

    So i guessed the right approach with expires too, just had trouble ensuring it, because beresp.ttl value is uneffected by expires from upsteam, even though it seems to work right.


    Comment actions Permalink

Please sign in to leave a comment.