Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

After the identity provider rotated its signing keys, some pods of a resource server reject valid tokens with 401 invalid_token and "Signed JWT rejected: Another algorithm expected, or no matching key(s) found" while other pods accept them. How do you analyse it?

The rejected tokens carry a kid, a key id, that is not in the JWKS key set the failing pods have cached, so those pods cannot find a key to check the signature. The decoder normally refetches the key set when it sees an unknown kid, so we find out why the refetch failed on those pods (network, proxy, TLS, rate limits) and whether the provider published the new key before using it, and we also check the token's algorithm against the one the decoder accepts.

Why this question is asked

Key rotation is routine for identity providers, and it should be invisible. When it is not, the failure is confusing: the same token works on one pod and fails on the next, so the identity provider looks fine and the service looks flaky. Interviewers ask this because it tests whether we understand how a resource server finds its keys, how it caches them, and how to reason about a failure that only some instances show. In this answer, we will recap how keys are looked up, explain what the error really means, work out why only some pods fail, check the algorithm, and set up logging and caching so the next rotation goes unnoticed.

1A Quick Recap of kid and the JWKS

A signed JWT has a header, a payload and a signature. The header names the algorithm in alg, for example RS256, and the key in kid. The identity provider publishes its public keys as a JWKS, a JSON Web Key Set, at a URL such as /.well-known/jwks.json. Each key in that set has its own kid.

Spring Security's NimbusJwtDecoder downloads the JWKS, caches it, and for each token picks the key whose kid matches the header. If it finds one with a suitable algorithm, it checks the signature. If it finds none, the Nimbus library reports "Signed JWT rejected: Another algorithm expected, or no matching key(s) found", and Spring turns that into a 401 with invalid_token. So the message has exactly two possible meanings: no key with this kid, or the key exists but the algorithm is not one we accept.

2What Happens When a New kid Arrives

A decoder does not refetch the JWKS for every request; that would put the identity provider on the path of every API call. Instead it keeps the set in a cache and, when a token arrives with a kid it does not know, it refetches the set once, assuming a rotation happened. To protect the provider from floods of tokens with made-up kids, this refetch is rate limited, so a pod will not try again for a short while after a refetch. The exact cache lifetime and limits depend on your Spring Security and Nimbus versions, so check your version.

Step throughHow a pod looks up the signing key
  1. Read kid and alg from the token header
  2. Is kid in the cached JWKS?
    YesVerify the signatureNoTry to refetch the JWKS
  3. Did the refetch succeed?
    YesLook for kid in the new setNoReject: no matching key
  4. Is kid in the new set?
    YesCache it and verifyNoReject: no matching key
  5. Is alg an accepted algorithm?
    YesCheck claims and acceptNoReject: another algorithm expected

5 steps in order. Play them, or step through them one at a time.

Every path that ends in a rejection produces the same message, so we must find which one the failing pods took.

3Why Only Some Pods Fail

Pods that work have either fetched the JWKS after the provider published the new key, or refetched successfully when the first new token arrived. Pods that fail took a different path, and there are a few common reasons.

The first is that their refetch failed. The pod may sit on a node whose egress goes through a proxy that blocks the JWKS host, its DNS may resolve the provider to an old address, or its trust store may not include a new TLS certificate on the provider's endpoint. Because the refetch fails, the pod keeps its old cache and rejects every new token. The logs at DEBUG level for the decoder usually show the failed request.

The second is the rate limit combined with timing. If the pod refetched just before the provider published the new key, it saw the old set, and the rate limit stops it from trying again immediately. It recovers on the next allowed refetch, which looks like a burst of failures that clears by itself.

The third is on the provider side. A good rotation publishes the new key, waits until verifiers have had time to see it, and only then starts signing with it, keeping the old key published until old tokens expire. If the provider started signing immediately, or its own nodes serve different key sets behind a load balancer, pods that fetch from different provider nodes see different sets. We can check that by calling the JWKS URL several times from a failing pod and comparing the kids.

