Prompt
How do I retry failed push notifications reliably?
Latest observation
To retry failed push notifications reliably, you want a durable, idempotent delivery pipeline rather than “send and hope.” The key is to separate:
- creating the notification
- attempting delivery
- retrying failures
- tracking final outcome
A reliable pattern
1) Persist every notification before sending
Store each notification in a database with a status like:
pendingsendingsentfailed_retryablefailed_permanent
Include:
- notification ID
- recipient/device token
- payload
- attempt count
- next retry time
- last error
- provider message ID if available
This ensures you don’t lose notifications if your app crashes.
2) Use a queue + worker
Put send jobs onto a durable queue (e.g. SQS, RabbitMQ, Pub/Sub, Kafka, Celery, Sidekiq, BullMQ).
A worker should:
- claim one job
- attempt delivery
- classify the response
- update state
- schedule retry if needed
Avoid doing push sends inline in request/response paths.
3) Retry only retryable failures
Not all failures should be retried.
Retryable examples
- network timeout
- temporary provider outage
- 5xx from push provider
- throttling / rate limiting (
429)
Permanent examples
- invalid token / unregistered device
- malformed payload
- unauthorized credentials
- app removed / token expired beyond recovery
For permanent failures, mark the token invalid and stop retrying.
4) Use exponential backoff with jitter
For retryable errors, retry with increasing delays, for example:
- attempt 1: immediately
- attempt 2: after 1 minute
- attempt 3: after 5 minutes
- attempt 4: after 15 minutes
- attempt 5: after 1 hour
Add jitter so many failed notifications don’t retry at the exact same time.
Example:
- delay =
base * 2^attempt + random(0, small_range)
This helps avoid retry storms.
5) Make sends idempotent
Push providers and your own system should tolerate duplicate attempts.
Use a stable notification identifier, and before sending, check whether it was already marked sent.
Important: push systems may still deliver duplicates if retries happen after the provider accepted the message but your app didn’t record the success. Design for “at least once” delivery, not exactly once.
6) Set a max retry policy
Don’t retry forever.
Example policy:
- max 5 attempts
- max age 24 hours
- stop earlier for permanent failures
After that, move the notification to a dead-letter/failure table for inspection or manual recovery.
7) Handle token cleanup
For mobile push, stale tokens are a major source of failures.
When the provider says the token is invalid/unregistered:
- mark the device token inactive
- stop sending to it
- prompt re-registration on next app launch if needed
This prevents repeated failed retries.
8) Track observability
Log and monitor:
- send success rate
- retry counts
- permanent failure counts
- provider error codes
- queue depth
- age of oldest pending notification
Alert on:
- rising 5xx / 429 rates
- backlog growth
- sudden spike in invalid tokens
Simple retry flow
- Create notification record in DB.
- Enqueue send job.
- Worker sends push.
- If success:
- mark
sent
- mark
- If retryable failure:
- increment attempts
- compute next retry time
- requeue or reschedule
- If permanent failure:
- mark
failed_permanent - invalidate token if appropriate
- mark
- If max retries exceeded:
- mark failed and move to dead-letter storage
Practical recommendations by provider
Firebase Cloud Messaging (FCM)
- Retry on transient HTTP errors and 5xx.
- Treat unregistered token responses as permanent.
- Watch quota/rate-limit errors.
Apple Push Notification service (APNs)
- Retry on transient server errors or connection issues.
- Treat bad device token / unregistered device as permanent.
Common mistakes to avoid
- retrying every error blindly
- no persistence of send state
- infinite retries
- no backoff
- retrying synchronously in user-facing requests
- not invalidating dead tokens
- assuming provider acceptance means device delivery
If you want a robust architecture
Use:
- DB table for notification state
- durable queue
- worker pool
- exponential backoff with jitter
- dead-letter queue/table
- token invalidation
- idempotency keys
If you want, I can give you:
- a database schema for this,
- a retry algorithm/pseudocode, or
- an example implementation in Node.js, Python, or Go.