Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 37 ยท Strategies and Invalidation

The Cache-Aside Pattern

Learn cache-aside, also called lazy loading: check the cache, load on a miss, store the result, and handle writes and negative results correctly, with runnable code.

Now that you know the individual commands, the next question is how an application should use them. There are a handful of well-known patterns, and the most common one by far with Memcached is cache-aside, also known as lazy loading. The application itself talks to both the cache and the database and keeps them in step. Memcached stays dumb and simple.

How cache-aside works

Read path

  1. Look in the cache for the key.
  2. On a hit, return the value.
  3. On a miss, read from the database, store the result in the cache with a TTL, and return it.

Write path

  1. Write to the database (the source of truth).
  2. Delete the cached key (or, less often, update it).
sequenceDiagram
  participant App
  participant Cache as Memcached
  participant DB as Database
  App->>Cache: get product:42
  Cache-->>App: miss
  App->>DB: SELECT ... WHERE id = 42
  DB-->>App: row
  App->>Cache: set product:42 (TTL 300)
  Note over App,DB: Later, a write
  App->>DB: UPDATE product 42
  App->>Cache: delete product:42

Reads fill the cache on demand; writes go to the database and remove the stale copy.

Only data that is actually requested ends up cached, so memory is spent on what matters. If Memcached dies, the application keeps working: every request becomes a miss and the database serves everything (slowly).

Implementing it

Python Cache-aside read path

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The write path: delete, don't update

After changing a product you have two choices: update the cached value or delete it. Deleting is usually better.

  • It is simple: the next read rebuilds the value from the database, the single source of truth.
  • It avoids storing a value computed from partial data, or in an inconsistent format.
  • It avoids a race where two concurrent writers update the database in one order and the cache in the other, leaving the cache permanently wrong.
Python Update the database, then delete the cache key

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The order matters: write the database first, then delete the key. If you delete first, a concurrent reader can reload the old row into the cache between your delete and your database write, and the stale value stays until the TTL ends.

Negative caching

If someone asks for a product that does not exist, every request reaches the database, because there is nothing to cache. Attackers can exploit that by requesting random ids. The fix is to cache the absence for a short time using a sentinel value, so you can tell "cached: not found" from "not in the cache".

Python Cache the fact that something is missing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Keep the TTL for negatives short (seconds), otherwise a newly created item appears to be missing for too long.

Strengths and weaknesses

Strengths Weaknesses
Simple, widely understood Application code carries the logic; every caller must do it right
Only requested data is cached The first request for each key is slow (a cold miss)
A cache failure is not fatal Data can be stale between a database change and the cache refresh
Cache and database schemas are independent Races are possible between a reader and a writer

The remaining race

Even with correct ordering, cache-aside has a small race: reader R misses and reads the old row; writer W updates the database and deletes the key; R then stores its old row. The cache now holds stale data until the TTL expires. The window is tiny because the reader's database read and cache write must straddle the writer's two steps, but on a busy system it happens. Always set a TTL on cache-aside entries as a safety net. Stronger techniques, such as versioned keys, appear in later lessons.

Practical checklist

  • Wrap the cache lookup in a function or class, not in every controller.
  • Always use a TTL.
  • Delete on write; write the database first.
  • Treat cache errors as misses.
  • Cache negative results briefly.
  • Log hit and miss counts so you can measure the result.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. On a cache miss in cache-aside, who loads the data from the database?

    Memcached does not know about the database. The application loads the row and stores it in the cache.
  2. 2. Why is deleting the key usually preferred to updating it after a write?

    The next read rebuilds the value from the database, so the cache cannot hold something the database never had.
  3. 3. What is negative caching?

    A sentinel with a short TTL stops repeated lookups for missing items from reaching the database.
  4. 4. Which order is correct on a write?

    Deleting first lets a concurrent reader refill the cache with the old row before the database changes.

Summary

  • Cache-aside: read the cache, load on a miss, store the result; on writes, update the database then delete the key.
  • Only requested data is cached, and a cache failure just means slower responses.
  • Cache negative results briefly with a sentinel.
  • A small reader-writer race remains, so always use a TTL as a safety net.
  • Wrap the logic in one place instead of scattering it through the code.

Next: Read-Through, Write-Through and Write-Behind