Estrategias de Caché

Cache-aside · Write-through · Desalojo

Escrito por Darjus Kazakevic · revisado el

Decide how data enters the cache, how it leaves, and how stale it may be.

A cache helps only if the app reads and writes through it safely. A caching strategy answers three questions: who fills the cache on a miss, what a write does to the cached copy, and when an entry leaves. The answers apply to any cache: in-process, Redis or a CDN.

Most systems use cache-aside with a TTL (time to live: how long an entry stays valid) and delete the key on write. The hard parts are invalidation, which keeps cached data in step with the database, and the stampede, when a hot key expires and thousands of requests miss at once.

En esencia — Most systems need three rules: fill the cache on a miss (cache-aside), delete the key on every write, and give every key a TTL as a safety net.

Cuándo recurrir a él

Recurre a él cuando

  • Reads repeat often and the data may be a few seconds old
  • Adding Redis or a CDN and deciding how writes reach it
  • The database spikes every time a hot key expires

Reconsidéralo cuando

  • Every read must see the latest write, like an account balance
  • Data changes on nearly every read, so most lookups would miss
  • Write-behind for data you cannot afford to lose

Conceptos clave

Cache-aside (Lazy loading)
The app checks the cache first. On a miss it reads the database and stores the result in the cache. The cache only holds data someone asked for. This is the default with Redis and Memcached.
Read-through and write-through
The cache sits in the data path and talks to the database itself. Read-through fills on a miss. Write-through writes the cache and the database together, so written data is always cached, but each write is slower.
Write-behind and write-around (Write-back)
Write-behind writes to the cache and flushes to the database later, in batches. It is fast, but a crash loses unflushed writes. Write-around writes only to the database and lets the next read fill the cache.
Eviction and expiry (LRU · LFU · TTL)
When the cache is full, a policy picks what to remove. LRU removes the key read least recently; LFU removes the key read least often. A TTL removes a key after a fixed time, full or not.
Stampede protection (Thundering herd)
When a hot key expires, many requests miss together and all query the database. Fixes: single-flight (one request fills, the others wait), a short lock, or random jitter on TTLs so keys do not expire together.

Reglas de diseño

Delete on write, do not update
After writing the database, delete the cached key; the next read loads the new row. Two writers that each update the cache can finish in the wrong order and leave the older value there until the TTL ends.
Give every key a TTL
Even with invalidation, set a TTL. It is the safety net for a missed delete or a rare race. Choose it from how stale the data may be: seconds for prices, hours for a list of countries.
Collapse concurrent misses into one fill
Use single-flight or a short lock so one request rebuilds a hot key while the others wait. Add 10–20% random jitter to TTLs so keys written together do not expire together.
Use write-behind only where loss is acceptable
Write-behind is fast because the database write is delayed. If the cache node dies first, those writes are gone. Use it for view counters and analytics, not for orders or payments.

Errores habituales

  • Updating the cached value on write. Two concurrent writers finish in the wrong order, and the older value stays cached for a full TTL.
  • Deleting the key before writing the database. A read in between fills the cache with the old row.
  • Giving every key the same TTL, so keys loaded after a deploy all expire in the same second.
  • Not caching "not found", so requests for ids that do not exist always reach the database. Cache it with a short TTL.