← Building blocks

Keep urgent work ahead of bulk work

Decoupling & async worksenior

A marketing blast of 10M messages delays a password-reset email.

Say it like this

A marketing blast must never delay a one-time passcode, so transactional and bulk work get separate queues and separate worker pools, with weighted consumers so the bulk lane still drains.

Basic solution

Separate queues (or partitions) per priority and per channel, with their own worker pools and limits.

per-priority queuesSQS FIFO + standardweighted consumers

Lanes per channel and priority

The producer classifies each message into a lane. Lanes have their own pools, so a 20M campaign in the marketing lane never sits in front of an OTP. Both pools draw from one provider token bucket, transactional first.

How it works

  1. When a notification is enqueued, the producer classifies it along two axes: the channel (push, SMS, email) and the priority (transactional, marketing). The classification comes from a server-side registry of notification types, never from the caller, so a caller cannot promote its own traffic.
  2. Each channel × priority pair is its own queue (or its own set of topic partitions), such as sms-transactional and sms-marketing. A backlog in one queue is physically separate from the others, so it cannot sit in front of their messages.
  3. Each queue has its own worker pool with its own concurrency, retry policy and share of the provider rate limit. Transactional pools are always running; bulk pools scale up when depth grows and down when it drains.
  4. Where one pool must serve several lanes, it takes work with weights rather than strict priority: for example 8 reads from the transactional lane for every 2 from the bulk lane. Weights guarantee the low lane always makes progress instead of waiting until the high lane is empty.
  5. Monitoring is per lane. For transactional lanes the alert that matters is the age of the oldest message, because a delay is the failure; for bulk lanes, depth is enough.

Why not one queue with a priority field

  • Most brokers store a backlog in arrival order and can only reorder a small window near the head. A priority field therefore only helps when the queue is nearly empty, which is exactly when you do not need it.

Worked example

Notification system during a campaign

  • Steady state: 2,900 notifications/s. A campaign enqueues 20M messages in 10 minutes, 33K/s, while 50 OTP messages/s keep arriving.
  • One shared queue: the OTP enqueued at minute 1 sits behind ~2M campaign messages. At 5K sends/s that is a 400 s wait; the OTP has expired before it is sent.
  • Separate lanes: sms-transactional has 50/s arriving and a pool sized for 500/s, so OTPs go out in under a second regardless of the campaign. sms-marketing holds the 20M backlog and drains at whatever the provider allows, 1,000/s, over about 5.5 hours.
  • Provider limit shared across lanes: a 1,000/s token bucket where transactional takes tokens first; marketing gets the remainder, 950/s during the campaign.
  • Cost: six queues and six small pools instead of one, roughly the same total workers, plus per-lane dashboards.

Variants and when each wins

Separate queues per lane

  • One queue per channel × priority with its own dedicated workers. It wins on isolation and clarity, which is why it is the standard answer; it is the most infrastructure to manage.

Weighted consumers over several queues

  • One pool polls several queues in a weighted round-robin. It wins when the number of lanes makes a pool per lane unwieldy; the cost is that a slow bulk job can occupy a worker that a transactional job needed.

Broker-native priority

  • RabbitMQ priority queues or SQS message groups reorder messages within the broker's limits. It wins for small backlogs; once the backlog is bigger than the window the broker reorders, the priority field stops helping.

Deferred scheduling for bulk

  • Bulk work is never put on the queue all at once; a scheduler releases it in slices sized to spare capacity. It wins for campaigns you control the timing of; it is useless for bursts you do not.

Separate queues with a shared provider token bucket is the right default; add weighted consumers only when the number of lanes gets unwieldy.

Failure modes

Starvation of the low lane

  • Marketing messages never go out while transactional traffic is high. It happens because the scheduler uses strict priority, so the bulk lane only gets a turn when the transactional lane is empty, which during a busy period is never. Use weighted scheduling or give every lane a guaranteed minimum worker count.

Misclassified work

  • An OTP waits behind a campaign despite the separate lanes. It happens because the caller labelled it marketing, or a newly added notification type fell into the bulk lane by default. Classify from a server-side registry of types, and make transactional the default for unknown types.

Lane shares a provider limit

  • Transactional sends get 429 responses from Twilio during a campaign. It happens because each pool enforces its own rate limit and the sum of the pools exceeds what the provider allows. Put one shared token bucket in Redis sized to the provider's limit, and let the transactional lane take tokens first.

Too many lanes

  • A dozen queues sit around with idle workers and nobody knows which team owns which. It happens because a lane was created per feature rather than per isolation need. Keep a lane only where a backlog in one must not delay another, which is usually channel × two priorities.

Bulk lane fills memory or disk

  • The broker runs out of space when a 20M-recipient campaign lands. It happens because the entire audience was enqueued at once. Keep the audience in a database table and have a scheduler enqueue it in slices.

Interviewer follow-ups

Answer out loud before you open each one.

How do you decide what counts as high priority?

Anything a user is actively waiting for or that is time-boxed: OTPs, password resets, fraud alerts, ride offers. Decide it per notification type in a server-side registry so callers cannot promote their own traffic.

Why not one queue and sort by priority?

A backlog is stored in arrival order, and most brokers only reorder a small in-memory window. Once two million campaign messages are ahead, the priority field cannot move an OTP past them. Separate queues avoid the backlog entirely.

How do you keep the low lane from starving?

Give it a guaranteed share: either its own minimum worker count or weighted polling such as 8:2. Monitor oldest-message age per lane so a starving lane is visible before customers notice.

What happens when both lanes hit a provider rate limit?

Both pools draw from a shared token bucket sized to the provider limit, and the transactional lane takes tokens first. Marketing then runs on whatever is left and simply takes longer, which is acceptable for bulk.

Connections

Built on
Heap / priority queue
Pairs with
Decouple producer from consumer · Limit requests per client · Isolate failures · Run work later or on a schedule · Slow the producer when the consumer is behind
Pulls against
Drop load before you fall over
Used in
Notification System · Video Streaming Platform (YouTube / Netflix)

The same parts on other clouds

The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Amazon EC2 Auto ScalingProducer, Transactional pool, Marketing poolCompute Engine managed instance groupsVirtual Machine Scale SetsKubernetes Deployment with a HorizontalPodAutoscaler, Nomad
Amazon SQSsms-transactional, sms-marketingPub/Sub, Cloud TasksService Bus queues, Queue StorageRabbitMQ, ActiveMQ