Cloud function seems to cache data from external database

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 things I’ve checked:

  return {
    statusCode: 200,
    ttl: 60,
    headers: {
      "Access-Control-Allow-Origin": referer,
    },
    body: JSON.stringify({
      data: {
       ...
      },
      timestamp: new Date(),
    }),
  };
  • 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.

@hrishikesh I did some more testing today, but it just not seem to work.

My website runs on https://development--jocular-cocada-4001c5.netlify.app/

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.

You’re calling /.netlify/functions/status which is never cached anyways and I can see the updated response.

image

@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)

It’s better if you share HAR files or request IDs of these issues. The response timestamps would hardly provide us enough info to debug.

Hereby some requests with the parameters you’ve asked. The last response timestamp is older than the previous 3.

As a new user I am not allowed to upload HAR attachments.

Request made at Sun, 02 Jul 2023 15:35:35 GMT
Response timestamp: 2023-07-02T13:52:23.653Z
x-nf-request-id 01H4BKQTBS6P4JNK3790H8ZXMN

Request made at Sun, 02 Jul 2023 15:32:29 GMT
Response timestamp: 2023-07-02T13:52:23.653Z
x-nf-request-id 01H4BKXG9YGJ4S7CA4DRH97048

Sun, 02 Jul 2023 15:37:25 GMT
Reponse timestamp: 2023-07-02T13:52:23.653Z
x-nf-request-id 01H4BM0V8HMXTYCXKW1GB9M3DC

Sun, 02 Jul 2023 15:38:54 GMT
Reponse timestamp: 2023-07-02T12:59:03.997Z
x-nf-request-id 01H4BM3HZFGB4KWVF8FJTQZCKA

Can you please try to PM me or post a link to your HAR file

@SamO I would love to, but on your profile there is no option to send a PM. Could it be that it is not enabled on my account do sent PMs?

Hi, @osi. PMs were not enabled for your forum account but they are now. The PM feature will work for you now.

@SamO PM sent, thanks @luke

Thanks for sharing the HAR files. I have tried to purge some cache for that URL from our end. Can you check if there has been any difference?

Unfortunately still the same issue, tried some refreshes:

At 21:21 got the response of 13:33 after some waiting and another refresh got the response from 12:59

We had some instances of function caching and this was resovled by the devs in the past week. Can you confirm if this is still happening for you?

It is still not working:

2023-08-13 13:47 call returns: timestamp: “2023-08-12T21:19:18.571Z”
2023-08-13 09:25 call returns: timestamp: “2023-08-12T21:04:34.103Z”

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.

Hi, @osi. We tracked down the reason for the caching.

The function you created for this API call is an On-demand Builder function:

So, yes, the caching is occurring but only because you specifically created the function to use caching.

If you modify that function to be a normal function (and not an On-demand Builder function), the caching will stop.

​Please let us know if there are other questions about this.