DataUpdated 2026-11-20

Caching

Cache-aside, TTLs and the four ways to invalidate. What a cache buys, what it costs, and when I would not add one.

Keep the popular answers within arm's reach.

If the same question arrives ten thousand times, you should not walk to the back room ten thousand times. You keep the common answers on the counter and go to the back room only when you have to.

How it works

A cache stores frequently read data closer to the application, so repeated requests skip the slower source of truth. That source is usually the database.

Code
Without cache:   User → API → Database
With cache:      User → API → Cache → Database   (only on a miss)

A hit returns straight from the cache. A miss goes to the database, stores the result in the cache, and then returns it.

If 10,000 requests arrive and 90% of them read the same data, the database should not answer that 9,000 times. The cache absorbs the repeat. You get lower database load, lower latency and more headroom.

Cache-aside

This is the most common pattern.

Code
Read:   check cache
          ├── HIT  → return
          └── MISS → database → update cache → return

Write:  update database → invalidate cache

The application owns the logic. The cache is a convenience, never the record. If it is empty, the system is slower, not wrong.

TTL

Give each entry a time to live, for example 300 seconds. After that it expires and the next read refills it. A TTL is the simplest way to bound how stale an answer can get.

The hard part is invalidation

Code
Database: username = Zeeshan
Cache:    username = Zeeshan

User renames → Database: Ahmed
               Cache:    Zeeshan   ← stale

There are four common strategies:

  1. Write-through invalidation. Update or delete the cache key in the same operation as the write. Most correct, and it adds latency to writes.
  2. TTL only. Accept a bounded staleness window. Simple, and fine when slightly old data is harmless.
  3. Event-driven. Publish an event on write and let a subscriber invalidate keys. It scales across services and adds moving parts.
  4. Versioned keys. Put a version or updated_at in the key, so old keys stop being read and expire unused.

My choice follows the cost of being wrong. For prices or inventory, invalidate on write and keep a short TTL as a safety net. For a public bio or a view count, a TTL alone is enough.

Redis

Redis is the usual home for a shared cache: an in-memory store with fast reads and simple data structures. It also holds sessions and rate-limit counters. It is not where I keep data I cannot lose. It can evict or drop data on restart unless you configure persistence, so the database stays the source of truth.

Where I used it

I haven't shipped this in a public project yet.

One related decision is on record. In IMS I chose pg-boss so that jobs are durable and tied to the business transaction, without Redis. The queue load sits on the database. That is a choice to run one fewer moving part, not a caching decision, and I mention it because Redis is the part of this note I deliberately did not add.

Trade-offs

  • Staleness is the price. Every cache can serve an answer that is no longer true. You decide how long that is acceptable, per piece of data.
  • Invalidation is real work. A cache turns one source of truth into two. Keeping them aligned is the hard part, and bugs here are quiet.
  • Stampedes. When a popular entry expires, many requests can miss at the same moment and hit the database together. Plan for it before the hot key expires, not after.
  • Another piece to run. A cache needs monitoring, memory limits and an eviction policy.
  • Do not cache what must be exact. A real-time balance before a withdrawal should come from the source, unless you accept the invalidation risk. Data read so rarely that the cache never warms is not worth the overhead either.

I cache when I have a read-heavy path I have measured, not as a first move. A query that is slow because of a missing index needs an index, not a cache.