Prompt
How do I rate limit email sends in my app?
Latest observation
To rate-limit email sends, you typically want two layers of protection:
- Per-user / per-action limits to stop abuse from a single account or endpoint
- Global sending limits to stay within your email provider’s quotas and protect deliverability
Common approaches
1) Fixed window counters
Track how many emails were sent in a time window.
Example:
- Max 5 verification emails per hour per user
- Max 20 password reset emails per day per IP
Store counters in Redis or your database with an expiry.
Redis example idea
- Key:
email:sent:user123:verification:2026-11-13T10 - Increment each send
- Reject if count exceeds limit
Pros: simple
Cons: can allow bursts at window boundaries
2) Sliding window
Track sends over the last N minutes/hours more accurately.
Instead of counting per calendar hour, count emails sent in the last 60 minutes.
Pros: smoother and fairer
Cons: slightly more complex
3) Token bucket / leaky bucket
Give each user or system bucket of “send tokens.”
- Emails consume tokens
- Tokens refill over time
Example:
- 10 tokens max
- refill 1 token every 6 minutes
- sending an email uses 1 token
Pros: good for burst handling
Cons: more logic to implement
What to rate limit
A good setup is to rate limit by:
- User account
- Email address
- IP address
- Action type
(signup verification, password reset, invoice, marketing, etc.)
Examples:
- Password reset:
3 per hour per email - Verification resend:
5 per day per account - Marketing batch sends: global per minute based on provider limits
Implementation tips
Use Redis if possible
Redis is ideal because it’s fast and supports atomic increments and expiration.
Pseudo-flow:
- Build a key from user/action/time bucket
- Increment the key atomically
- Set expiry if first time
- If count > limit, block send
- Otherwise send email
Make the send idempotent
If your app retries after a timeout, you don’t want duplicate sends. Use a unique request ID or dedupe key.
Queue emails
For production, put sends into a job queue:
- app enqueues email job
- worker checks rate limit
- worker sends email
- if over limit, job is delayed/requeued or discarded
This prevents spikes and keeps your app responsive.
Example policy
A practical default:
- Per user: max 5 transactional emails / hour
- Per email address: max 3 password reset emails / hour
- Per IP: max 20 email-triggering requests / hour
- Global: match your provider’s limits (e.g. 100/sec, depending on service)
Extra safeguards
- Add CAPTCHA after repeated requests
- Add cooldown timers in the UI (“Try again in 2 minutes”)
- Log rate-limit rejections
- Monitor delivery/bounce/complaint rates
If you want a quick Redis pattern
A simple fixed-window key:
key = "rate:email:verification:user123:2026-11-13T10"
INCR key
EXPIRE key 3600
if value > 5: reject
If you tell me your stack (Node, Python, Rails, Django, etc.) and whether you use Redis, I can show you a concrete implementation.
Brands