Sub topic 16
Scaling and reliability
This chapter assumes you know how the queues and channel workers from earlier chapters fit together. It also assumes you know why retries and idempotency keys exist. Here you will use them to keep the system running as load grows and parts of it fail.
Scale each stage on its own
A notification system has three load points: the ingestion API, the queues, and the channel workers. Each one scales differently.
- Ingestion API: Keep it stateless and put it behind a load balancer. Add instances as request volume grows.
- Queues: Partition them. More partitions allow more parallel consumers.
- Workers: Run them as a separate pool for each channel. Scale each pool on queue depth, meaning the number of waiting messages, or on message age.
Separate pools matter because channels behave differently. A slow SMS provider should not use up the threads that send push notifications.
import hashlib
def partition_for(user_id: str, partitions: int) -> int:
digest = hashlib.sha256(user_id.encode()).digest()
return int.from_bytes(digest[:8], "big") % partitionsPartitioning by user keeps one user's messages in order within a partition. Use a stable hash like this one. Python's built-in hash() for strings changes between processes, so two workers would disagree about where a user belongs.
Isolate failures
If the email provider goes down, only the email queue grows. Push delivery continues. Three mechanisms support this:
- Dead-letter queue (DLQ): A queue for messages that still fail after the retry limit. Inspect it, fix the cause, then replay the messages. This stops a bad message from blocking the queue.
- Circuit breaker: After repeated provider failures, stop calling the provider for a short time. Then send one trial request. This protects the provider while it recovers and frees your workers.
- Bounded retries: Retries with backoff and jitter help with brief failures. Unbounded retries during a long outage create a retry storm.
Delivery guarantees
Most queues give at-least-once delivery. A message may arrive twice, for example when a worker crashes after sending but before acknowledging. Exactly-once delivery to an external provider is not practical, so the usual design is at-least-once delivery combined with deduplication. Workers claim the idempotency key before sending, which turns duplicates into skipped work.
Do not acknowledge a message until the send succeeds or the message is moved to the DLQ. Acknowledging early loses notifications when a worker crashes.
Plan for spikes and outages
- Absorb bursts: A marketing campaign can send millions of messages at once. Queues hold the burst while workers drain it at a safe rate.
- Protect urgent traffic: Keep a separate queue or reserved capacity for critical messages such as security codes, so bulk traffic cannot delay them.
- Use backup providers: Keep a secondary provider for critical channels such as SMS. Fail over only for failure types that make sense, because a second provider adds cost and more behavior to test.
- Replicate state: Run the queue and the deduplication store across multiple availability zones. If the dedup store is lost, you risk duplicates. If it is unreachable, decide whether to fail open (risk a duplicate) or fail closed (risk a delay), and choose per notification type.
Common errors
| Error | Result |
|---|---|
| One shared queue for all channels | One slow provider blocks every channel |
| Acknowledging before sending | Lost notifications after a crash |
| No DLQ | Poison messages retry forever |
| Retries without jitter | Synchronized load hits a recovering provider |
Exercise
The SMS provider starts returning errors for every request. Name two mechanisms that keep this from affecting push delivery, and one that stops workers from hammering the SMS provider.
Show answer
Separate queues and worker pools per channel keep SMS backlog and failures away from push. A dead-letter queue also keeps failing SMS messages from blocking the SMS queue. A circuit breaker stops SMS workers from calling the failing provider until a trial request succeeds.