Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Queue-Based Load Leveling
HLD

Queue-Based Load Leveling

Absorbing traffic spikes with a queue between producers and consumers — throughput smoothing as an architecture.

The Problem It Solves

 synchronous path under spike:

 burst 10k rps ──►► service (handles 2k rps) ──► timeouts, errors,
                 direct, unbuffered             retry storms, cascade

 the service receives demand AT THE RATE IT ARRIVES —
 spikes hit the fragile core at full force.

The Pattern

 insert a queue as a shock absorber:

 producers ──► [ QUEUE ] ──► consumers (steady ~2k/s)
   10k rps       depth        paced by consumer capacity

 consumers pull at their own sustainable rate.
 the SPIKE is absorbed as queue depth, not as service collapse.

 latency cost: queued items wait (depth ÷ rate).
   burst of 100k msgs draining at 2k/s ≈ 50s tail for the last one.
   acceptable for image processing; unacceptable for "is this card valid?"

When It Fits vs Doesn’t

Fits wellPoor fit
Video transcoding, thumbnailsInteractive reads
Email/notification sendsPayment authorization
Order fulfillment pipelinesAnything needing sync response
Analytics ingestionReal-time bidding
 decision rule: does the USER wait for the result?
   yes → keep synchronous (or hybrid: ack fast + process async)
   no  → queue it. spikes become backlog, backlog becomes work,
         and the core services never see the storm.

Leveling ≠ Unlimited Buffering

 queues are not infinite sponges:

 - unbounded growth = silently failing system
   (latency climbs, memory fills, messages expire)
 
 pair leveling with BACKPRESSURE policy:
   depth > threshold → shed load / return 429 / degrade gracefully
   the queue buys MINUTES of buffer, not infinite patience
 
 monitor: depth trend, oldest-message age — not just rate.
 "depth flat but age growing" = consumers stuck on poison message

The Bonus Benefits

 beyond spike absorption:
 - consumer failures don't fail producers (temporal decoupling)
   producer publishes even while consumers are down/redeploying
 - independent scaling: workers scale on DEPTH signal —
   textbook elastic tier
 - retry/dead-letter machinery comes with the broker
 - natural place for ordering keys, dedupe, rate shaping

Architecture Shape

 [ api ]──ack──►[ queue: orders ]──►[ worker pool ×N ]──►[ db ]
    │                                                        ▲
    └─ client gets 202 Accepted + status URL                 │
                                       polling/webhook ◄────┘

 async contract with clients: submit → ticket → poll/notify.
 the UX must be designed FOR asynchrony, not bolted on

Interview Framing

“Flash sale at midnight” answers should reach for this immediately: queue in front of order processing, consumers sized to sustainable DB write capacity, depth-based autoscaling, backpressure thresholds, and honest latency math for queued users. Naming what stays synchronous (“auth and payment check stay inline”) shows judgment about which spikes may be buffered.

My Private Notes

Notes are auto-saved locally to this device.