Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scalable way to share cached files across frontend servers

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 ?

like image 739
Julien Pellet Avatar asked Mar 20 '23 03:03

Julien Pellet


2 Answers

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.

like image 142
Philip Hallstrom Avatar answered Apr 01 '23 10:04

Philip Hallstrom


If you don't want to hit the rails stack, you catch the request with something like rack-cache before it ever reaches the whole app:

http://rtomayko.github.io/rack-cache/

At least that way, you only have to bootstrap rack.

It also supports memcached as a storage mechanism: http://rtomayko.github.io/rack-cache/storage

like image 32
Marc Seeger Avatar answered Apr 01 '23 09:04

Marc Seeger