スケールに備えたトークン検証のキャッシュ
セキュリティを損なわずにJWKSキーと検証結果をキャッシュし、高トラフィック時のJWT検証の負荷を軽減する方法を学びます。
「スケールに備えたトークン検証のキャッシュ」はCoddyKit上の無料Spring Security 6 & JWT Authenticationレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSpring Security 6 & JWT Authentication学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Spring Security 6 & JWT Authenticationコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
The Cost of Validation
Every request to a JWT-protected API runs signature verification and claim checks. At thousands of requests per second, repeated work, especially fetching public keys, becomes a bottleneck.
What Is Safe to Cache
Not everything should be cached. Safe to cache:
- The public keys (JWKS) used to verify signatures
- Expensive parsed metadata
Risky: caching a final allow decision for too long can let a revoked token slip through.
Caching the JWKS
Fetching the JWKS endpoint on every request is wasteful. Cache the key set and refresh it periodically or when an unknown kid appears.
// pseudo: refresh keys at most once per 10 minutes
if (now - keysFetchedAt > 600000) {
keys = fetchJwks();
keysFetchedAt = now;
}Refresh on Unknown kid
If a token carries a kid not in the cache, the signing key may have rotated. Force a one-time refresh before rejecting, so legitimate new tokens are accepted promptly.
let key = keys[kid];
if (!key) { keys = fetchJwks(); key = keys[kid]; }
if (!key) reject('unknown key');Local Verification Beats Introspection
Self-contained JWTs can be verified locally with the cached public key, avoiding a network call per request. This is far faster than remote token introspection.
Short-Lived Decision Cache
You may cache the parsed claims for a token's lifetime keyed by the token hash, but the cache entry's TTL must never exceed the token's own exp.
ttl = Math.min(claims.exp - now, MAX_CACHE_TTL);
cache.set(hash(token), claims, ttl);The Revocation Tradeoff
Caching a decision means a revoked token might still be accepted until the cache entry expires. Keep this TTL short (seconds) when you support revocation, so the stale window stays tiny.
Spring's Built-In JWKS Cache
Spring's NimbusJwtDecoder already caches the JWKS internally and handles refresh, so for many apps you get caching for free just by configuring the JWK set URI.
JwtDecoder decoder = NimbusJwtDecoder
.withJwkSetUri(jwksUri)
.build();Measuring the Win
Always measure before and after. Track average validation latency and JWKS fetch count. Caching that does not move your metrics adds complexity for no gain.
Cache Stampede Protection
When a cached key expires, many requests may refresh at once. Use a single-flight lock so only one thread fetches the new JWKS while others wait.
if (refreshing) await refreshPromise;
else { refreshing = true; refreshPromise = fetchJwks(); }Distributed Caches
In a multi-instance deployment, a shared cache like Redis avoids each node refetching keys independently and keeps a consistent view of revocation state.
Quick Check
Test your understanding of caching token validation.
Recap
You learned to scale JWT validation with caching:
- Cache the JWKS public keys; refresh on unknown kid
- Local verification avoids per-request network calls
- Decision caches must respect the token's exp and stay short when revocation matters
- Use single-flight refresh and distributed caches at scale
Smart caching cuts latency while keeping security intact.
よくある質問
「スケールに備えたトークン検証のキャッシュ」レッスンは無料ですか?
はい。「スケールに備えたトークン検証のキャッシュ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Spring Security 6 & JWT Authenticationコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Security 6 & JWT Authenticationコースには全4レッスンが含まれています。
「スケールに備えたトークン検証のキャッシュ」で何を学びますか?
セキュリティを損なわずにJWKSキーと検証結果をキャッシュし、高トラフィック時のJWT検証の負荷を軽減する方法を学びます。 ブラウザで直接実行するハンズオンコードでSpring Security 6 & JWT Authenticationを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Spring Security 6 & JWT Authenticationを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSpring Security 6 & JWT Authenticationは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「スケールに備えたトークン検証のキャッシュ」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSpring Security 6 & JWT Authenticationレッスンでコードを書いて実行できますか?
はい。すべてのSpring Security 6 & JWT Authenticationレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 短期間のJWTとリフレッシュサイクル
- JWTのブラックリストとホワイトリスト
- JWTのパフォーマンスに関する考慮事項
- スケールに備えたトークン検証のキャッシュ