I forked netlify functions repo and added my own code (this is my additional code). I also deployed the code onto Netlify. The test is located at the bottom of the Netlify page. In short, I added 2 Netlify Functions
Original: Uses ytdl-core and collect youtube video info, based on the VideoID obtained in the query parameter (e.g. .netlify/functions/youtube?videoID=UVV80kTudSM)
Modded: Exactly the same API as the one above, with addition JSON post-processing
Test Case 1:
Call modded API, with the parameter UVV80kTudSM, and run it multiple times (browser refresh). The API fails half of the time.
Here is the attach the Netlify Function log, after calling the API multiple times:
8:30:33 AM: Duration: 7.92 ms Memory Usage: 96 MB
8:30:36 AM: Duration: 721.67 ms Memory Usage: 96 MB
8:30:39 AM: Duration: 10.96 ms Memory Usage: 96 MB
8:31:33 AM: Duration: 641.21 ms Memory Usage: 96 MB
8:31:36 AM: Duration: 4.93 ms Memory Usage: 96 MB
8:31:46 AM: Duration: 697.44 ms Memory Usage: 96 MB
The long-short-long-short API duration actually synced with the pass/fail situation. It seems to me that the reason of the short duration is because the Netlify function is not being executed fully.
When I run the above sequence, the Youtube (Full) API works fine. The API doesn’t work fine only when the same API (with the same parameter) is being called again. Therefore I am not sure if the problem comes from my code or the behavior (caching?) of the Netlify function.
I added the cache-control and here is my commit and here is the API code. It is re-deployed on Netlify.
But when I rerun Test case 1 above, the duration behavior didn’t change - it is still one long and one short, I would assume that the short duration is caused by Netlify Functions replying using cache.
The problem here is the caching behavior breaks line 12 of my code. In contrast, the original API (code, with the same parameter, would not fail on re-run.
I can confirm that we are not returning a cached response for your function. What you are seeing is a caching issue but rather a cold-start one. The longer requests are likely when a function needs to cold-start and the short requests one to an already spun-up function.
Hmm I tried the API code locally and I don’t see any problem, that is strange.
Still, the API calling duration makes me feel extremely curious. Also if you run the Test Case #2 (if caching is not being observed, then the long short duration behavior should not exist when the API parameter is changed) above, actually there is no issue, also according to the log the duration value keeps flat.
As I mentioned, I’ve reviewed our logs with a colleague and can definitely confirm that we are not responding with a cached response. I believe the issue is directly related to the difference in your API calls. I’m not familiar with that package you are using so I won’t be much help with that.
Testing locally won’t always replicate how things work live, since there are a lot more factors when the function is deployed. Even the Node version could be different. You could try setting the node runtime version of your deployed Netlify function to match what you use locally by following: Build functions | Netlify Docs.