Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

A Dropwizard authenticator validates tokens by calling the identity provider on every request, and the provider becomes a bottleneck. How would you improve it?

If the tokens are JWT access tokens, we stop calling the provider per request and validate them locally with its public keys, fetched from the JWKS endpoint, cached, and refreshed when an unknown key id appears. If they are opaque tokens that need introspection, we wrap the authenticator in Dropwizard's CachingAuthenticator with a short expiry and a size limit, keep that expiry below the revocation delay we can accept, watch hit rate and latency, and fail closed with a clear 401 or 503 when validation is impossible.

Why this question is asked

Authentication runs on every single request, so whatever it costs is multiplied by our whole traffic. An authenticator that makes one network call per request turns the identity provider into a shared bottleneck and a single point of failure for every service that uses it. Interviewers ask this because the fix depends on understanding what kind of token we have and what a cache does to security. In this answer, we will recap the two kinds of tokens, validate JWTs locally, cache introspection results for opaque tokens, choose an expiry from the revocation delay, and decide how to fail when validation is impossible.

1A Quick Recap: Self-Contained and Opaque Tokens

An identity provider, the service that signs users in and issues tokens, can issue two kinds of access tokens. A JWT (JSON Web Token) is self-contained: it carries its claims, such as the subject, the issuer, the audience and the expiry time, and a digital signature. Anyone with the provider's public key can verify the signature and read the claims without asking the provider.

An opaque token is just a random string. It means nothing on its own, so the service must ask the provider about it through the introspection endpoint defined in RFC 7662, which answers whether the token is active and whom it belongs to. That call is what our current authenticator makes for every request.

So the first question in this scenario is which kind of token we receive. Many teams use JWTs but still call introspection or the userinfo endpoint out of habit, and for them the fix is simply to stop.

2Fix 1: Validate JWTs Locally With JWKS

The provider publishes its public keys as a JSON Web Key Set at a JWKS URL, usually listed in its OpenID Connect discovery document. Each key has a key id (kid), and each JWT header names the kid that signed it. Our authenticator fetches the key set once, caches it, and verifies tokens in memory. Here is a sketch with the Nimbus JOSE + JWT library; method names differ slightly between versions, so check yours.

Java
public class JwtAuthenticator implements Authenticator<String, User> {
private final DefaultJWTProcessor<SecurityContext> processor = new DefaultJWTProcessor<>();
public JwtAuthenticator(URL jwksUrl, String issuer, String audience) {
JWKSource<SecurityContext> keys = JWKSourceBuilder.create(jwksUrl).build(); // caches keys, refetches on unknown kid
processor.setJWSKeySelector(new JWSVerificationKeySelector<>(JWSAlgorithm.RS256, keys));
processor.setJWTClaimsSetVerifier(new DefaultJWTClaimsVerifier<>(
audience, new JWTClaimsSet.Builder().issuer(issuer).build(), Set.of("sub", "exp")));
}
@Override
public Optional<User> authenticate(String token) throws AuthenticationException {
try {
JWTClaimsSet claims = processor.process(token, null); // signature, iss, aud, exp checked in memory
return Optional.of(User.from(claims));
} catch (ParseException | BadJOSEException e) {
return Optional.empty(); // bad or expired token: 401
} catch (JOSEException e) {
throw new AuthenticationException("cannot verify token", e); // keys unavailable
}
}
}
// Before: 1 IdP call per request, p99 auth latency 120 ms
// After: 1 JWKS fetch per key rotation, auth latency well under 1 ms
Step throughValidating a JWT without calling the provider
ClientServiceKey cacheIdentity provider
GET JWKS (once, at start)
public keys kid=k1, k2
request with Bearer JWT, kid=k2
key for k2?
cached key
verify signature, iss, aud, exp
200 OK

7 messages in order. Play them, or step through them one at a time.

The provider is contacted only when the key set changes.

Notice how the key cache handles rotation. When the provider starts signing with a new key, the first token with an unknown kid makes the source refetch the key set, rate-limited so that a flood of forged kids cannot hammer the provider. Old keys stay in the set until their tokens have expired, so rotation causes no failures.

The cost of local validation is revocation: a JWT stays valid until it expires even if the user is disabled. That is why providers issue short-lived access tokens, often 5 to 15 minutes, and refresh them with refresh tokens that the provider can revoke.

3Fix 2: Cache Introspection Results for Opaque Tokens

When tokens really are opaque, we cannot avoid introspection, but we can avoid repeating it. Dropwizard ships CachingAuthenticator, a wrapper that keeps recent token-to-user answers in a Caffeine cache, so the provider sees one call per token per expiry period instead of one per request.

