# Multiple nginx instances share same proxy cache storage

**URL:** <https://www.ruby-forum.com/t/multiple-nginx-instances-share-same-proxy-cache-storage/231361>\
**Category:** NGINX\
**Created:** [August 5, 2014, 1:43am UTC](https://www.ruby-forum.com/t/multiple-nginx-instances-share-same-proxy-cache-storage/231361 "2014-08-05T01:43:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [August 5, 2014, 1:43am UTC](https://www.ruby-forum.com/t/multiple-nginx-instances-share-same-proxy-cache-storage/231361/1 "2014-08-05T01:43:05Z")

</div>

I am thinking about setting up multiple nginx instances share single  
proxy  
cache storage using NAS, NFS or some kind of distributed file system.  
Cache  
key will be the same for all nginx instances.  
Will this theory work? What kind of problem will it cause(locking,  
cached  
corruption or missing metadata in the memory)?

Posted at Nginx Forum:

> **[Multiple nginx instances share same proxy cache storage](https://forum.nginx.org/read.php?2,252275,252275#msg-252275)**
>
> I am thinking about setting up multiple nginx instances share single proxy cache storage using NAS, NFS or some kind of distributed file system. Cache key will be the same for all nginx instances. Will this theory work? What kind of problem will it...

---

<div class="post-metadata">

**Author:** ![badtzhou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a6a055/32.png) [@badtzhou](https://www.ruby-forum.com/u/badtzhou)\
**Post date:** [August 5, 2014, 2:49am UTC](https://www.ruby-forum.com/t/multiple-nginx-instances-share-same-proxy-cache-storage/231361/2 "2014-08-05T02:49:33Z")

</div>

Hello!

On Mon, Aug 04, 2014 at 07:42:20PM -0400, badtzhou wrote:

> I am thinking about setting up multiple nginx instances share single proxy  
> cache storage using NAS, NFS or some kind of distributed file system. Cache  
> key will be the same for all nginx instances.  
> Will this theory work? What kind of problem will it cause(locking, cached  
> corruption or missing metadata in the memory)?

As soon as a cache is loaded, nginx relies on it’s memory data to  
manage cache (keep it under the specified size, remove inactive  
items and so on). As a result it won’t be happy if you’ll try to run  
multiple nginx instances working with the same cache directory.  
It can tolerate multiple instances working with the same cache for  
a short period of time (e.g., during binary upgrade). But running  
nginx this way intentionally is a bad idea.

Besides, using NFS (as well as other NASes) for nginx cache is a  
bad idea due to blocking file operations.

–  
Maxim D.

> **[nginx](http://nginx.org/)**

---

<div class="post-metadata">

**Author:** ![badtzhou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a6a055/32.png) [@badtzhou](https://www.ruby-forum.com/u/badtzhou)\
**Post date:** [August 11, 2014, 11:37am UTC](https://www.ruby-forum.com/t/multiple-nginx-instances-share-same-proxy-cache-storage/231361/3 "2014-08-11T11:37:54Z")

</div>

## Robert P. Wrote:

> like rsyncing the cache contents between nodes thus would not work);  
> are there any recommendations to achieve such a solution?

I would imagine a proxy location directive and location tag;

shared memory pool1 = nginx allocated and managed  
shared memory pool2 = socket or tcp pool on a caching server elsewhere

The problem you have is speed and concurrency of requests, rsyncing a  
cache  
requires a specific tag which needs to be respected by each instance  
using  
it or you will have a battle between instances.

A better idea would be a database with a persistent connection, in  
memory  
cached again to avoid duplicate queries.  
ea. use the database for a central repository of cached items and local  
memory to avoid hitting the database more then once for each item. No  
disk-IO would be involved so it should also be non-blocking.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,252275,252479#msg-252479](http://forum.nginx.org/read.php?2,252275,252479#msg-252479)

---

<div class="post-metadata">

**Author:** ![badtzhou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a6a055/32.png) [@badtzhou](https://www.ruby-forum.com/u/badtzhou)\
**Post date:** [August 11, 2014, 12:19pm UTC](https://www.ruby-forum.com/t/multiple-nginx-instances-share-same-proxy-cache-storage/231361/4 "2014-08-11T12:19:00Z")

</div>

Hello!

On Sun, Aug 10, 2014 at 05:24:04PM -0700, Robert P. wrote:

> Any options then to support an architecture with multiple nginx  
> nodes sharing or distributing a proxy cache between them? i.e.,  
> a HAProxy machine load balances to several nginx nodes (for  
> failover reasons), and each of these nodes handles http proxy +  
> proxy cache for a remote origin? If nginx handles cache info in  
> memory, it seems that multiple instances could not be used to  
> maintain the same cache info (something like rsyncing the cache  
> contents between nodes thus would not work); are there any  
> recommendations to achieve such a solution?

Distinct caches will be best from failover point of view.

To maximize cache effeciency, you may consider using URI-based  
hashing to distribute requests between cache nodes.

–  
Maxim D.

> **[nginx](http://nginx.org/)**

---

<div class="post-metadata">

**Author:** ![badtzhou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a6a055/32.png) [@badtzhou](https://www.ruby-forum.com/u/badtzhou)\
**Post date:** [December 25, 2014, 2:03pm UTC](https://www.ruby-forum.com/t/multiple-nginx-instances-share-same-proxy-cache-storage/231361/5 "2014-12-25T14:03:39Z")

</div>

Hi,

> > maintain the same cache info (something like rsyncing the cache  
> > [http://nginx.org/](http://nginx.org/)  
> > I wonder if it would hurt to make nginx load cache metadata from file as  
> > a  
> > fallback only if there’s no entry in the keys\_zone. If this would be a  
> > param  
> > for proxy\_cache\_path we could build a distributed cache cluster by  
> > simply  
> > copying cache files to other nodes. Making this a param would not hut  
> > performance if you don’t want this behavior. The functionality is  
> > already  
> > there, because nginx loads metadata from files on startup.

Is this a valid feature request or does no one care aboout clustering  
nginx  
caches?

–  
Thomas

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,252275,255788#msg-255788](http://forum.nginx.org/read.php?2,252275,255788#msg-255788)

---

<div class="post-metadata">

**Author:** ![badtzhou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a6a055/32.png) [@badtzhou](https://www.ruby-forum.com/u/badtzhou)\
**Post date:** [November 25, 2016, 5:00pm UTC](https://www.ruby-forum.com/t/multiple-nginx-instances-share-same-proxy-cache-storage/231361/6 "2016-11-25T17:00:24Z")

</div>

hey guys, along the lines of the nginx multi-server architecture  
described here, we built a control panel that can manage such large  
deployments automatically with many other nice tools.  
If you are available to give it a try at [https://clustercs.com](https://clustercs.com) we highly  
value your feedback (removed\_email\_address@domain.invalid) … this is an effort from  
developers for developers … thanks a lot!

---

<div class="post-metadata">

**Author:** ![badtzhou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a6a055/32.png) [@badtzhou](https://www.ruby-forum.com/u/badtzhou)\
**Post date:** [August 11, 2014, 2:24am UTC](https://www.ruby-forum.com/t/multiple-nginx-instances-share-same-proxy-cache-storage/231361/7 "2014-08-11T02:24:51Z")

</div>

Any options then to support an architecture with multiple nginx nodes  
sharing or distributing a proxy cache between them? i.e., a HAProxy  
machine load balances to several nginx nodes (for failover reasons), and  
each of these nodes handles http proxy + proxy cache for a remote  
origin? If nginx handles cache info in memory, it seems that multiple  
instances could not be used to maintain the same cache info (something  
like rsyncing the cache contents between nodes thus would not work); are  
there any recommendations to achieve such a solution?
