Sub topic 13
Prioritizing urgent notifications
This chapter assumes you know how message queues and channel workers fit together, and that you can read short Python snippets. Here we cover how to make sure a password reset code is not stuck behind a million promotional messages.
Why priority matters
A single shared queue is first in, first out. If a marketing campaign enqueues two million emails, a one-time login code that arrives right after waits until the campaign drains. For a code that expires in five minutes, that is a failure.
Priority is a property of the event, set by the producer. In the event model from earlier chapters it is the priority field. Typical levels are critical (security codes, fraud alerts), high (order and payment updates), normal (social activity), and low (promotions, digests).
Separate queues per priority
Do not sort one big queue. Use one queue per priority level and let workers favor the higher ones.
There are two common ways for workers to consume these queues:
| Strategy | How it works | Risk |
|---|---|---|
| Strict priority | Always drain critical first, then high, and so on | Low queues can starve forever |
| Weighted | Take roughly 8 critical, 4 high, 2 normal, 1 low per cycle | Slightly slower for critical under load |
Weighted consumption is the safer default because low-priority messages still make progress.
import itertools
WEIGHTS = {"critical": 8, "high": 4, "normal": 2, "low": 1}
def next_queue_order():
"""Yield queue names in a repeating weighted order."""
order = []
for name, weight in WEIGHTS.items():
order.extend([name] * weight)
return itertools.cycle(order)
order = next_queue_order()
print([next(order) for _ in range(15)])A worker loops over this order and polls the named queue. If that queue is empty, it moves on immediately instead of waiting.
Keep capacity for urgent traffic
Queues alone do not help if every worker is busy with a slow low-priority send. Two techniques address this:
- Reserve a dedicated worker pool for
critical, sized for your peak urgent load. - Cap how many workers may handle
lowat once.
Priority interacts with other controls
- Rate limits: Apply per-user limits to low and normal traffic. Exempt critical security messages, or give them a separate, much higher limit.
- Quiet hours: As in
choose_channels, onlycriticalevents bypass quiet hours. Never let producers mark promotions as critical to get around this. - Retries: Retry critical messages sooner and with a shorter cap, since their value decays fast.
Common errors
- Trusting the producer's priority without validation. Allow only specific services to emit
critical. - Using strict priority and never noticing that low-priority messages are delayed for hours.
- Sharing a provider rate limit between priorities, so a bulk send throttles urgent ones. Use separate provider credentials or reserved quota where the provider allows it.
Exercise
Why is a weighted consumption order usually safer than strict priority, and what is one drawback?
Show answer
Strict priority can starve low queues indefinitely while higher queues stay busy. Weighted consumption lets every queue make progress. The drawback is that critical messages can wait a little longer under heavy load, because workers spend some cycles on lower priorities.