Cache purging via API fails randomly

Since the cache API form Workers isn’t enabled when doing wrangler dev I’m using the API to purge a URL when a user publishes some content.

https://api.cloudflare.com/client/v4/zones/:zoneId/purge_cache

The response I’m getting is totally ok and I get success: true.

For some reason this doesn’t work 100% of the times though. I thought it was a matter of waiting but when it works the url is purged immediately. When it fails the url is not purged even after 10 minutes of calling the API.

Is this normal behavior?

Will the cache API give me better results?

We actually had the topic only recently

You want to purge the cache Cloudflare-wide, not just in the datacentre where the Worker is running, correct? In that case the API is the right approach.

Purging can take a couple of seconds, but it shouldnt take ten minutes. Did you check all caching relevant headers? Are you absolutely sure the file didnt get cached and maybe re-added?

If you are sure, open a support ticket and let them know the result.id of that particular purge request. They might be able to tell you more.

1 Like

Yeah same headers and same logic every time.

I will open a support ticket, thanks for the suggestion.

My question was rather directed towards the headers of the resources. But if you have double checked everything a support ticket will be the best approach.

If it was a headers problem the behavior would be the same on every purge request, no?

These are the cache headers btw:

cache-control: max-age=60,s-maxage=31449600,immutable

I was not referring to cache-control, rather Cloudflare’s headers.

But anyhow, if you say you double checked simply go via support :slight_smile:

Yeah I contacted support a couple of hours ago.

About the headers, you mean on the response?

Yes. You took that screenshot right after a purge?

Yeah I purged 3 hours ago and still it hasn’t been purged…

Ehm, thats not exactly right afterwards. And that file has been in the cache only for about 2.5 hours, so it probably was removed from the cache.

But if it was purged how come it’s still showing the old content?

The page should be showing some new content based on some value from KV. I just checked and the content on the KV is indeed updated and it has a timestamp 1590434851270 which is:

GMT: Monday, May 25, 2020 7:27:31.270 PM
Your time zone: Monday, May 25, 2020 2:27:31.270 PM

So almost 3 hours ago.

The page is 10414 seconds old now so it seems just a couple of minutes before the new content was created and the purging request was mande.

So you are populating the cache from Cloudflare’s database? In that case you better start a JavaScript debugger :slight_smile:

At this point I’d probably refrain from opening a ticket, as the purge seems to work and there is some other issue with outdated data getting into the cache. From your description I’d mostly think of some JavaScript issue but that’s something you will need to debug :slight_smile:

I’m using the Cache API from a Worker and then using fetch to purge via the REST API.

The HTML is generated in the worker and put into the Cache API using some data from KV.

It’s not really a matter of debugging. I receive a correct response from the API and the result is the same when purging that URL from cloudflare’s dashboard. Sometimes it’s purged, sometimes it’s not.

{"result":{"id":"XXXX"},"success":true,"errors":[],"messages":[]}

Ehm, didnt we just discover that purging worked? The resource in question has been in the cache for less time than we you ran the last purge.

I think you are right. I’ve counted the seconds more precisely and the content change and the purging happened with a second or two of difference.

I know what is going on.

I’m going to test something and get back to you.

Edit:

Nah, it didn’t work. I have no idea what’s going on.

I’m writing to KV and purging at the same time inside a Promise.all() and thought that could be a problem but in fact the cache should be refreshed when visiting the url later on.

Purging and writing at the same time might not be the best idea. Only write to the cache if you cant find any data in the cache, otherwise only fetch or purge.

It is best you post your code here.

To clarify, I’m not writing to the Cache and purging at the same time.

I’m purging and writing some data to KV.

Edit:

And anyway my code is irrelevant. I’ve seen the same behavior when purging from the dashboard.

I’d trim down the code to the absolute minimum and take it from there. You might have some race condition.

Well, if you dont want to post the code it is difficult to say anything. Purging does seem to work, so I’d stick with the assumption of a race condition. You’ll have the debug that code, something is not writing the right data.

Let’s recapitulate:

The data is being written to KV when it should. I have triple checked it.

The API response from the purge returns OK.

The same problem seems to happen randomly from Cloudflare’s dashboard too.

I’m not writing to the cache API when I’m doing the purge. The cache API is written when the URL is refreshed, just like a Workers Site project does. It doesn’t matter if you refresh 1 minute after the purge or 1 hour.

Where do you think the race condition could be happening?