简体   繁体   English

如何计算各种类型缓存的 TTL?

[英]How to calculate TTL for various types of cache?

Is there a standard way to calculate TTL for different types of cache?是否有标准方法来计算不同类型缓存的 TTL? this's more of a generic question so lets assume we're designing a system from scratch and we have the following requirements/specs:这更像是一个通用问题,所以让我们假设我们正在从头开始设计一个系统,并且我们有以下要求/规范:

  1. static resources served by CDNs are rarely updated eg(privacy policy, about, images and maps) CDN 服务的 static 资源很少更新,例如(隐私政策、关于、图像和地图)

  2. application cache is used to serve a- sessions b- recently used reads regardless of the type应用程序缓存用于服务 a- 会话 b- 最近使用的读取,无论类型如何

  3. client side cache (previously requested files), as well as lets say images or posts a client can see (something similar to Instagram/twitter in this case)客户端缓存(以前请求的文件),以及客户可以看到的图像或帖子(在这种情况下类似于 Instagram/twitter)

Calculate TTL for the following types based on the little to no information provided above:根据上面提供的很少或没有信息计算以下类型的 TTL:

  • Client cache客户端缓存
  • CDN内容分发网络
  • Webserver cache (used for media)网络服务器缓存(用于媒体)
  • Application caache (sessions and recent reads of some data)应用程序缓存(会话和最近读取的一些数据)

TTLs are mostly defined using historical data, use cases, and experience. TTL 主要使用历史数据、用例和经验来定义。 There are no predefined rules/theories that tell you about the cache expiry.没有预定义的规则/理论可以告诉您缓存到期。 Cache TTL should have some tolerance like if you set TTL too high then you might see expired(stale) data, what's the impact of stale data in your application?缓存 TTL 应该有一些容忍度,例如如果您将 TTL 设置得太高,那么您可能会看到过期(陈旧)数据,陈旧数据对您的应用程序有什么影响? In some cases, stale data is not accepted at all but in other cases, it's ok to use stale data for SOME TIME .在某些情况下,根本不接受过时的数据,但在其他情况下,可以在SOME TIME使用过时的数据。

Still, you'll observe each caching system has some predefined TTL for example AWS CDN has 24 hours expiry, Google CDN has 1 hour.不过,您会观察到每个缓存系统都有一些预定义的 TTL,例如 AWS CDN 有 24 小时到期,Google CDN 有 1 小时。 Etag is another thing, that's used in CDN. Etag 是另一回事,用于 CDN。

CDN can catch data for a week but depending on the data some data can change hourly as well so in that case expiry is set to a lower value, similar things apply to other use cases. CDN 可以捕获一周的数据,但根据数据,一些数据也可以每小时更改一次,因此在这种情况下,到期设置为较低的值,类似的事情适用于其他用例。

The session should be cached for a week or so, but some applications cache the session for a longer period. session 应该缓存一周左右,但有些应用程序缓存 session 的时间更长。 Of course, there're pros and cons of using low/high TTL.当然,使用低/高 TTL 有利有弊。

Application data cache has similar characters as CDN data, the data can change any time and change must reflect in the cache.应用数据缓存与 CDN 数据具有相似的特性,数据可以随时更改,更改必须反映在缓存中。 Again depending on the use case the TTL should be used, my experiences say you can cache some data for one day or one week but some data can not be cached for more than 15 minutes since it might get updated within 15 minutes.再次根据应使用 TTL 的用例,我的经验是您可以将某些数据缓存一天或一周,但某些数据不能缓存超过 15 分钟,因为它可能会在 15 分钟内更新。


Depending on the nature of the data you can always find some optimal TTL, finding optimal TTL takes time as you would have to monitor cache hit/miss and stale data ratio.根据数据的性质,您总能找到一些最佳 TTL,找到最佳 TTL 需要时间,因为您必须监控缓存命中/未命中和过时数据比率。


Refers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM