Our email provider allowed 500 a second. We were sending 14.
Our email provider allowed 500 messages a second. Our system was sending about 14. This is the story of why, and what fixed it.
The system
I owned the notification service at an enterprise communication platform. It was multi-tenant: every customer had its own database schema, to keep their data isolated, and one service sent notifications across dozens of those tenants and a few hundred thousand users around the world.
There were 18 to 20 fixed campaign types across email, push (Android, iOS and web) and SMS, plus dynamic email campaigns built with a template builder. Each campaign could target everyone, a segment, or specific users. Some ran on a schedule: daily, weekly, biweekly or monthly digests, in each user’s time zone. Others fired on events, such as new content, a survey coming due, or a milestone like a work anniversary. Admins could also trigger them by hand. One trigger could mean hundreds of thousands of messages.
Where it hurt
Push was the easy channel. The messages were short plain text, and the provider’s limits were hard to hit.
Email was different. The provider capped us at 500 emails a second, we could not send duplicates, and each email was personalized. Before we sent to a user, we had to decide whether they should get it at all (had they already seen the content, or already submitted the survey?), and then build their HTML.
A large campaign was about 150,000 emails and took roughly three hours. Some of those campaigns had to land within minutes to be useful.
First attempt: more parallelism
The obvious move was more threads. It helped, but it did not get close. 150,000 emails in three hours is about 14 a second, far below the 500 we were allowed. The limit was not what was stopping us.
Measuring instead of guessing
The same steps, moved out of the per-recipient loop.
So I looked at where the time went for a single email. Sending was a small part. Most of it was work done before sending, repeated for every recipient:
- Working out whether this user should get the email.
- Rendering the HTML template for this user.
Adding workers multiplied that work. It did not reduce it.
Two changes
Cache the rendered template. We were re-rendering the same HTML for each recipient. We rendered it once and reused it.
Precompute ahead of time. Much of what a campaign needs could be known well before the send. I started preparing content and recipient lists ahead of the scheduled time, so at send time the system only checked current state (is it still unread, is the survey still open) and pushed.
The result
Bars are to scale against the provider’s 500 a second limit.
The same campaign went from about three hours to about 12 minutes, from roughly 14 emails a second to roughly 200. That is still below the provider’s cap, but no longer by a factor of 35.
What I would do differently
I made one mistake, and it was a design mistake, not a performance one. We assembled personalized content for every user inside the email, late in the pipeline and in a very dynamic way. That was the expensive part, and caching and precomputing only made an expensive design cheaper.
The simpler answer was to send a small hook, a short email or push that brings the user into the app, and let the app build the up-to-date content on demand. It is cheaper and easier to build.
What I took from it
- When throughput is low and a limit is nearby, check how much work happens before you reach it.
- More workers multiply the work you are doing. They do not remove it.
- Ask what has to be true before a send, and whether any of it can happen earlier.
- Then ask whether the work needs to happen at all.