Error: too many emails per second on Heroku Error: too many emails per second on Heroku heroku heroku

Error: too many emails per second on Heroku


This is not going to be related to Sidekiq (well, not directly, anyway). This relates to whatever SMTP server you are using and the server's rate limiting. If you have multiple instances of Sidekiq firing off emails near simultaneously, this error might get thrown if you have a relatively low rate limit.

For instance, if you are using Mailtrap as a SMTP server in a development or staging environment, a free account is limited to 2 emails per second. Pretty easy to run into this error.

If you can't avoid running into this issue for your purposes, you can try a few different strategies by using the keyword argument 'wait' for ActionMailer's deliver_later method, e.g.:

users_i_need_to_email.each_with_index do |user, index|  t = 5 * index  UserMailer.send_important_stuff(user)            .deliver_later(wait: t.seconds)end

In the event your :deliver_later calls are operating in different processes or flows, you can get away with randomizing the delivery time (say, some random time up to 5 minutes).

Obviously, neither solution above is full proof, so you are best off using a SMTP server that is adequate for your purposes.


Definitely seems to be an issue with sidekiq concurrency set to 3.

Additional info: Setting concurrency to 1 fixes the issue but I don't want all jobs to be processed by a single worker. It would be great if Sidekiq would let use set concurrency per queue so emails could use a single worker while other job could use multiple workers. However, sidekiq author said it's not possible.

I found a solution to my particular problem here using heroku which involves putting each queue in it's own heroku dyno which allows you to set concurrency in each dyno.