Skip to main content
When building SMS-based login (send a six-digit code to a phone number), your backend typically forwards send requests to an SMS provider (e.g., Twilio). If the provider enforces a hard throughput limit, a sudden traffic spike can overwhelm your capacity. This article explains strategies to handle excess requests safely while preserving user experience and provider limits. Assumptions
  • Provider limit: 100 texts/sec
  • Normal traffic: 20 texts/sec
  • Burst traffic after an outage: 1,000 requests/sec
Your backend can only forward at 100/sec, so 900 requests/sec cannot be sent immediately. The question: what do you do with those excess requests?
A stylized diagram titled "LOGIN BY SMS" showing many mobile phones and message dots funneling down into a backend system and then to a Twilio SMS box with rate limits noted. The graphic highlights message flow and throughput (e.g., 1,000/sec to the backend, Twilio limit 100/sec).
Callout: warning about retry storms
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.
A dark infographic slide with the headline "REJECT THEM" and a circled "A" showing a grid of smartphones all displaying error messages. The graphic illustrates "Thousands of users retry" and a denial-of-service scenario with traffic rising to ~5,000 requests/sec.

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.
A dark infographic showing an SMS delivery flow: a backend sends messages into a growing queue which forwards to Twilio SMS while many phone icons show success or failure. Headings read "QUEUE THEM" and "CODE ARRIVES LATE," illustrating delayed code delivery.
  • 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 Requests with a Retry-After header 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:
  1. On request, compute key = sms:last_sent:{phone}.
  2. Try to create the key atomically with expiry (SET NX EX).
  3. If set succeeds:
    • Generate a code.
    • Enqueue send job (e.g., push to Redis list or broker).
    • Return success.
  4. If key exists:
    • Return success or informative response without enqueuing.
    • Include Retry-After header (ttl of the key) so clients can show cooldown.
Example (Flask + Redis — conceptual):
Notes on global capacity saturation
  • 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 200 without enqueueing (because there is already a scheduled send).
    • For new/different phones, return 429 Too Many Requests with Retry-After until capacity returns.
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:
    • 200 for collapsed successes (where a code was recently sent),
    • 429 Too Many Requests for rate-limited requests where no collapse is possible.
    • Include Retry-After headers so clients can back off.
  • Improve client UX: disable resend button, display cooldown timer, and handle Retry-After.
Which approach to choose depends on your business goals (UX vs. strict rate control) and tolerance for delayed messages. In practice, combine coalescing, sensible cooldowns, and global throttling for a robust solution.

Watch Video