Sistema de Notificaciones

Push · Email · SMS

Escrito por Darjus Kazakevic · revisado el

One queue per channel, and a key that makes every retry safe.

Other services ask for a notification, such as "order shipped" for user 42. The system picks the channels (push, email, SMS), applies the user's settings and quiet hours, fills a template in the user's language, and sends it through third-party providers.

It must never lose a notification that matters, like a login code, and must keep duplicates rare. A slow or failing provider must not block the other channels. Most messages can arrive a few seconds late; one-time codes need under 10 seconds.

En esencia — Accept each request once, put one job per channel on its own queue, and let workers retry each provider call with the same idempotency key until it succeeds.

Cuándo recurrir a él

Recurre a él cuando

  • Several services send messages to users by push, email or SMS
  • A duplicate costs money or trust, like an SMS code or a payment alert
  • Users need one place to set channels, quiet hours and opt-outs

Reconsidéralo cuando

  • One app sends a few emails a day: call the provider directly
  • Messages form a conversation that must stay in order: build a chat
  • Marketing needs segments and A/B tests: use a campaign tool

The components

Notification API (uses a rate limiter)
The one entry point for every team. It validates the request, attaches an idempotency key (a unique name for this send), applies per-user limits, and creates one job per channel.
Preferences and templates
Per user and channel: opted in or out, quiet hours, language, and the address or device token. Templates turn an event and its data into text for each channel and language.
Channel queues (uses a message queue)
One queue per channel (push, email, SMS), each with its own worker pool. Login codes go on a separate high-priority queue so a newsletter cannot delay them.
Channel workers and providers (uses retry with backoff)
Workers call APNs and FCM for push, SES or SendGrid for email, Twilio for SMS. They retry with growing, random delays and stop calling a provider that keeps failing.
Delivery log
One row per key with its status: pending, sending, sent, retry, failed or skipped. It is the duplicate check, the audit trail, and the source for delivery reports.

Key decisions

Make every send idempotent
Queues redeliver jobs and callers repeat requests. Store a key per notification and channel, and skip a send whose key is already sent. A provider can time out after sending, so pass it the key if it accepts one.
Give each channel its own queue
SMS providers are slow and costly; push is fast and cheap. Separate queues and workers mean an email outage cannot delay a push. Put one-time codes on a high-priority queue.
Check preferences at send time
Read opt-outs just before the send, not when the request arrives. A user who unsubscribes at 9:00 must not get the digest that was queued at 8:55 and sent at 9:05.
Limit per user, not only per provider
A bug in one service can ask for 500 notifications for one user in a minute. Cap each user per channel per hour (for example, 3 SMS) and merge the rest into a digest.

Errores habituales

  • Sending without a key. A caller repeats its request after a timeout, or the queue delivers the job twice, and the user gets the login code twice.
  • One shared queue for every channel. A slow email provider fills the queue, and login codes wait behind a million newsletters.
  • Keeping dead push tokens. A phone that removed the app returns an "unregistered" error. Delete the token, or you keep paying for sends that fail.
  • Rows stuck in 'sending' after a worker crash. Without a sweeper that resets old claims, those notifications are never sent, and nobody notices.