Dynamic Key Management: JWKS Endpoints, Public Key Rotation (kid) & Cache-Stampede Protection
A JSON Web Key Set (JWKS, RFC 7517) is an HTTP endpoint published by an Authorization Server exposing public keys in standardized JSON format. Key rotation allows servers to retire old keys and adopt new ones without application downtime by matching the Key ID (kid) in the token header, protected by caching and rate limiting against cache stampedes.
Step 1: Auth Server exposes public keys as a JSON structure at /.well-known/jwks.json.
Cryptographic keys must be rotated periodically (e.g. every 90 days) or immediately if suspected of compromise. If public keys were hardcoded in every microservice, rotating a key would require redeploying 50 applications simultaneously. JWKS enables automated, zero-downtime key discovery.
The Embassy Passport Bulletin: Rather than printing every border guard a new physical handbook every time an ambassador's signature changes, the embassy publishes the current valid signature seals on an official public bulletin board. Guards check the bulletin and keep a cached copy in their booth.
[ CosmosApp Auth Server ] ──(Publishes JWKS)──► https://auth.cosmos.app/.well-known/jwks.json
{ "keys": [
{ "kid": "key_2026_q1", "alg": "RS256", "n": "...", "e": "AQAB" },
{ "kid": "key_2026_q2", "alg": "RS256", "n": "...", "e": "AQAB" }
] }
│
▼ (Cached for 24h in Redis / Local RAM)
[ Microservice API Gateway ]
│
├── Incoming Token Header: { "kid": "key_2026_q2" }
├── Lookup in Local Cache ──► Found! Verify Signature.
└── Unknown kid? ──► Rate-limited single background fetch (stampede defense)CosmosApp conducts quarterly cryptographic key rotations. To prepare for Q2, it generates a new RSA key pair ('key_2026_q2') and adds its public key to its JWKS endpoint alongside 'key_2026_q1'. Microservices cache the JWKS for 24 hours. When CosmosApp begins signing new tokens with Q2, microservices seamlessly match the kid without dropped requests or redeployments.
Step 1: Auth Server exposes public keys as a JSON structure at /.well-known/jwks.json.
Step 2: Microservices fetch the JWKS on startup and store keys in a local in-memory cache.
Step 3: Incoming request arrives; microservice inspects JWT header to extract the Key ID ('kid').
Step 4: If key with matching 'kid' exists in cache, it verifies the signature immediately.
Step 5: If 'kid' is unknown (indicating recent key rotation), microservice triggers a rate-limited cache refresh.
Step 6: If 'kid' is still missing after refresh, token is rejected as untrusted.
import { createRemoteJWKSet, jwtVerify } from 'jose';
// CosmosApp Hardened JWKS Verification with Caching & Rate Limiting:
const jwksUri = new URL('https://auth.cosmos.app/.well-known/jwks.json');
const JWKS = createRemoteJWKSet(jwksUri, {
cacheMaxAge: 24 * 60 * 60 * 1000, // Cache public keys for 24 hours
cooldownDuration: 30 * 1000, // Max 1 remote fetch per 30s for unknown kid (stampede defense!)
});
export async function verifyWithJwks(token: string) {
return await jwtVerify(token, JWKS, {
issuer: 'https://auth.cosmos.app',
audience: 'https://api.cosmos.app',
});
}- ✕Microservices should fetch JWKS on every single incoming API request (Fetching JWKS per request destroys performance and allows DoS attacks; JWKS must be aggressively cached).
- ✕Any unknown kid should trigger an immediate remote JWKS fetch (Attackers can flood servers with random kid values to trigger a cache stampede / denial of service).
Key Architectural Takeaways
- ✓JWKS decouples key rotation on the identity provider from microservice deployments.
- ✓Always match keys using the Key ID ('kid') header parameter.
- ✓Enforce cooldown limits on remote JWKS fetches to prevent cache-stampede denial-of-service attacks.
Fetching the JWKS URL synchronously on every incoming request when an unknown 'kid' is encountered.
Use a rate-limited cooldown window (e.g. 1 fetch per 30 seconds) to defend against cache-stampede attacks.
What is the purpose of the 'kid' (Key ID) header parameter in a JSON Web Token?