4 ms·
This approach might be totally obvious to many people here, but its quick to implement, it performs well, and it scales impressively: 1) The web server checks
by patio11 17y ago
This approach might be totally obvious to many people here, but its quick to implement, it performs well, and it scales impressively:
1) The web server checks to see if the image exists in a file store and, if it does, serves it directly.
2) If not, the request goes to your web application stack (Rails, etc) which fires off an ImageMagick process to do whatever magick the image requests, operating on either a base image or something which you magick up at request time.
3) Your application twiddles its thumbs for a second or two. (This has potentially negative consequences for other requests coming into the same mongrel, at least in Rails. Consider doing load balancing which is aware of the mongrel being tied up -- I think Pound does this fairly easily.)
4) After you've got the image, save it to the file store and tell the web server "Send the user the file at this path".
5) The next request for the same URL will hit the webserver, find the file on disk, and stream it automatically.
In my case I use a cron script every day to blow away all the images that haven't been accessed in the last X days, but depending on your needs it might be easier to just persist them indefinitely. (I use GIFs to give users a live preview of the PDF they're essentially editing, and generate one every second, so if I didn't do this I would drown in half-written bingo cards. My users go through a couple of gigs a week 20kb at a time.)
- JacobAldridge 17y agoPatrick, your observations, awareness, and ability / willingness to communicate them clearly continue to impress me. Cheers.
- patio11 17y agoThanks! Writing in English helps me take refreshing little breaks from, e.g., trying to figure out what the single comment left in a 3,000 line source file written in Japanese by our Korean outsourcing team means. (Answer: it means today will be a long, long day.)
- emmett 17y agoRe point 3: Don't use Pound. Use HAProxy, it's superior along pretty much every dimension. The idea is right though :-).