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
feat: contentTypeParser cache, prefer lru to fifo #5340
Conversation
Because you changed it: #5331 |
@gurgunday lru is only useful when you expect eviction due to exceeding the cache size, otherwise you are just paying overhead on every get for no reason. |
No, I indeed changed it from For this part of the code, an Lru could be better since a match would bump that contentType in cache so that it stays longer before getting evicted |
@gurgunday lru doesn't affect ttl, it only switches eviction order. do we expect evictions? |
Yes, when I said it would stay longer, I meant the evictions
The cache can fill up fast for RegExp matches, but for string matches it actually doesn't do much, so maybe the cache should only be for RegExp, will take a look if I can come up with something better |
@gurgunday we can have two separate caches for these cases, lru for regex and fifo for strings accordingly, that would have optimal runtime characteristics |
Actually, after a second look, there could be evictions because of string matches too: fastify/lib/contentTypeParser.js Lines 106 to 113 in f776447
Consider the following: A client sends a POST request with the Content-Type header Combined with the RegExp matches, I feel like bumping fits better, no? Is bumping that expensive? |
Not super expensive, but state mutation is state mutation. |
No idea 😁 This is a client-dependent area, in theory I could spam a server with different variations of I'll think about what the best way to handle this would be |
We don't need to consider malicious agent scenario here, eviction event is not expensive enough to qualify as a ddos attack surface. |
Should I bench? In any case, this is when the server receives stuff, which is much rarer than sending stuff anyway — so neither LRU or FIFO will cause any meaningful bottleneck Looking at the nature of this cache, I think an LRU makes more sense — why not push back the eviction order of a Content-Type that was matched recently? An alternative is to increase the FIFO size, as @kibertoad said, but my problem isn't necessarily with the size What does @fastify/core think? |
I read a few things, looked at some practical benchmarks, and concluded that a FIFO is simply better unless there is huge interest in bumping recent hits, like in rate-limit |
@gurgunday Do you think we may still benefit from increasing the cache size? |
100 sounds reasonable to me... but maybe it is too little – some real-world usage data would be pretty helpful here |
Why Fifo?
If there is a cache match, we should make that entry fresh again as chances of it being used again is now higher