What I found
The send loop was serial. For each contact it resolved the template, sent the message, wrote a few database rows, and then slept. After every 100 messages it slept again, longer.
for each batch of 100 contacts:
for each contact:
resolve template, send, record result
sleep(5 s) # after every message
sleep(120 s) # between batches
The sleep alone accounts for 8.57 hours on a 5,000-contact run: 5,000 multiplied by 5 seconds, plus 49 between-batch gaps of 120 seconds. We were sending at roughly 0.13 messages a second, a fraction of a percent of what the account could carry.
That 8.57 hours is configuration multiplied by a count, not a profiled measurement. I only quote measured rates below, and I say which is which when asked.
First change: delete the waiting
The delays were read from environment variables, so this was a configuration change rather than a deploy: instantly revertable if the provider started rejecting traffic. Measured across 18 production runs afterwards, send rate went from about 22 to about 59 messages a minute.
Better, and nowhere near enough. Removing the sleep exposed the real ceiling.
Why that was the limit
The loop still sent one message at a time and waited for each provider round trip, roughly 0.7 seconds, before starting the next. With the sleep gone, the run time was simply the number of contacts multiplied by one network call. No amount of tuning moves that, because nothing is running in parallel.
It had a second problem that mattered more to customers: the loop ran inside a scheduled process, so a deploy mid-campaign destroyed whatever had not been sent.
Second change: a job queue
One job per recipient on a Redis-backed queue, workers sending in parallel, and a shared rate limiter holding the whole account under its ceiling so a large campaign cannot starve login codes. Claims and state transitions are atomic Lua scripts, a reconciler re-adds jobs the queue drops, and pause and resume are idempotent.
Measured on production at 15 sends a second: 785 to 809 messages a minute, against the loop's median of 56. The largest real run sent 18,615 contacts in 23.6 minutes, which the loop would have taken about five and a half hours to do.
| Measured on production | Rate | 5,000 contacts |
|---|---|---|
| Serial loop, original delays | ~22 / min | ~3.8 h |
| Serial loop, delays removed | ~59 / min | ~85 min |
| Job queue at 15 a second | ~790 / min | ~6 min |
The number I care about more than the speed: across 20,637 sends in that window there were zero duplicates and zero double charges. Making a send queue fast is easy. Making it bill each recipient once, while customers are paying per message, is the actual work. Not because delivery is exactly-once, which is not available over a network, but because every claim is atomic and every charge is idempotent, so a job that runs twice still costs the customer once.
What I took from it
- Measure the waiting before optimising the work. The biggest cost here was not inside any function.
- A configuration win can hide a structural ceiling. Deleting the sleep tripled throughput and still left the system 14x short.
- Say which numbers are measured and which are arithmetic. I replaced my own earlier calculated figure with measured ones for exactly that reason.