Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1

184 | Chapter 6: Performance


One common problem is that email delivery via SMT Pis quite slow, on the average.
In addition, it is an unknown; the time it takes to send one email is highly variable.
Even when delivering to an SMT Prelay on the local network (which is a good idea
for high-volume sites), SMTP delivery is slow.


To counteract this slowness, it is usually desirable to decouple the email sending
from the web request/response cycle. It makes sense to allow the user to continue
working, even if the server is still trying to send email in the background. One option
is to simplyforkoff a separate OS process, or use a separate interpreter thread (via
Thread.newwith a block) to send email asynchronously. However, this solution does
not scale well, as you must handle any concurrency issues that arise on your own. In
addition, you have overhead from starting a new thread or process on each piece of
mail. For high-volume mail situations, you want a mailer daemon running a tight
loop that can send mail without having to start a worker process.


The scalable option is the Robot Co-op’s ar_mailer.*This little library uses the data-
base as an outgoing mail spool. When mail is to be sent, rather than delivering it
externally, Rails just dumps it into the database. The separatear_sendmailprocess
picks it up and sends it along. This way, the application does not get backed up
because of slow SMT Pperformance.ar_sendmailcan be run periodically (from cron)
or continuously, as a daemon.


Further Reading


Zed Shaw’s most famous rant,Programmers Need To Learn Statistics Or I Will Kill
Them All(http://www.zedshaw.com/rants/programmer_stats.html), is an excellent (if a
little aggressive) description of the most common misconceptions surrounding per-
formance measurement.


Peepcode has a screencast on benchmarking with httperf athttp://peepcode.com/
products/benchmarking-with-httperf. It is $9 but is worth the cost for anyone involved
in performance tuning.


Evan Weaver has a set of MySQL configuration files that are tuned for common Rails
situations at http://blog.evanweaver.com/articles/2007/04/30/top-secret-tuned-mysql-
configurations-for-rails. These are drop-in replacements for the standardmy.cnfcon-
figuration file, and they are much more current than the examples provided with
MySQL.


*http://blog.segment7.net/articles/2006/08/15/ar_mailer

Free download pdf