Skip to content

Lesson 9 of 9 · 26m

Draining, Scaling, and Incident Operations

Operate queues under stress: pause producers, scale consumers safely, shed low-priority work, drain shards, and recover without replay storms.

The worst queue incidents are amplification incidents. The backlog grows, someone adds ten times as many workers, workers overwhelm the database, latency worsens, retries increase, and now the queue is larger and the primary service is down. Operational control means knowing which lever reduces load rather than merely moving it.

First stabilize the boundary

When backlog is growing, establish four facts before touching capacity:

1. Are workers running and fetching?
2. Are jobs succeeding or retrying?
3. Is a downstream dependency saturated?
4. Is incoming traffic unusually high?

The answers decide the lever. If workers are down, fix consumers. If they are up but failing, scaling them increases failure traffic. If the dependency is saturated, reduce concurrency or pause noncritical queues. If traffic is legitimately high, capacity planning may actually help.

Scaling is not free

Each consumer adds connections, memory pressure, lock contention, and downstream QPS. Scale in measured steps:

kubectl scale deployment/billing-worker --replicas=12

Watch database connections, p95 job latency, lock wait time, retry rate, and downstream error rate. If completion rate does not rise proportionally, stop. You hit a bottleneck elsewhere. For partition-based systems, consumer count is bounded by partition count. Twenty consumers on six Kafka partitions means fourteen idle consumers. Queue systems with competing consumers do not have that exact limit, but they still have hot-key and locking constraints.

Shed load by priority

Not all jobs deserve the same survival odds. Separate queues by class before the incident:

critical      password reset, payment capture, security notification
normal        invoice email, search indexing, report generation
bulk          imports, exports, analytics backfills

During degradation, pause bulk consumers and protect critical lanes. A single bulk import should not starve a password-reset email. Priority implemented only inside one FIFO queue is fragile because head-of-line blocking still applies.

Drain deliberately

Deploys and node maintenance need graceful shutdown. The worker should stop fetching new messages, finish in-flight work within a bounded grace period, heartbeat or release what it cannot finish, then exit:

def shutdown(signum, frame):
    consumer.stop_fetching()

with graceful_timeout(seconds=25):
    worker.wait_for_inflight()
consumer.close()

If termination is abrupt, messages reappear after visibility timeout. That may be acceptable, but know the duplication implication. Do not simply kill workers during a deploy and call it rolling.

Recovery is a second incident waiting to happen

Once the dependency recovers, a million queued jobs wake up. This is the replay storm. Control it:

resume consumers gradually
cap concurrency below recovered dependency capacity
prioritize fresh critical work before old bulk work
use dead-letter replay rate limits
watch retry amplification for the first hour

Sometimes the right answer is to discard or compact stale work. Cache refreshes and presence updates may be worthless after an hour. Customer money movement is not. You only know if job classes have explicit freshness semantics.

Runbooks beat heroics

A queue incident runbook should name the dashboards, owner, pause commands, scaling limits, replay commands, destructive actions requiring approval, and customer communication triggers. In an outage, nobody should be reverse-engineering queue topology from Terraform while messages pile up.

Field notes

  • Never scale workers against a downstream rate limit without doing arithmetic. You are purchasing faster failure, not capacity.
  • Pausing a producer is often safer than pausing consumers, but only if producers can buffer or reject gracefully.
  • Keep one low-traffic synthetic job flowing through critical queues. It reveals end-to-end stalls before customers report them.
  • After the incident, compare delivered side effects with queue records. Duplicated notifications and orphaned jobs are common recovery residue.

Was this lesson helpful?

Sign in to rate