Sub topic 7
Decoupling with queues and channel workers
This chapter assumes you know what a message queue is, can read short Python snippets, and have seen the high-level architecture from the earlier chapter. It covers how queues separate accepting an event from delivering it, and how per-channel workers do the delivery.
Why put a queue between ingestion and delivery
Delivery providers are slow and sometimes fail. If the ingestion API called them directly, a slow SMS gateway would slow every producer. A queue lets the API store the event, return quickly, and leave delivery to workers that run on their own schedule. The queue also absorbs bursts. A flash sale can create a million events in a minute, and the workers drain them at the rate the providers allow.
One queue per channel
Push, SMS, email and in-app messages use different providers, with different latencies, rate limits and failure patterns. Give each channel its own queue and worker pool.
| Choice | Benefit | Cost |
|---|---|---|
| One shared queue | Simple to run | An email outage blocks SMS and push behind it |
| One queue per channel | Failures stay isolated, and each pool scales on its own | More queues to monitor |
A router sits between ingestion and the channel queues. It reads an event, applies the user's preferences (for example with choose_channels), and writes one message per selected channel.
A minimal version
This in-process sketch uses Python's queue.Queue in place of a real broker. The shape is the same: a router fans out, and one worker per channel consumes.
import queue
import threading
CHANNELS = ["push", "sms", "email", "in_app"]
queues = {c: queue.Queue() for c in CHANNELS}
def route(event, channels):
for channel in channels:
queues[channel].put(
{"event_id": event["event_id"], "user_id": event["user_id"], "channel": channel}
)
def worker(channel):
q = queues[channel]
while True:
msg = q.get()
if msg is None: # shutdown signal
break
print(f"{channel} worker sending {msg['event_id']} to {msg['user_id']}")
threads = [threading.Thread(target=worker, args=(c,)) for c in CHANNELS]
for t in threads:
t.start()
route({"event_id": "evt-1001", "user_id": "user-42"}, ["push", "email"])
for q in queues.values():
q.put(None)
for t in threads:
t.join()Acknowledgement and delivery guarantees
Real brokers keep a message until the worker acknowledges it. If the worker crashes first, the broker makes the message visible again and another worker picks it up. This gives at-least-once delivery, which means a message is never lost but can arrive twice. Later chapters cover the retry policy and the deduplication keys that handle those repeats.
Put messages that keep failing into a dead-letter queue after a fixed number of attempts. This keeps one bad message from blocking the queue and gives operators something to inspect.
Scaling workers
Each pool scales independently. A common signal is queue depth, or the age of the oldest message. When the email queue grows, add email workers and leave the others alone. Cap each pool at the rate its provider allows, since extra workers beyond that only trigger throttling.
Exercise
Why does a per-channel queue protect push notifications when the email provider has an outage?
Show answer
Email messages pile up only in the email queue, and only email workers are blocked or retrying. Push messages sit in a separate queue with their own workers, so they keep flowing. With one shared queue, the stuck email messages would delay everything behind them.