Question: I have multiple backend servers continuously building and refresing the public parts of an api in order to cache it. The backend servers are builing depending on what has to be done in the job queue.

At a time,
backend server 1 will build :

```
/article/1.json
/article/5.json

```

backend server 2 will build :

```
/article/3.json
/article/9.json
/article/6.json

```

I need to serve these files from the front-end servers. The cache is stored as file in order to be directly served by nginx without going through the rails stack.

The issue is to manage to have the cache up to date on the front-end servers in a scalable way (adding new servers should be seamless).

I've considered :

* NFS / S3 (but too slow)
* Memcached (but can't serve directly from nginx - might be wrong ?)
* CouchDB direcly serving JSON (I feel this is too big for the job)
* Backend to write json in redis, job in fronted to re-write files at the good place (currently my favorite option)

Any experience or great idea on a better way to achieve this ?

Answer: 
You don't say how long it takes to build a single article, but assuming it's not horrifically slow, I think you'd be better off letting the app servers build the pages on the fly and having the front end servers doing the caching. In this scenerio you could put some combination of haproxy/varnish/squid/nginx in front of your app servers and let them do the balancing/caching for you.

You could do the same thing I suppose if you continued to build them continuously on the backend.

You're end goal is to have this:

```
internet -> load balancer -> caching server 1   --> numerous app servers
                         \-> caching server 2  -/

```

Add more caching servers and app servers as needed. The internet will never know. Depending on what software you pick the load balancer/caching server might be the same, or might not. Really depends on your load and particular needs.
You are right, S3 is pretty slow by itlsef, especially HTTPS session setup can take up to 5-10 seconds. But S3 is the ideal storage for primary data, we use it a lot but with combination of **S3 Nginx** proxy to speed data delivery up and inject caching facilities.

Nginx S3 proxy solution well tested on production and the caching mechanism works perfect, every application server goes to proxy that fetches original file from S3 to be cached.

To prevent dog-pile effect you can use:

* **proxy\_cache\_lock** for new files, [doc](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_lock)
* **proxy\_cache\_use\_stale updating** for updated files, [doc](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_use_stale)

An S3 Nginx proxy configuration look at this <https://gist.github.com/mikhailov/9639593>