Series — 7 Cache Patterns (1 of 8). This post covers cache-aside (also called lazy loading). Next: read-through. The full comparison hub publishes last: 7 Cache Patterns at a Glance.
Introduction
Most apps that feel fast under load are not hitting the database on every request. They check a cache first—Redis, Memcached, or an in-process store—and only talk to MySQL/Postgres when the cache does not have the answer.
Cache-aside is the pattern you will see most often. The application owns the logic: check cache → on miss, read the database → put the result in the cache → return it. Frameworks like Spring’s @Cacheable and Laravel’s Cache::remember() implement this idea.
This guide explains cache-aside with a clear diagram, a real product-page example, when to use it (and when not to), and a practical Laravel + Redis implementation you can copy into a project.
What is cache-aside?
In cache-aside, the application sits between the cache and the database. The cache never talks to the database by itself. Your code decides what to load, which key to use, and how long the value lives (TTL).
Other names you may hear:
- Lazy loading — the cache fills only when something is requested
- Cache-aside / lazy cache — same pattern; “aside” means the cache sits beside the app, not in front as a black box
Diagram: hit vs miss
Two paths matter. On a hit, the database stays idle. On a miss, the app loads from the database and warms the cache for the next callers.
Cache hit (fast path)
Cache miss (load + fill)
Rule: the app always talks to both systems. The cache is a helper, not the source of truth. The database remains the source of truth.
Real-world example: product detail page
Imagine an e-commerce site. Thousands of users open the same product URL:
- URL:
/products/blue-running-shoes - Data needed: name, price, stock flag, short description, image URLs
- This data changes a few times a day (price or stock), but is read constantly
Without a cache, every page view runs something like:
SELECT id, name, slug, price, in_stock, short_description
FROM products
WHERE slug = 'blue-running-shoes'
LIMIT 1;
Under a sale or a viral post, that query repeats thousands of times for the same row. Cache-aside fits perfectly: read-heavy, same key, acceptable if a price update appears a few seconds (or minutes) late until you invalidate.
Flow for this product
- Request arrives for slug
blue-running-shoes. - App builds cache key:
product:slug:blue-running-shoes. - Hit → return JSON/array from Redis. Done.
- Miss → query MySQL, store result in Redis with TTL (e.g. 10 minutes), return result.
- When an admin updates the price, app deletes (or updates) that cache key so the next reader reloads fresh data.
How to implement it (Laravel + Redis)
Laravel’s Cache::remember() is cache-aside in one call: get-or-load-and-store.
use App\Models\Product;
use Illuminate\Support\Facades\Cache;
public function show(string $slug)
{
$product = Cache::remember(
"product:slug:{$slug}",
now()->addMinutes(10),
function () use ($slug) {
return Product::query()
->select([
'id', 'name', 'slug', 'price',
'in_stock', 'short_description',
])
->where('slug', $slug)
->firstOrFail();
}
);
return view('products.show', compact('product'));
}
What happens inside remember:
- Look up
product:slug:...in the configured store (Redis in production). - If present, return it (hit).
- If missing, run the closure (DB read), write the value with TTL, return it (miss).
Invalidate on write (important)
Cache-aside does not update the cache automatically when you change the database. You must invalidate (or refresh) the key after updates:
public function update(Request $request, Product $product)
{
$product->update($request->validated());
// Drop stale entry so the next read reloads from DB
Cache::forget("product:slug:{$product->slug}");
// If the slug can change, also forget the old slug key
if ($product->wasChanged('slug')) {
Cache::forget("product:slug:{$product->getOriginal('slug')}");
}
return redirect()->route('products.show', $product);
}
Without invalidation, users can see the old price until the TTL expires. That is the classic cache-aside trade-off: simple reads, but you own freshness.
Spring equivalent (same pattern)
In Spring, @Cacheable is also cache-aside: the method runs only on a miss; the result is stored under the key.
@Cacheable(value = "products", key = "#slug")
public Product findBySlug(String slug) {
return productRepository.findBySlug(slug)
.orElseThrow();
}
@CacheEvict(value = "products", key = "#product.slug")
public Product update(Product product) {
return productRepository.save(product);
}
Different syntax. Same idea: app checks cache, loads on miss, evicts on write.
When to use cache-aside
- Read-heavy data — product pages, blog posts, config flags, category trees, user profiles that rarely change
- You want control — custom keys, selective fields, different TTLs per feature
- Multiple data sources — sometimes you join DB + API; the app can assemble the value before caching
- Simple mental model — easy to reason about in code reviews and debugging
When not to use cache-aside (alone)
- Write-heavy / write-then-read-immediately — consider write-through so the cache stays in sync on every write
- Data written often, almost never read — write-around avoids polluting the cache with cold keys
- Hot key expiry storms — when one popular key expires, many requests miss at once (cache stampede). Pair cache-aside with request coalescing and/or refresh-ahead
- You need the cache library to own loading — that is read-through (e.g. Caffeine
LoadingCache)
TTL vs explicit invalidation
Two common freshness strategies:
- TTL only — key dies after N minutes; simple, but stale until expiry
- TTL + forget on update — best default for editable content (products, pages, settings)
Practical tip: use a short TTL as a safety net even when you invalidate on write. If one code path forgets to call Cache::forget, TTL still limits how long bad data lives.
Common mistakes
- Caching huge objects — cache only what the page needs, not the full Eloquent model graph with relations you never render
- Unstable keys — include tenant id, locale, or currency in the key when those change the payload (
product:slug:en:inr:...) - Caching null forever — decide whether “not found” should be cached briefly to protect the DB from hammering missing slugs
- Forgetting invalidation — updates look “broken” in production while local (no Redis) looks fine
- Same TTL for everything — homepage config can live longer than stock counts
How to verify it works
- Open the product page once — expect a DB query (miss).
- Open it again — expect no product query (hit). Use Laravel Debugbar, Telescope, or Redis
MONITOR. - Update the price in admin — confirm the key is forgotten.
- Reload the page — one DB query again, then hits resume.
Summary
Cache-aside = the app checks the cache; on a miss it reads the database and stores the result. Use it for read-heavy data when you are happy to own keys, TTLs, and invalidation. Laravel Cache::remember and Spring @Cacheable are everyday implementations of this pattern.
Next in the series: Read-through — when the cache itself loads from the database and the app only talks to the cache.
Series note: posts 2–7 cover the other patterns. The final post is the overview hub with a decision table for all seven.