Cache Key Design & Request Coalescing
Learn how to design effective cache keys across layers and how request coalescing prevents thundering-herd load on your origin.
Cache Key Design & Request Coalescing is a free Caching Strategies: Redis + CDN + Edge Computing 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 Caching Strategies: Redis + CDN + Edge Computing learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Cache Key?
A cache key uniquely identifies a cached entry. When a request arrives, the cache computes its key and looks for a matching stored response.
- Same key equals a cache hit
- Different key equals a separate entry (or a miss)
- Poor key design causes low hit ratios or wrong responses
Default Cache Keys
By default many caches key on the full URL including host and path. But query strings, headers, and cookies can also be part of the key, which dramatically affects how often you get hits.
Normalizing Query Strings
Tracking parameters like utm_source create needless cache fragmentation. Strip or ignore irrelevant query params so equivalent requests share one entry.
cache_key = url_without_params(request.url) + "?" + only_keep(request.query, ["id", "page"])Varying on Headers
The Vary header tells caches to store separate copies per header value. Useful for content negotiation but dangerous if overused: Vary: User-Agent can explode into thousands of entries.
Vary: Accept-EncodingCookies and Cache Keys
Cookies often make responses uncacheable because each user has a unique cookie. Strip cookies for static assets, and only include the specific cookies that truly change the response.
Device and Geo Variants
Sometimes you intentionally vary by device class or country. Add a small, controlled dimension to the key (like a normalized device type) rather than the raw header to keep cardinality low.
- Good: mobile vs desktop
- Bad: full User-Agent string
The Thundering Herd Problem
When a popular item expires, many requests can simultaneously miss and hammer the origin at once. This thundering herd can overwhelm a backend during peak traffic.
Request Coalescing
Request coalescing (also called collapsed forwarding) makes the cache send only one request to the origin for a given key while other concurrent requests wait for that single fetch.
proxy_cache_lock on;
proxy_cache_lock_timeout 5s;Stale-While-Revalidate
To smooth expiry, serve slightly stale content while a background refresh runs. This avoids both the herd and a latency spike for users.
Cache-Control: max-age=60, stale-while-revalidate=120Multi-Layer Key Consistency
In a Redis plus CDN plus edge stack, keep cache keys consistent across layers so they agree on what is the same object. Mismatched keys cause duplicate storage and confusing invalidation.
Designing Keys: Checklist
A solid cache key strategy:
- Strip tracking and irrelevant params
- Vary only on headers that change the response
- Avoid keying on raw cookies or User-Agent
- Enable coalescing for hot keys
- Use stale-while-revalidate to hide refresh latency
Quick Check
Test your understanding of cache keys and coalescing.
Recap
You learned how cache key design controls hit ratios: normalizing query strings, using Vary carefully, and avoiding high-cardinality dimensions. You also saw how request coalescing and stale-while-revalidate protect the origin from thundering-herd spikes across a multi-layer cache.
Frequently asked questions
Is the “Cache Key Design & Request Coalescing” lesson free?
Yes — the full text of “Cache Key Design & Request Coalescing” is free to read here on the web, and the Caching Strategies: Redis + CDN + Edge Computing 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 Caching Strategies: Redis + CDN + Edge Computing course, upgrade to CoddyKit PRO.
What will I learn in “Cache Key Design & Request Coalescing”?
Learn how to design effective cache keys across layers and how request coalescing prevents thundering-herd load on your origin. You practise Caching Strategies: Redis + CDN + Edge Computing 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 Caching Strategies: Redis + CDN + Edge Computing?
No prior experience is required. Caching Strategies: Redis + CDN + Edge Computing 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 Key Design & Request Coalescing” 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 Caching Strategies: Redis + CDN + Edge Computing lesson?
Yes. Every Caching Strategies: Redis + CDN + Edge Computing 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
- Combining Redis and CDN
- Multi-Layer Caching Strategy
- Data Consistency Across Caches
- Cache Key Design & Request Coalescing