Cache Invalidation Patterns
Apply cache-aside, write-through, and TTL-based invalidation strategies.
Cache Invalidation Patterns is a free PHP Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Hard Problem
"There are only two hard things in Computer Science: cache invalidation and naming things." — Phil Karlton. Knowing when and how to invalidate cached data is critical.
TTL-Based Expiry (Passive)
The simplest strategy: set a TTL and accept that the cache may serve stale data for up to that duration. Works when slight staleness is acceptable.
<?php
Cache::put("user:1", $user, 300); // expires in 5 minutes automaticallyWrite-Through Invalidation
When you update data, immediately delete or update the corresponding cache key.
<?php
function updateUser(int $id, array $data): void {
User::find($id)->update($data);
Cache::forget("user:$id"); // delete cache
// Or re-warm:
Cache::put("user:$id", User::find($id), 300);
}Cache Tagging
Tag related cache entries so you can invalidate a whole group at once — e.g. all cache entries for a specific user.
<?php
// Store with tags:
Cache::tags(["user:1", "posts"])->put("user:1:recent-posts", $posts, 600);
// Invalidate all "user:1" entries:
Cache::tags("user:1")->flush();Event-Driven Invalidation
In Laravel, use Eloquent model events to automatically invalidate cache when a model is saved or deleted.
<?php
class User extends Model {
protected static function booted(): void {
static::saved(fn($u) => Cache::forget("user:{$u->id}"));
static::deleted(fn($u) => Cache::forget("user:{$u->id}"));
}
}Cache-Aside with Versioning
Append a version number to cache keys. Incrementing the version effectively invalidates all existing entries without deleting them.
<?php
$v = Cache::get("user:version", 1);
$key = "user:{$id}:v{$v}";
// To invalidate: increment the version
Cache::increment("user:version");Probabilistic Early Expiration (PER)
To prevent cache stampedes, re-compute the cache slightly before it expires for some fraction of requests. Avoids simultaneous expiry across all servers.
Background Refresh
A background job re-populates frequently-read cache entries before they expire. The cache is never empty — reads are always fast.
Stale-While-Revalidate
Serve stale data immediately while asynchronously refreshing the cache in the background. Eliminates cache-miss latency spikes.
Distributed Cache Invalidation
In a multi-server setup, one server invalidating a cache key does not automatically clear other servers' local caches. Use Redis pub/sub or a shared store to broadcast invalidations.
Cache Hierarchy
Layer caches: local in-process cache (APCu) in front of a distributed cache (Redis). Local cache is faster but must be invalidated across all instances when data changes.
Summary
Common patterns: TTL expiry (simple), write-through delete (accurate), tagging (grouped), event-driven (automatic). Choose based on how strongly you need data freshness vs. simplicity.
Quick Check
What does Cache::tags()->flush() do?
Frequently asked questions
Is the “Cache Invalidation Patterns” lesson free?
Yes — the full text of “Cache Invalidation Patterns” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Cache Invalidation Patterns”?
Apply cache-aside, write-through, and TTL-based invalidation strategies. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Cache Invalidation Patterns” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why Caching Matters in PHP
- Redis as a PHP Cache Backend
- Memcached for Session and Object Caching
- Cache Invalidation Patterns