Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paperclip + Rails with load balanced machines

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.

like image 605
alste Avatar asked Nov 27 '12 22:11

alste


2 Answers

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.

like image 143
Anthony Alberto Avatar answered Nov 04 '22 21:11

Anthony Alberto


If you don't want to use a file share like NFS, you could store the images in your database. Here is a gem that provides a :database storage type for Paperclip:

https://github.com/softace/paperclip_database

like image 38
Paul A Jungwirth Avatar answered Nov 04 '22 20:11

Paul A Jungwirth