Video in progress

Staff 🌱 Spring

In a system where several services send email, SMS and push notifications, how do you design the notification service?

One central notification service takes in events, applies templates and user preferences, and sends per channel through queues. The most important parts: idempotency, the ability to swap providers, rate limits, retry, and tracking that a message was delivered.

📖 Answer

If every service calls the SMS provider directly: the keys live in 10 places, templates are scattered, if a user says "don't send me SMS" you need to check in 10 places, and if you switch providers, that is 10 PRs.

Architecture:

services --(event: OrderShipped)--> Kafka --> notification-service
     notification-service:
       1. to whom, on which channel (user preferences, language, quiet hours)
       2. render the template (versioned)
       3. write to the channel queue (email / sms / push)
     channel workers --> providers (primary + backup)
     provider callbacks --> delivery status

Key decisions:

  • The input is a business event, not text. Services say "order shipped, id=..."; what to write and in which language is the notification service's job. That way changing a text doesn't need another team.
  • Idempotency. Kafka may deliver a message twice, and a customer must not get two SMS. A unique key on notification(event_id, channel) and a check before sending.
  • Priority queues. An OTP code must not wait behind marketing messages: separate queues and workers.
  • Rate limits and provider limits. The SMS provider has a per-second limit; workers are throttled with a token bucket. Separation also keeps a marketing campaign from choking OTP.
  • Switching providers (failover). Several providers behind a channel interface; if the error rate rises, switch to the backup. A circuit breaker fits here.
  • A retry limit. An OTP is useless after 5 minutes, so don't resend an expired message.
  • State and audit. QUEUED → SENT → DELIVERED/FAILED, with provider callbacks. Customer support checks an "SMS didn't arrive" complaint here.
  • User preferences and the law. Unsubscribe, quiet hours (marketing isn't sent at night), separating transactional and marketing messages.

On the Spring side these are ordinary components: @KafkaListener, a TaskExecutor per channel, RestClient for providers and Resilience4j. The complexity is not in the code but in the agreements, which is why this service needs a clear owner and contract.