DBAtlas · Database operations atlas

Decision: which caching strategy?

A cache keeps frequently read data in a fast layer in front of the database (e.g. Redis), lowering latency and DB load. But not all data is suitable for caching, and wrong invalidation creates a risk of stale/incorrect data. This tree first checks whether you really need a cache, then helps you choose between cache-aside, read-through, write-through and write-behind based on the read/write pattern, and covers preventing stampedes.

What does your workload look like: read-heavy or write-heavy?

A cache pays off most when the same data is read many times (read-heavy) and does not change often. First establish the overall pattern: is the data read a lot and changed rarely, is it updated constantly, or are you unsure whether adding a cache is worth it?

  • Very read-heavy, data changes relatively rarely
  • Writes are heavy too; written data is read immediately/often
  • Not sure; do I really need a cache?

Who should be responsible for populating the cache?

In a read-heavy scenario the key choice is who populates the cache. Should your application manage the cache explicitly and fall back to the DB itself (high flexibility), or should the cache layer fetch the data from the backing DB on a miss and return it transparently?

  • Let the application manage it explicitly; I want flexibility and control
  • Let the cache layer fetch from the DB on a miss (transparent)

On writes, is consistency or write speed the priority?

If writes are heavy, you must decide how consistent the written data will be between the cache and the DB. Should the cache and DB be updated together on every write to guarantee consistency (somewhat slower writes), or should writes go to the cache first and be applied to the DB asynchronously in the background (very fast writes but a risk of loss)?

  • Consistency matters; reads must always be current
  • Write speed is critical; slightly delayed consistency and risk of loss are acceptable

Recommendation: do not add a cache for now

A cache is not free: it introduces a consistency problem (stale data), extra infrastructure and invalidation complexity. If the query is already fast or the data changes on every request, a cache adds no benefit, only failure surface.

Recommendation: cache-aside (lazy loading)

With cache-aside the application checks the cache first; if the data is absent (miss) it reads from the DB, writes it to the cache and returns it. It is the most common and flexible pattern; the cache fills only with data that is actually requested.

Recommendation: read-through cache

With read-through the application talks only to the cache; on a miss, the cache layer itself fetches the data from the DB and populates it (through a provider/loader). Application code becomes simpler and the population logic lives in one place.

Recommendation: write-through cache

With write-through every write goes to both the cache and the DB (synchronously); both are updated together. The cache therefore never falls behind the DB and reads always see current data.

Recommendation: write-behind (write-back) cache

With write-behind the write goes to the cache first and returns to the user immediately; the DB write happens in the background, asynchronously (batched/delayed). Write speed is the highest, but it carries durability and consistency risks.

Open in DBAtlas →