Time-to-Live (TTL) Invalidation
Deslize para mostrar o menu
How TTL-Based Cache Invalidation Works
Time-to-Live (TTL) invalidation is a core caching strategy where each cached item is assigned an explicit expiration time. Once the TTL expires, the cache automatically removes or marks the data as stale. TTL values are usually set when writing data to the cache, either as a fixed duration (such as 60 seconds) or a specific expiration timestamp.
When a client requests data:
- If the cached item is still within its TTL, the cache returns the stored value;
- If the TTL has expired, the cache either fetches fresh data from the source or signals a cache miss.
Many cache systems use background processes or lazy expiration. With lazy expiration, the cache only checks TTL when an item is accessed, so expired items may persist until next read.
Impact on Consistency and Performance
TTL invalidation offers a balance between cache freshness and efficiency:
- Reduces load on backend systems by serving frequently requested data from cache;
- Ensures data is eventually refreshed, improving consistency over time;
- Introduces a window where stale data may be returned if the underlying data changes before TTL expiry.
Shorter TTLs increase consistency but reduce cache hit rates and performance. Longer TTLs improve performance but risk serving outdated data.
TTL analogy: Expiring milk
Imagine you buy a carton of milk that expires in 7 days. You mark the expiration date on the carton. Once the date passes, you throw the milk away and buy a fresh one.
TTL cache invalidation works the same way: when you store data in the cache, you set a timer (the TTL). Once the timer runs out, the cache automatically removes the data, ensuring you never use "expired" information.
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo