# For how long is it safe to cache a Prismic API ref

**URL:** <https://community.prismic.io/t/for-how-long-is-it-safe-to-cache-a-prismic-api-ref/5962>\
**Category:** Developing with Prismic\
**Tags:** rest-api\
**Created:** [June 25, 2021, 10:39pm UTC](https://community.prismic.io/t/for-how-long-is-it-safe-to-cache-a-prismic-api-ref/5962 "2021-06-25T22:39:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul2](https://avatars.discourse-cdn.com/v4/letter/p/df705f/32.png) [@paul2](https://community.prismic.io/u/paul2)\
**Post date:** [June 25, 2021, 10:39pm UTC](https://community.prismic.io/t/for-how-long-is-it-safe-to-cache-a-prismic-api-ref/5962/1 "2021-06-25T22:39:45Z")

</div>

For our server-side rendered app, we want to avoid the overhead of fetching the Prismic API ref for every single requests. The ~200-300ms it takes to load the master ref are critical.

For this, we use a stale-while-revalidate mechanism to ensure most users never have to pay the cost of loading the API ref:

- The API ref is cached and considered fresh for 30 seconds. After this period of time, it becomes stale but can be used while we refresh it in background.
- A stale API ref can be used for maximum 5 minutes. After this period of time, we must refresh the ref before requesting any Prismic document.

This logic is based on the assumption that if a document is published, the master ref will change but the previous ref will eventually stay valid for a short period of time (5 minutes based on our tests).

However, in some rare cases, we saw that a ref could expire almost immediately after a document is published, causing some errors because we are using a cached ref which has expired.

Could you share some more info regarding this?

- For how long is it safe to cache an API ref after a document is published?
- Does publishing many documents in row or a big release impacts the duration of a ref?

---

<div class="post-metadata">

**Author:** ![Phil](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/phil/32/14_2.png) [@Phil](https://community.prismic.io/u/Phil)\
**Post date:** [June 28, 2021, 10:29am UTC](https://community.prismic.io/t/for-how-long-is-it-safe-to-cache-a-prismic-api-ref/5962/2 "2021-06-28T10:29:31Z")

</div>

Hi Paul,

Welcome to the community!

We don't recommend caching at all:

> [@Prismic data caching](https://community.prismic.io/t/prismic-data-caching/437/6):
>
> Hey Dmitry, Yes, we don’t recommend using a caching system as this can cause issues when the master ref is changed on every document update, this can cause websites to break. Make sure you are using your CDN endpoint to offer your website extra protection and speed, for example [https://your-repo-name.cdn.prismic.io/api/v2](https://your-repo-name.cdn.prismic.io/api/v2). Also like Paulina said the platform is very stable. We have exceeded the Uptime of 99.9% for the past 26 consecutive months and are committed to maintaining this standard.

Thanks.

---

<div class="post-metadata">

**Author:** ![paul2](https://avatars.discourse-cdn.com/v4/letter/p/df705f/32.png) [@paul2](https://community.prismic.io/u/paul2)\
**Post date:** [June 28, 2021, 3:06pm UTC](https://community.prismic.io/t/for-how-long-is-it-safe-to-cache-a-prismic-api-ref/5962/4 "2021-06-28T15:06:01Z")

</div>

Thanks for you answer. @Phil. I do understand that not caching at all is the simplest solution.

We just realised that in many cases, fetching the Prismic ref takes more time (up to 300ms) than requesting an entire Prismic document, which takes around 50ms when it is cached by your CDN 🚀. That's why we tried to get around this problem.

Here is another solution we have in mind:  
If the Prismic ref changes only when a document is published, we could cache the ref and listen to Prismic webhook to know when to refresh it.

Do you see any potential issues with this solution?

---

<div class="post-metadata">

**Author:** ![Phil](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/phil/32/14_2.png) [@Phil](https://community.prismic.io/u/Phil)\
**Post date:** [June 29, 2021, 4:28pm UTC](https://community.prismic.io/t/for-how-long-is-it-safe-to-cache-a-prismic-api-ref/5962/5 "2021-06-29T16:28:36Z")

</div>

Hi again Paul,

So I think the team might be working on providing a faster Prismic Ref response this would hopefully be the solution you're looking for. I'm not 100% what the status of this is, but if you provide me with your repo URL I can try and get the BETA activated on your repo.

Thanks.

---

<div class="post-metadata">

**Author:** ![365nice](https://avatars.discourse-cdn.com/v4/letter/3/db5fbb/32.png) [@365nice](https://community.prismic.io/u/365nice)\
**Post date:** [July 2, 2021, 3:18pm UTC](https://community.prismic.io/t/for-how-long-is-it-safe-to-cache-a-prismic-api-ref/5962/6 "2021-07-02T15:18:06Z")

</div>

Why can't you answer his question? It sounds reasonable to do that paul2 is saying, and cache the ref - whats the issue with that?

---

<div class="post-metadata">

**Author:** ![Phil](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/phil/32/14_2.png) [@Phil](https://community.prismic.io/u/Phil)\
**Post date:** [July 2, 2021, 4:02pm UTC](https://community.prismic.io/t/for-how-long-is-it-safe-to-cache-a-prismic-api-ref/5962/7 "2021-07-02T16:02:17Z")

</div>

I feel I did answer his question in the post I linked:

> [@Prismic data caching](https://community.prismic.io/t/prismic-data-caching/437/6):
>
> We don’t recommend using a caching system as this can cause issues when the master ref is changed on every document update, this can cause websites to break. Make sure you are using your CDN endpoint to offer your website extra protection and speed, for example [https://your-repo-name.cdn.prismic.io/api/v2](https://your-repo-name.cdn.prismic.io/api/v2). Also like Paulina said the platform is very stable. We have exceeded the Uptime of 99.9% for the past 26 consecutive months and are committed to maintaining this standard.

So it's never really safe to cache the endpoint as it changes with every document update.

Also as we talked about we recently replicated the API ref endpoint in London for our European customers to have a faster response time on that endpoint, we will do the same for our Asian customers soon, so hopefully, there shouldn't be much need to cache the endpoint soon if everyone is already satisfied with the response time.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/system/32/1746_2.png) [@system](https://community.prismic.io/u/system)\
**Post date:** [July 7, 2021, 11:02am UTC](https://community.prismic.io/t/for-how-long-is-it-safe-to-cache-a-prismic-api-ref/5962/8 "2021-07-07T11:02:37Z")

</div>

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/system/32/1746_2.png) [@system](https://community.prismic.io/u/system)\
**Post date:** [December 9, 2021, 4:15pm UTC](https://community.prismic.io/t/for-how-long-is-it-safe-to-cache-a-prismic-api-ref/5962/9 "2021-12-09T16:15:47Z")

</div>



---

<div class="post-metadata">

**Author:** ![olehkhalin](https://avatars.discourse-cdn.com/v4/letter/o/b5e925/32.png) [@olehkhalin](https://community.prismic.io/u/olehkhalin)\
**Post date:** [October 11, 2022, 7:17pm UTC](https://community.prismic.io/t/for-how-long-is-it-safe-to-cache-a-prismic-api-ref/5962/10 "2022-10-11T19:17:00Z")

</div>

Hello Phil,

First of all, thanks for such a cool product to Prismic team.  
Any updates on this improvement? If there is a chance to get the BETA activated on my repo, I would be very grateful. Because now the response time has quite big impact on my client's website load time, which is important to him.

Thanks.

---

<div class="post-metadata">

**Author:** ![Phil](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/phil/32/14_2.png) [@Phil](https://community.prismic.io/u/Phil)\
**Post date:** [October 12, 2022, 1:38pm UTC](https://community.prismic.io/t/for-how-long-is-it-safe-to-cache-a-prismic-api-ref/5962/11 "2022-10-12T13:38:04Z")

</div>

Hi @olehkhalin,

I'm really happy to hear you're enjoying the product. The API is now replicated at the closest location for our users by default, so no need to activate this.

Are you sure you are using the CDN endpoint? So [https://example.cdn.prismic.io/api/v2](https://example.cdn.prismic.io/api/v2) not [https://example.prismic.io/api/v2](https://example.prismic.io/api/v2)

Thanks.