Java
# config.yml
authenticationCachePolicy: maximumSize=50000, expireAfterWrite=60s
// run()
Authenticator<String, User> introspecting = new IntrospectionAuthenticator(idpClient);
CachingAuthenticator<String, User> cached = new CachingAuthenticator<>(
environment.metrics(), introspecting, config.getAuthenticationCachePolicy());
// A client that sends 20 requests per second with the same token:
// without cache: 1,200 introspection calls per minute
// with cache: 1 introspection call per minute

Notice that we use expireAfterWrite, not expireAfterAccess. With expireAfterAccess, a busy client would keep its entry alive forever, and a revoked token would be accepted as long as it kept being used. maximumSize bounds memory; at roughly a few hundred bytes per entry, 50,000 entries stay in the tens of megabytes. CachingAuthenticator also has invalidate methods, so a logout or revocation event that reaches the service can remove an entry at once.

4Choosing the Expiry From the Revocation Delay

Every cache is a promise that we may act on old information for up to its expiry. So the right question is not how long we can cache, but how long the business can accept a revoked token still working. If security says a disabled employee must lose access within 2 minutes, the cache expiry must be well under that, for example 60 seconds, and the JWT lifetime must also fit the same rule.

We then measure whether the cache helps. CachingAuthenticator registers metrics for hits and misses, and we add a timer for authentication latency. A hit rate of 95 percent or more is typical for API clients that reuse tokens; a low hit rate means each request carries a new token, and the cache only adds memory.

5Failing Closed With Clear Status Codes

Sometimes validation is impossible: the JWKS endpoint is down and the token has a kid we have never seen, or introspection times out. The service must then fail closed, meaning it refuses the request rather than letting it through unchecked. Letting traffic through because the provider is down would turn an availability problem into a security hole.

The response code should tell the client what happened. An invalid or expired token is the client's problem, so it gets 401 and should fetch a new token. Being unable to check is our problem, so it should be 503 with Retry-After, so that clients retry later instead of forcing users to sign in again. By default dropwizard-auth turns an AuthenticationException into a 500, so if we want a 503 we map it ourselves, for example by throwing a ServiceUnavailableException; check your version's behaviour. Because cached keys and cached results keep working during a short provider outage, this path should be rare.

6Follow-up questions

How do I handle a token that was revoked before it expired, if I validate JWTs locally?

Keep access tokens short-lived so revocation takes effect within minutes, and revoke the refresh token at the provider. For the rare cases that need immediate effect, such as a stolen account, the provider can publish revocation events that services consume to keep a small deny list of token ids (jti) or subjects until their tokens expire. That keeps the per-request check local.

Is it safe to cache by the raw token string?

It works, but the cache then holds live credentials in memory, and a heap dump would expose them. Using a hash of the token, such as SHA-256, as the cache key avoids that at negligible cost. We should also never log tokens, not even in debug logs.

What clock skew should local validation allow?

Servers' clocks differ by a little, so validators usually allow a small skew when checking exp and nbf, typically 30 to 60 seconds. Larger values extend the life of expired tokens. Keeping hosts synchronised with NTP makes a small skew enough.

7Conclusion

In this answer, we removed the provider from the per-request path. JWT access tokens are validated locally with cached JWKS keys that refresh when an unknown kid appears, and opaque tokens go through Dropwizard's CachingAuthenticator with expireAfterWrite and a size limit. The cache expiry comes from the revocation delay the business accepts, hit rate and latency are measured, and when validation is impossible the service fails closed, with 401 for bad tokens and 503 when it cannot check.

What to say in the interviewAbout 70 seconds aloud

Open with

First I'd find out what kind of token we get. If it's a JWT, there's no reason to call the provider per request: I'd validate it locally, checking the signature with the provider's public keys from the JWKS endpoint, plus issuer, audience and expiry.

Then, if they want more

The key set is cached and refetched only when a token names a key id we haven't seen, which covers key rotation, and the refetch is rate-limited. That takes authentication from a network call to well under a millisecond.

If the tokens are opaque and need introspection, I'd wrap the authenticator in Dropwizard's CachingAuthenticator with a Caffeine spec like maximum size 50,000 and expireAfterWrite of 60 seconds. I'd pick the expiry from the revocation delay security accepts, keep it below that, and use expireAfterWrite so a busy client can't keep a revoked token alive.

I'd watch the cache hit rate and authentication latency. Finally, I'd fail closed: a bad token gets 401, but if we can't validate at all because the provider is down, we refuse with a 503 and Retry-After rather than letting requests through.

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 official documentation for the version you use. 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 "A Dropwizard authenticator validates tokens by calling the identity provider on every...": https://scrivio-interview-library.netlify.app/java/dropwizard/c976bdcefe/