- Provider limit: 100 texts/sec
- Normal traffic: 20 texts/sec
- Burst traffic after an outage: 1,000 requests/sec

If you reject requests without a well-behaved client backoff, users tend to retry repeatedly (tap “Send code” many times), which can multiply the load and create a retry storm — a self-inflicted denial-of-service.
Options
Option A — Reject the extra requests
- Immediately return an error to clients that you couldn’t send the code.
- Typical client behavior: users tap “Send code” again and again when they see an error.
- Retry storms: a few retries per user multiply the load. Example: 1,000 initial requests × 4 retries = 5,000 requests/sec — effectively a self-inflicted DoS.

Option B — Accept and enqueue every request
- Accept each request and append it to a queue; return success to the user (“Code sent”).
- Dequeue at the provider limit (100/sec). This prevents client retries but allows the queue to grow large.
- Problem: codes can arrive very late. Time-limited login codes become useless if delayed beyond their TTL, harming UX and security.

Option C — Collapse duplicates + intelligent throttling (recommended)
- Treat requests for the same phone number as duplicates within a short window (cooldown).
- If a code was recently sent (or already scheduled), return a success response without enqueuing another message.
- This collapses N attempts by the same user into 1 provider call and prevents retry storms.
- Combine with:
- Per-phone cooldown (e.g., one SMS per phone per 60–120 seconds).
- Global rate limiting (e.g., token bucket) to protect provider capacity.
- Queue trimming: drop older/outdated messages for the same phone, or enforce a maximum queue depth and TTL for queued items.
- Client-side UX: disable the “send code” button briefly after it is pressed and show remaining cooldown time.
- Proper status codes and headers: when rejecting, return
429 Too Many Requestswith aRetry-Afterheader to help well-behaved clients back off.
Strategy comparison
Implementation pattern — Redis coalescing lock per phone
Key idea: set a short lock/flag for a phone number in Redis when a code is sent or scheduled. If another request arrives while the key exists, treat it as a duplicate and return a success-like response without enqueuing another SMS. High-level steps:- On request, compute
key = sms:last_sent:{phone}. - Try to create the key atomically with expiry (SET NX EX).
- If set succeeds:
- Generate a code.
- Enqueue send job (e.g., push to Redis list or broker).
- Return success.
- If key exists:
- Return success or informative response without enqueuing.
- Include
Retry-Afterheader (ttl of the key) so clients can show cooldown.
- Maintain a global token bucket or semaphore representing available sends to provider.
- When tokens are exhausted:
- For coalesced requests (duplicates for phones with recent keys), you can return
200without enqueueing (because there is already a scheduled send). - For new/different phones, return
429 Too Many RequestswithRetry-Afteruntil capacity returns.
- For coalesced requests (duplicates for phones with recent keys), you can return
Collapsing duplicate requests per phone plus a short cooldown gives the best balance of UX and operational safety: it prevents retry storms, keeps queue sizes manageable, and delivers codes promptly for most users.
Summary checklist
- Enforce per-phone cooldowns to collapse retries.
- Use a global rate limiter (token bucket) to protect provider throughput.
- Avoid enqueuing everything blindly; enforce TTLs and queue limits.
- Return appropriate HTTP status codes:
200for collapsed successes (where a code was recently sent),429 Too Many Requestsfor rate-limited requests where no collapse is possible.- Include
Retry-Afterheaders so clients can back off.
- Improve client UX: disable resend button, display cooldown timer, and handle
Retry-After.
Links and references
- Twilio
- Redis
- Token bucket algorithm: Token Bucket — Wikipedia
- HTTP 429: MDN — 429 Too Many Requests
Retry-Afterheader: MDN — Retry-After- Flask: Flask documentation