-
-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
performance issue: createVerifier called for every request and cache not used #358
Comments
thank you for your report! can you send a fix PR with test? |
@kibertoad Absolutely, just wanted to confirm I wasn't doing something incorrectly before I fired off a PR. I wanted your opinion on the best way to cache the verifier instances. We could do something like a simple
|
@SeanReece Bringing two different cache libraries doesn't feel nice, so staying with the same solution as fast-jwt seems more reasonable. |
@kibertoad I think you're probably right about public keys not changing dynamically (at least not frequently). Maybe I'm just overthinking the solution here :) I think a simple Map will work (maybe with a max size to avoid any possible memory issues) |
Do you expect anyone to have thousands of keys? |
Not at any one time, but imagine some organization has a policy to rotate their signing keys every hour, that would mean we would have 1000 keys/verifiers every 41days, which is technically a slow memory leak. This is likely a very rare edge case but thought I'd handle it anyways. Would be pretty simple I think. Something like this: const maxCacheSize = 500
const cache = new Map()
...
let verifier = cache.get(publicKey)
if (!verifier) {
verifier = createVerifier(verifierOptions)
cache.set(publicKey, verifier)
if (cache.size > maxCacheSize) {
cache.delete(cache.keys().next().value) // Remove the oldest cached verifier
}
}
verifyResult = verifier(token) |
sure, that does make sense |
Prerequisites
Fastify version
5.1.0
Plugin version
9.0.2
Node.js version
22.12.0
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
15.1.1
Description
createVerifier
seems to be called to create a new fast-jwt verifier instance for every incoming request. Not only is this expensive in itself, but thefast-jwt
cache is not used even if enabled using the options.Testing on my local machine. Macbook M1 Pro. 10,000 requests.
verify
/createVerifier
takes ~2.6s which is %5-20% of total response time depending on the endpoint.When I monkeyPatch
@fastify/jwt
to reuse the verifier perkey
(we only use a single publicKey from a JWKS endpoint),verify
/createVerifier
only take 400ms.Here's an example of our @fastify/jwt options
Link to code that reproduces the bug
No response
Expected Behavior
I expect
createVerifier
to only be called once per publicKey.The text was updated successfully, but these errors were encountered: