<h4>Is a shared cache necessary?</h4>
<p>In general a shared cache is not necessary because everything that is not in cache gets re-computed anyways. However, I would highly recommend a shared cache to avoid numerous issues.</p>
<p>An example: Imagine you have a template cache tag, the related page has been cached on all servers. Now you edit the related entry and Craft tries to clear the template cache that corresponds to that entry. The cache clear job is only run on the server that you are using the control panel at this point in time. The other servers will still have the template cache of the previous entry revision. You would have to build something to run the clear cache job on all other servers as well.</p>
<p>This is only one possible challenge that you could face. I am sure there are more.</p>
<p>To avoid a single point of failure for the cache component you could use a Redis cluster.</p>
<h4>What to recommend?</h4>
<p>Using Redis as cache storage type is perfectly fine. You would want to run it on a separate instance that is accessible by the multiple Craft servers. (It doesn't and shouldn't be exposed to the internet, neither directly nor via the load balancer.)</p>
<p>There are other possible cache storage type described in the <a href="https://craftcms.com/docs/3.x/config/#cache-component" rel="nofollow noreferrer">Cache component docs</a>.</p>

CC BY-SA 4.0
2021-09-24T15:18:29.317Z
thanks for your effort, but what I wanted to know something different:
- is it not only a good idea to have a shared cache, but also a necessity? will bad things happen if I cache on both webservers separately?
- I do not want one central Redis instance, I want to have something in sync (Master-Master). I want the database and the load balancer to be the single points of failure, a central REDIS would become another one...
Matthias Redl-Mann
CC BY-SA 4.0
2021-09-24T13:40:17.500Z
2021-09-24T15:43:03.987Z
2021-09-24T15:43:03.987Z
Johannes
<p>We are currently setting up 2 Craft webservers behind a load balancer with sticky sessions.</p>
<p>What I am wondering:
Is there any need, that</p>
<ul>
<li>the file based cache needs to be somehow switched to a shared cache?</li>
<li>runtime files (@runtime) need to be shared between the 2 systems?</li>
</ul>
<p>Especially the first question is bothering me because of how Craft can invalidate its cache.</p>
<p>If a shared cache is needed, what would you recommend? According to my research on the net I believe that Redis is not suitable with 2 locally installed instances (Master-Master). So either KeyDB as Redis alternative, DB Caching or file based caching via NFS would be options.</p>
<p>Thanks</p>

CC BY-SA 4.0
2021-09-24T13:40:23.313Z
I suppose you are already running a dedicated database server that the 2 Craft servers can talk to?
Johannes
CC BY-SA 4.0
2021-09-24T15:17:30.317Z
yes, we have a dedicated DB server
Matthias Redl-Mann
<cache><filecache><load-balancer>
file based cache and runtime files in load-balanced environments
# file based cache and runtime files in load-balanced environments

(Asked by: username_0 on 2021-09-23)

We are currently setting up 2 Craft webservers behind a load balancer with sticky sessions.

What I am wondering:
Is there any need, that

* the file based cache needs to be somehow switched to a shared cache?
* runtime files (@runtime) need to be shared between the 2 systems?

Especially the first question is bothering me because of how Craft can invalidate its cache.

If a shared cache is needed, what would you recommend? According to my research on the net I believe that Redis is not suitable with 2 locally installed instances (Master-Master). So either KeyDB as Redis alternative, DB Caching or file based caching via NFS would be options.

Thanks

**Tags:** cache, filecache, load-balancer
> **Comments:**
> **username_1**: I suppose you are already running a dedicated database server that the 2 Craft servers can talk to?  
> **username_0**: yes, we have a dedicated DB server  

---

## 1 answers

### Answer by username_1 on 2021-09-24. Score: 1

#### Is a shared cache necessary?

In general a shared cache is not necessary because everything that is not in cache gets re-computed anyways. However, I would highly recommend a shared cache to avoid numerous issues.

An example: Imagine you have a template cache tag, the related page has been cached on all servers. Now you edit the related entry and Craft tries to clear the template cache that corresponds to that entry. The cache clear job is only run on the server that you are using the control panel at this point in time. The other servers will still have the template cache of the previous entry revision. You would have to build something to run the clear cache job on all other servers as well.

This is only one possible challenge that you could face. I am sure there are more.

To avoid a single point of failure for the cache component you could use a Redis cluster.

#### What to recommend?

Using Redis as cache storage type is perfectly fine. You would want to run it on a separate instance that is accessible by the multiple Craft servers. (It doesn't and shouldn't be exposed to the internet, neither directly nor via the load balancer.)

There are other possible cache storage type described in the [Cache component docs](https://craftcms.com/docs/3.x/config/#cache-component).

> **Comments:**
> **username_0**: thanks for your effort, but what I wanted to know something different: - is it not only a good idea to have a shared cache, but also a necessity? will bad things happen if I cache on both webservers separately? - I do not want one central Redis instance, I want to have something in sync (Master-Master). I want the database and the load balancer to be the single points of failure, a central REDIS would become another one...  

---