Properties
# from inside a failing pod
curl -s https://login.example.com/.well-known/jwks.json | jq -r '.keys[].kid'
# 2026-10-key
# (the new kid 2026-11-key is missing, or the call fails with a TLS or proxy error)

4Do Not Forget the Algorithm

The same message appears when the kid matches but the algorithm does not. NimbusJwtDecoder accepts RS256 by default. If the provider rotated to an EC key and now signs with ES256, or to PS256, every pod configured with the default rejects every new token. The difference from the cache problem is that all pods fail, not some. The fix is to declare the algorithms we accept.

YAML
spring:
security:
oauth2:
resourceserver:
jwt:
jwk-set-uri: https://login.example.com/.well-known/jwks.json
jws-algorithms: RS256, ES256 # accept both during the change

We keep the list short on purpose. Accepting every algorithm is not safer; it only widens what an attacker can try.

5Logging and Alerting for the Next Rotation

To make the next incident short, we log the kid and alg of rejected tokens, never the token itself, because a logged token can be replayed. We also alert when a JWKS fetch fails, since that is the early sign of the problem above. With those two signals, the analysis takes minutes: if the kid is unknown and the fetch is failing, it is the pod's network; if the kid is unknown and the fetch succeeds, it is the provider; if the kid is known, it is the algorithm.

6Follow-up questions

How would you configure JWKS caching explicitly?

The builder from NimbusJwtDecoder.withJwkSetUri(uri) accepts a Spring Cache through its cache method, so we can back the key set with a Caffeine cache whose lifetime we choose, for example five minutes, and it accepts our own RestOperations so we can set connect and read timeouts and a proxy. For full control we can build a Nimbus JWKSource ourselves with JWKSourceBuilder, choosing the cache time, refresh behaviour and rate limit, and pass a JWT processor built on it to the NimbusJwtDecoder constructor. Check your version, because the builder methods have changed over time.

Would restarting the failing pods fix it?

Often it hides it. A restart forces a fresh fetch, so if the provider now publishes the key, the pod recovers. But if the cause was a blocked proxy or a missing certificate, the new pod fails to fetch at startup or on the next rotation. We restart only after we know why the refetch failed.

Why not refetch the JWKS on every unknown kid without any limit?

Because anyone can send us a token with a random kid. Without a rate limit, each such request would make our service call the identity provider, which turns a cheap attack into load on a system every service depends on.

7Conclusion

In this answer, we saw that the error means the decoder found no key matching the token's kid, or found one with an algorithm it does not accept. Pods differ because each keeps its own JWKS cache: those whose refetch failed through a proxy, DNS or TLS problem, or was blocked by the rate limit, keep rejecting new tokens. We checked the provider publishes keys before using them, compared the algorithm with the RS256 default, and added kid logging and fetch alerts so the next rotation is quick to diagnose.

What to say in the interviewAbout 80 seconds aloud

Open with

My first thought is that the tokens carry a kid the failing pods do not have in their cached JWKS. NimbusJwtDecoder picks the key by kid from the header, and when it sees an unknown kid it refetches the key set once, with a rate limit.

Then, if they want more

So pods that fetched after the new key was published, or refetched successfully, work, and pods whose refetch failed keep rejecting. I would exec into a failing pod and fetch the JWKS URL myself: a proxy block, a DNS problem or a TLS trust issue shows up immediately, and the DEBUG logs of the decoder show the failed fetch.

I would also call the JWKS several times to see whether different provider nodes serve different key sets, and check that the provider published the new key before signing with it and kept the old one during the overlap.

Then I check the algorithm: the same message appears if the new key is ES256 and the decoder only accepts the default RS256, but then every pod fails, not some. Going forward I would log the kid and alg of rejected tokens, never the token, alert on JWKS fetch failures, and configure the cache and HTTP timeouts explicitly.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "After the identity provider rotated its signing keys, some pods of a resource server...": https://scrivio-interview-library.netlify.app/spring-boot/oauth2-and-oidc/42359bc804/