キャッシュキーの設計とリクエスト集約
各レイヤーで効果的なキャッシュキーを設計する方法と、リクエスト集約によってオリジンへのThundering Herd負荷を防ぐ仕組みを学びます。
「キャッシュキーの設計とリクエスト集約」はCoddyKit上の無料Caching Strategies: Redis + CDN + Edge Computingレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはCaching Strategies: Redis + CDN + Edge Computing学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Caching Strategies: Redis + CDN + Edge Computingコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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.
よくある質問
「キャッシュキーの設計とリクエスト集約」レッスンは無料ですか?
はい。「キャッシュキーの設計とリクエスト集約」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Caching Strategies: Redis + CDN + Edge Computingコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Caching Strategies: Redis + CDN + Edge Computingコースには全4レッスンが含まれています。
「キャッシュキーの設計とリクエスト集約」で何を学びますか?
各レイヤーで効果的なキャッシュキーを設計する方法と、リクエスト集約によってオリジンへのThundering Herd負荷を防ぐ仕組みを学びます。 ブラウザで直接実行するハンズオンコードでCaching Strategies: Redis + CDN + Edge Computingを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Caching Strategies: Redis + CDN + Edge Computingを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのCaching Strategies: Redis + CDN + Edge Computingは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「キャッシュキーの設計とリクエスト集約」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このCaching Strategies: Redis + CDN + Edge Computingレッスンでコードを書いて実行できますか?
はい。すべてのCaching Strategies: Redis + CDN + Edge Computingレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- RedisとCDNの組み合わせ
- 多層キャッシュ戦略
- キャッシュ間のデータ整合性
- キャッシュキーの設計とリクエスト集約