Part 3 of Building a Notification Service
OTP: the notification that can’t wait
Module 3 · 5 min read
OTP: the notification that can’t wait
TL;DR
- An OTP has to be fast, it expires within minutes, and it works like a password.
- Give OTPs their own topic, workers, reserved gateway capacity and a backup gateway. Drop jobs past
expires_at. - Never log the code. Store a hash, limit attempts, and rate-limit per number and device to stop SMS pumping.
Not all messages are equal
Nobody notices a “you received $5” push that’s ten seconds late. A slow OTP is different. The user watches a login countdown, presses “resend” after a minute, again, then calls support. And an expired code still costs money to send. An OTP is fast (seconds), has a short life (often two to five minutes) and is sensitive (whoever reads it can log in). No other message has all three.
The campaign in front of you
Say at 7 pm marketing sends a weekend offer to 200,000 users. At 500 SMS a second (illustrative, your contract sets the real limit), that takes nearly seven minutes to drain. An OTP in the same queue waits behind all of it, well past its expiry.
Give OTPs their own lane
- A separate topic or queue with its own consumers, e.g.
sms.otpnext tosms.bulkin Kafka. (RabbitMQ could use a priority queue instead, more on that in the Q&A.) - Reserved capacity. Two lanes sharing one gateway limit still collide, so cap the campaign lane at around 80% of the gateway’s throughput. Some companies use a separate gateway account, or even a separate provider, for OTPs.
- No batching delay. Bulk lanes batch to save cost and calls. The OTP lane sends each message as it arrives.
Don’t send a dead code
An outage, a deploy or a slow consumer can still back up the lane. Expired OTPs waste money and leave the user with three codes and no idea which one works. So each OTP job carries an expires_at, checked right before sending. If it has passed, drop the job and log “expired before send”. RabbitMQ can use a message TTL, in Kafka the consumer checks. A rising count is a good alarm that the lane is too slow.
When the gateway itself fails
- Failover. Have two gateways. When A keeps erroring or timing out, a circuit breaker (module 4) moves OTPs to B.
- Delivery receipts. The delivery receipt (DLR) that comes after “accepted” says whether the phone got it. If a gateway’s DLR rate drops, route OTPs elsewhere before it even starts returning errors.
- Another channel. Push or in-app if the app is installed. Some services offer a voice call as the last resort.
An OTP is a password
For a few minutes it’s as good as one, so treat it like one.
- Never log the code anywhere, including application logs, the notification log and the Kafka event. Log “OTP sent to +1******123” instead. Ideally the event carries only a template and a reference, and the code is filled in at the last step.
- Store a hash of the code and its expiry in the auth service, and compare hashes when the user types it.
- Short life, few attempts. A few minutes covers a slow SMS and a slow typist, and a stolen code soon dies. Burn the code after a handful of wrong guesses, since a six-digit code with unlimited guesses can be brute-forced.
- Rate-limit per number and per device. Otherwise attackers make you send thousands of OTPs to one victim, or to premium-rate numbers whose operators pay them a share of each message. That’s “SMS pumping”, and you pay for every SMS. Add CAPTCHAs on suspicious traffic and alerts on unusual destination countries.
The “resend” button is part of the system. People press it when the first code is slow, which doubles the load right when you’re behind. Show a countdown before allowing a resend, and make a resend invalidate the previous code.
Quick check
After a 10-minute gateway outage, the OTP topic holds 4,000 waiting jobs. OTPs are valid for 3 minutes. What should the worker do?
Most codes expired during the outage, and users have probably asked for new ones. Sending them costs money and confuses people. Check expires_at, drop (and count) the dead ones, and send the rest.
Q&A
Why not just put OTPs at the front of the same queue?
Kafka can’t reorder a partition. RabbitMQ priority queues can, and they work. I still find a separate lane easier to monitor and scale on its own, and it can use a different gateway account.
Should the OTP code travel through Kafka?
Not in plain text. Kafka keeps events for days and many consumers may read the topic. Encrypt the field, send a short-lived reference the SMS worker resolves, or keep the OTP lane’s retention very short with tight access control.