I am using a NodeJS cloud function to call a database server to fetch data. The data is then returned by the cloud function as JSON. The data in the database changes very often so a caching of 5 minutes is the maximum.
Unfortunately it looks like the database data is only fetched once when the cloud function is called for the first time after deploy. All successive calls to the cloud function returns the same value even if I change the values in the database.
The topics I’ve read about caching cloud functions:
The timestamp field shows an old time and not the current time
I have the strong feeling the response of the cloud function is cached and therefore the database data is not fetched at all. Did anyone ran into this issue or know how to disable caching so the database data is fetched every time the cloud function is called?
Since you’re using TTL, I’m assuming you’re using On Demand Builders. Their property is to remain cached, but yes after 60 seconds they should start revalidating. What that would mean is, after 60 seconds, whenever you make a request, you would continue to get the old data, and the new data would be fetched in the background. Once the new data would be ready, it would be served instead.
If this is not what’s happening, we would need the site info and/or URLs to check.
Thanks for the prompt reply! This part is interesting for me. Since it would mean that I need to make 2 requests to make sure the data will be refreshed. Especially on low traffic websites this could be an issue when a single request is made. What do you recommend? I assume making 2 requests is not the best practise.
If you inspect the network traffic you see a call to the “status” cloud function. You can not call this url directly since there are some security measures in place.
In the response, the track part has a logTime property of 2023-06-25T20:21:07.000Z which was yesterday while the database is already updated 4 times.
@hrishikesh Maybe you were lucky because you are the first to trigger it today.
Btw the response timestamp attribute is filled the current time:
timestamp: new Date()
I’ve checked multiple times over a period of an hour by refreshing the page on several moments. As you can see there is some weird behavior:
Sometimes the response is without any old response, which is great.
However there are situations where the response is too old. One of the calls was 58 minutes old.
Another one is even weirder, older responses served while the previous call had a newer response.
13:22 gives response from 13:02 (20 minute old)
13:25 gives response from 13:25 (10 mins old)
13:29 gives response from 13:12 (17 mins old)
which is weird since it is older than the 13:25 call.
13:33 gives response from 13:33 (0 mins old)
13:35 gives response from 13:35 (0 mins old)
13:42 gives response from 13:12 (30 mins old)
which is weird since it is older than the 13:35 call
13:48 gives response from 13:48 (0 mins old)
13:52 gives response from 13:52 (0 mins old)
14:02 gives response from 14:02 (0 mins old)
14:04 gives response from 14:02 (2 mins old)
14:05 gives response from 14:05 (0 mins old)
14:11 gives response from 14:11 (0 mins old)
14:14 gives response from 13:16 (58 mins old)
14:16 gives response from 14:16 (0 mins old)
14:18 gives response from 14:16 (2 mins old)
So far, I have never been able to reproduce this and there’s lack of evidence of any issue happening outside of your device. I just refreshed multiple times and reliably got fresh data. I also checked across the entire CDN and tried to clear cache for your function multiple times, but since I’m not seeing the issue, I’m not sure if it made any difference or not. Based on this, I don’t think we can continue debugging this any further.
If you can show some conclusive data showing cached responses that reliably happen, we can pick this up, but so far, all the data that you’ve shown seems to be happening locally. Try reproducing this across devices/networks.
Sorry, I don’t get what you mean. I’ve provided HAR files, responses and everything you asked for. This data was from Chrome visiting https://development--jocular-cocada-4001c5.netlify.app/ It is so weird that you can’t see the old timestamps.
The solution I could think of is to make a repo only containing a cloud function which returns the current timestamp and see if that works.
I really don’t know what more to say really. I have been trying to see the issue myself, but it simply never happens.
The only “staleness” I am seeing is, if I immediately refresh a couple of times, I don’t see the data accurate to the second, but if I wait for a minute or two and refresh, I get the latest data. But I’m never seeing it go an old as an hour or so like you describe.
I’ve asked someone else from the team to take a look, but as for me, I still cannot reproduce this.