阅读背景:

CloudFront中的TTL 0有用吗?

来源:互联网 

Some weeks ago Amazon announced that they have lowered the content expiration period:

几周前亚马逊宣布他们降低了内容的有效期:

Amazon CloudFront Lowers Minimum Content Expiration Period

Amazon CloudFront降低最低内容过期时间

So much that you can actually set now TTL in CloudFront to 0. So my question is, why could it be useful to have a CloudFront distribution with TTL set to 0. To me this means no caching at all so every request that gets to CloudFront will end up hitting the origin.

实际上,您可以将CloudFront中的TTL设置为0.所以我的问题是,为什么将TTL设置为0的CloudFront分配有用。对我而言,这意味着根本没有缓存,因此每个到达CloudFront的请求都是如此最终会击中原点。

What am I missing?

我错过了什么?

3 个解决方案

#1


128  

This new feature of Amazon CloudFront is actually extremely useful for many use cases, because hitting the origin works a bit different than it looks at first sight and is not necessarily a problem, to the contrary; while this feature has been released earlier already, it all comes together with the recent release of Amazon CloudFront - Support for Dynamic Content, e.g. for the question at hand:

Amazon CloudFront的这一新功能实际上对于许多用例非常有用,因为命中原点的工作方式与第一眼看上去有点不同,而不一定是问题,相反;虽然此功能早已发布,但最新版本的Amazon CloudFront - 支持动态内容,例如对于手头的问题:

Variable Time-To-Live (TTL) - In many cases, dynamic content is either not cacheable or cacheable for a very short period of time, perhaps just a few seconds. In the past, CloudFront's minimum TTL was 60 minutes since all content was considered static. The new minimum TTL value is 0 seconds. If you set the TTL for a particular origin to 0, CloudFront will still cache the content from that origin. It will then make a GET request with an If-Modified-Since header, thereby giving the origin a chance to signal that CloudFront can continue to use the cached content if it hasn't changed at the origin. [emphasis mine]

可变生存时间(TTL) - 在许多情况下,动态内容要么不可缓存,要么可以在很短的时间内缓存,可能只需几秒钟。过去,CloudFront的最小TTL为60分钟,因为所有内容都被认为是静态的。新的最小TTL值为0秒。如果将特定原点的TTL设置为0,CloudFront仍将缓存该原点的内容。然后,它将使用If-Modified-Since标头发出GET请求,从而使源有机会发信号通知CloudFront可以继续使用缓存内容(如果它在原点未更改)。 [强调我的]

In other words, using a TTL of 0 primarily means, that CloudFront delegates the authority for cache control to the origin, i.e. the origin server decides whether or not, and if for how long CloudFront caches the objects; please note specifically, that a GET request with an If-Modified-Since header doesn't necessarily mean that the object itself is retrieved from the origin, rather the origin can (and should) return the HTTP status code 304 - Not Modified where applicable:

换句话说,使用0的TTL主要意味着,CloudFront将缓存控制的权限委托给源,即源服务器决定是否以及CloudFront缓存对象的时间长短;请特别注意,带有If-Modified-Since标头的GET请求并不一定意味着从源检索对象本身,而是源可以(并且应该)返回HTTP状态代码304 - 如果适用,则不修改:

Indicates the resource has not been modified since last requested. [...] Using this saves bandwidth and reprocessing on both the server and client, as only the header data must be sent and received in comparison to the entirety of the page being re-processed by the server, then sent again using more bandwidth of the server and client. [emphasis mine]

表示自上次请求后资源未被修改。 [...]使用它可以在服务器和客户端上节省带宽和重新处理,因为与服务器重新处理的整个页面相比,必须仅发送和接收标头数据,然后使用更多带宽再次发送服务器和客户端。 [强调我的]

See Mark Nottingham's excellent Caching Tutorial for details on the mechanics and benefits of HTTP cache control, a really important and effective part of the HTTP architecture.

有关HTTP缓存控制的机制和优势的详细信息,请参阅Mark Nottingham的优秀缓存教程,这是HTTP体系结构中非常重要和有效的部分。

Understanding how all these parts work together can be a bit difficult indeed, accordingly the table in section Specifying the Minimum Time that CloudFront Caches Objects for Download Distributions within Specifying How Long Objects Stay in a CloudFront Edge Cache (Object Expiration) attempts to summarize the effects when applied in the context of CloudFront with or without TTL = 0 specifically.

了解所有这些部分如何协同工作确实有点困难,因此,在指定CloudFront缓存对象在CloudFront边缘缓存中停留的时间(对象到期)中指定CloudFront缓存对象下载分布的最短时间的表中的表格尝试汇总效果当在具有或不具有TTL = 0的CloudFront的上下文中应用时。

#2


3  

Note that Amazon is not saying "TTL is 0", it is saying "Minimum TTL is 0". and that is very different. The description above is very desirable but there is no guarantee that Cloudfront actually does it.

请注意,亚马逊并没有说“TTL为0”,而是说“最小TTL为0”。那是非常不同的。上面的描述是非常理想的,但不能保证Cloudfront实际上是这样做的。

In my experiences right now, I can see a cached image stay for a few minutes in the edge while my origin has it changed already.

根据我的经验,我可以看到缓存的图像在边缘停留几分钟,而我的原点已经改变了。

So, I think saying "Minimum TTL is 0" is probably more like "Amazon has no strict intent to keep this in a cache", and maybe "and it will refetch often".

因此,我认为“最小TTL为0”可能更像是“亚马逊没有严格意图将其保留在缓存中”,也许“它会经常重新获取”。

For applications like CMSs, where the web user is posting new content, I think TTL-0 is still not sufficient. You still have to either invoke invalidations from the CMS or employ different paths for different version numbers.

对于像CMS这样的应用程序,Web用户发布新内容,我认为TTL-0仍然不够。您仍然必须从CMS调用失效或为不同的版本号使用不同的路径。

#3


0  

CloudFront can be used in combination with certificate manager to add HTTPS support to S3 websites. You might want this, but zero caching.

CloudFront可与证书管理器结合使用,为S3网站添加HTTPS支持。你可能想要这个,但零缓存。


分享到: