mongodb + mailqueue + rails? Yes!

Rails Israel - 9th October 2013

Krzysztof Knapik

Tech Lead @ Lunar Logic
Ruby & JS consultant @ infakt

@knapo
github.com/knapo

sending email in rails apps - asynchronus or not?

it depends...

in our case: YES

but....how?

redis + resque_mailer?

NO.

  • no VM & swapping, good for small and not very important data.
  • requires extra implementation:

delayed_job?

NO.

  • queue in SQL - possible.............
  • requires extra implementation:

none of existing solutions offers emergency/backup/secondary delivery method in case failure

custom RabbitMQ solution?

  • powerful, but over complicated

https://blog.serverdensity.com/replacing-rabbitmq-with-mongodb

why mongo?

  • swapping
  • journaling
  • atomic findAndModify operation
  • performing and scalable

mongo_mailer


github.com/infakt/mongo_mailer

mongo_mailer components


  • rails plugin providing ActionMailer delivery method, generators & capistrano recipies
  • (standalone) worker with base and emergency delivery method

rails plugin



# Gemfile
gem 'mongo_mailer', require: 'mongo_mailer/rails'

# environment file
config.action_mailer.delivery_method :mongo_queue

$ bundle exec rails generate mongo_mailer		
#=> config/mongo_mailer.yml.example

# config/deploy.rb
require 'mongo_mailer/capistrano'


$ cap mongo_mailer:start|stop|restart

(standalone) worker



# rails app
$ bundle exec mongo_mailer start|stop|restart

# independent app
$ bundle exec worker start|stop|restart

base and emergency delivery methods

config/mongo_mailer.yml

base_delivery_method: smtp
base_delivery_settings:
  user_name: "username"
  password: "apikey"
  domain: "example.com"
  address: "smtp1.example.com"
  port: 587
  authentication: plain

emergency_delivery_method: smtp
emergency_delivery_settings:
user_name: "username"
  password: "apikey"
  domain: "example.com"
  address: "smtp2.example.com"
  port: 587
  authentication: plain

thank you!


slides:
http://knapo.github.io/rails-israel-2013-mongo-mailqueue-rails/