Progettazione delle cache key e request coalescing
Scopra come progettare cache key efficaci tra i diversi livelli e come il request coalescing prevenga il carico da thundering herd sull’origine.
Progettazione delle cache key e request coalescing è una lezione Caching Strategies: Redis + CDN + Edge Computing gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Caching Strategies: Redis + CDN + Edge Computing, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Caching Strategies: Redis + CDN + Edge Computing include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Impara Caching Strategies: Redis + CDN + Edge Computing con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 48
Domande Frequenti
La lezione «Progettazione delle cache key e request coalescing» è gratuita?
Sì — il testo completo di «Progettazione delle cache key e request coalescing» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Caching Strategies: Redis + CDN + Edge Computing, passa a CoddyKit PRO. Il corso Caching Strategies: Redis + CDN + Edge Computing include 4 lezioni in totale.
Cosa imparerò in «Progettazione delle cache key e request coalescing»?
Scopra come progettare cache key efficaci tra i diversi livelli e come il request coalescing prevenga il carico da thundering herd sull’origine. Eserciti Caching Strategies: Redis + CDN + Edge Computing con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Caching Strategies: Redis + CDN + Edge Computing?
Non è richiesta alcuna esperienza precedente. Caching Strategies: Redis + CDN + Edge Computing su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Progettazione delle cache key e request coalescing»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Caching Strategies: Redis + CDN + Edge Computing?
Sì. Ogni lezione Caching Strategies: Redis + CDN + Edge Computing include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Combinare Redis e CDN
- Strategia di caching multilivello
- Coerenza dei dati tra le cache
- Progettazione delle cache key e request coalescing