How to calculate TTL for various types of cache?

Viewed 593

Is there a standard way to calculate TTL for different types of cache? 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 e.g.(privacy policy, about, images and maps)

  2. application cache is used to serve a- sessions b- recently used reads regardless of the type

  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)

Calculate TTL for the following types based on the little to no information provided above:

  • Client cache
  • CDN
  • Webserver cache (used for media)
  • Application caache (sessions and recent reads of some data)
1 Answers

TTLs are mostly defined using historical data, use cases, and experience. 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? In some cases, stale data is not accepted at all but in other cases, it's ok to use stale data for 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. Etag is another thing, that's used in 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.

The session should be cached for a week or so, but some applications cache the session for a longer period. Of course, there're pros and cons of using low/high TTL.

Application data cache has similar characters as CDN data, the data can change any time and change must reflect in the cache. 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.


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.


Refers

Related