Web DevDatabases & CachingDiagramBeginner
Caching: TTL & LRU
KO: 캐시 전략: TTL & LRU·EN: Caching: TTL & LRU·JA: キャッシュ戦略:TTL&LRU·Time To Live · Least Recently Used
Core caching policies that control data freshness via Time-To-Live (TTL) expiration and bound memory usage by evicting Least Recently Used (LRU) entries
Table of Contents
Structure Diagram
Cache-Aside Lookup, TTL Expiry & LRU Eviction Cycle
1/41. Cache Lookup— Hit → instant ~1ms return
Hit → instant ~1ms returnQuick Answer
What is Caching: TTL & LRU?
Core caching policies that control data freshness via Time-To-Live (TTL) expiration and bound memory usage by evicting Least Recently Used (LRU) entries. It is a core concept in Databases & Caching within Web Dev.
Core Principles
Operating a fast in-memory cache (Redis, Memcached, or local memory) reliably depends on two complementary controls:
- TTL (Time To Live): Automatically expires entries after a set duration so cached data cannot stay stale indefinitely.
- LRU (Least Recently Used): When memory reaches its ceiling, evicts the entry that hasn't been accessed for the longest time, keeping hot data resident.
Practical Takeaways
- Prevent Cache Stampedes (Thundering Herd): If a hot key expires under heavy traffic, thousands of concurrent misses can crush the database. Add random TTL jitter (e.g.,
300s ± 30s) and coalesce rebuilds with a single-flight lock. - Guard against Cache Penetration: Repeated lookups for non-existent IDs bypass the cache every time. Cache negative (
null) results with a short TTL (15–30s) or place a Bloom Filter in front. - Prefer invalidation over blind overwrite: On database writes, deleting the cache key (
DEL) is generally safer than overwriting it under concurrent updates.
Notable Examples
- Redis with
allkeys-lru: Serving product catalogs, rate-limit counters, and session profiles within strict memory budgets. - Two-tier L1/L2 caching: Combining a tiny in-process LRU cache (seconds TTL) with shared Redis (minutes TTL) for ultra-hot reads.