Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache future timestamps for a short time #542

Merged
merged 1 commit into from
Nov 1, 2018

Conversation

nickdaugherty
Copy link
Member

@nickdaugherty nickdaugherty commented Oct 16, 2018

Instead of sending nocache headers, which has implications for cache servers like Varnish that do hit-for-pass, set a short TTL so the content can be served from cache, but expires when the timestamp becomes valid.

Example of the problem (using Varnish as an example):

  • Varnish server that does hit-for-pass caching for 10 seconds
  • Client makes a request for 1 second in the future
  • Liveblog calls nocache_headers() which sets the max-age to 0 and a 1984 Expires header
  • Varnish sees the object is not cacheable
  • Varnish marks the object as hit-for-pass
  • One second elapses
  • Clients continue making requests for the now valid timestamp
  • Those requests all go through Varnish to the origin for 10s (until hit-for-pass TTL expires)
  • After 10s the request is recached normally

This is obviously very problematic because it means one client requesting a (just barely) invalid timestamp can cause 100% of traffic (for that Varnish server) to bypass Varnish and hit the origin for a period of time.

After this change, the above scenario looks like this:

  • Varnish server that does hit-for-pass caching for 10 seconds
  • Client makes a request for 1 second in the future
  • Liveblog calculates how far into the future the invalid timestamp is
  • Liveblog sets the cache headers to expire the object in that many seconds
  • Varnish sees the object is cacheable
  • Requests keep coming in (both before and after the timestamp becomes valid)
  • Varnish serves these requests from cache, skipping origin
  • Once timestamp becomes valid, the TTL of the object in Varnish expires
  • Varnish sends through a single request to origin to get the object
  • Liveblog serves that single request normally (timestamp is now valid)
  • Object gets cached in Varnish with normal TTL

Instead of sending nocache headers, which has implications for cache servers like Varnish that do hit-for-pass, set a short TTL so the content can be served from cache, but expires when the timestamp becomes valid.
Copy link

@rogertheriault rogertheriault left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@nickdaugherty nickdaugherty merged commit b2d70c9 into master Nov 1, 2018
@GaryJones GaryJones deleted the update/improve-caching-of-future-timestamps branch June 7, 2024 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants