Paperclip + Rails with load balanced machines
image, paperclip, ruby-on-rails, scalability
Solution
One solution is to use NFS to mount a shared folder that will be the root of your `public/system` or whatever you called your folder containing paperclip images.
There's a few things to consider to make everything work though :
- Use a dedicated server that will only contain assets, this way your hard drive(s) are dedicated to serve your paperclip images
- NFS can be expensive. Use it to write files from your App servers to your Asset server only. You'll have to configure your load balancer or reverse proxy or web server to retrieve all images from the asset server directly, without asking an application server to do it over NFS.
- a RAID system is recommended on your asset server of course
- a second asset server is recommended, with the same specs. You can make it act as a backup server and regularly rsync your paperclip images to it. If the master asset server ever goes down, you'll be able to switch to this one.
- When mounting the shared NFS folder, use the `soft` option, and mount via a high-speed local network connection, for example : `mount -o soft 10.0.0.1:/export/shared_image_folder` . If you're not specifying the `soft` option, and the asset server goes down, your Ruby instances will keep waiting for the server to go up. Everything will be stuck, and the website will look down. Learned this one the hard way ...
THese are general guidelines to use NFS. I'm using it on a quite big production website with hundreds of thousands of images and it works fine for me.
Problem
How do I get Paperclip image uploads to work on a Rails app running on 8 machines (load-balanced)? A user can upload an image on the app. The image is stored on one of the machines. The user later requests the image, but it's not found, because it's being requested from another machine. What's the workaround for this type of problem? I can't use AWS or any cloud service; images have to be stored in-house. Thanks.