Excessive Caching Causes Memory Leak, but can't get next to stop caching
Unanswered
Florida White posted this in #help-forum
Florida WhiteOP
So we're seeing an issue where Next appears to be caching A LOT of data seemingly indefinitely, and it appears to cache it regardless of any
We have an endpoint
I noticed that locally when developing, we were also seeing similar entries being cached to
I'm not sure if this is a bug or there is something that I am missing about how to prevent this endpoint from caching? Is anyone familiar with Next's usage of the IncrementalCache, FileSystemCache or LRUCache and how to get it to flush/clear out or just not cache certain endpoints?
Cache-Control settings.We have an endpoint
/internal/jobs/[jobId]/run which when investigating a memory leak and looking at the heap snapshots, we noticed an insane growth in strings which are just base64 representations of the data returned by this endpoint (and traversing the tree for these strings shows that they originate from this endpoint). They appear to be being cached by the IncrementalCache in the FileSystemCache under the LRUCache as Entry's, and they don't seem to ever clear out of here.I noticed that locally when developing, we were also seeing similar entries being cached to
/.next/cache/fetch-cache so it immediately made me think that this caching was causing the memory leak. I tried adding Cache-Control: no-store, no-cache, max-age=0 to the only place this endpoint gets called (it gets called very frequently, so this is a major memory leak), but it didn't seem to stop the cache files from being created locally in /.next/cache/fetch-cache, so it didn't seem to be respecting the cache-control. But then I realized that if I placed the route within an api folder (/internal/api/jobs/[jobId]/run), then locally it stops this cache file from being created when I hit this endpoint. I thought this was going to be the solution to this leak, but after deploying and monitoring the heap snapshots and memory usage, we're still seeing the same problem, so it seems like this cache is still storing all of the return data from this endpoint.I'm not sure if this is a bug or there is something that I am missing about how to prevent this endpoint from caching? Is anyone familiar with Next's usage of the IncrementalCache, FileSystemCache or LRUCache and how to get it to flush/clear out or just not cache certain endpoints?
12 Replies
This seems like a very very specific issue, with very good docs regarding the issue @Florida White
I would recommend you make an issue in the nextjs repo
Since your gonna get a response from the actual team regarding this
There aren't a lot of people here who have issues like these
Florida WhiteOP
Thanks, will try to open an issue on the Next repo. Wasn't sure if I was just missing some sort of cache setting or needed a specific strucutre to fix. Guess I gotta try and find a way to reproduce this with a minimal example 😩
@Florida White what version are you using ? (cuz it differ from v15 and prior to 15)
@`${ViNoS}` <@329839691195219969> what version are you using ? (cuz it differ from v15 and prior to 15)
Florida WhiteOP
we're on v14.2
@Florida White we're on v14.2
explains it
fetch requests are cached by default
fetch requests are cached by default
you have to specify cache to not store
fetch("url", {cache: "no-store"})|you can also set it for all routes