Dashboard
Courses
Problems
Badges
Theme
Sign inStart free
Courses/Asynchronous and Event-Driven Systems
Lesson 10 of 20

Backpressure and flow control

What to do when producers outrun consumers, because a queue only buys time.

15 min read

When producers outrun consumers

A queue smooths bursts, but it cannot fix a sustained mismatch. If 2 000 messages arrive per second and workers handle 1 500, the backlog grows by 500 per second, or 1.8 million per hour, and the wait for each new message grows with it. Little's law makes this precise: the number of items in a system equals the arrival rate times the time each spends there. Once arrivals exceed capacity, waiting time grows without limit. On the SystCode canvas, a consumer running above 100% utilisation means its queue grows forever: add workers or reduce the work per message.

Bound everything

Unbounded queues turn overload into slow death: memory fills, latency climbs to hours, and when the system finally crashes it loses everything in the buffer. Every queue, buffer and in-memory channel should have a limit, and you should decide in advance what happens when it is full.

  • Slow the producer: block, or make it wait for credit (TCP flow control, reactive streams demand)
  • Reject: return 429 or 503 at the edge and let clients back off (load shedding)
  • Drop or sample: acceptable for telemetry and metrics, never for payments
  • Scale consumers: autoscale on queue lag rather than CPU
  • Prioritise: separate queues so a bulk import cannot starve password-reset emails

Pull consumers get backpressure for free

Pull-based consumers (Kafka, SQS long polling) only fetch what they can handle, so they cannot be overwhelmed; the backlog simply stays in the broker. Push-based delivery needs explicit limits: RabbitMQ's prefetch count caps how many unacknowledged messages a consumer holds, typically 10 to a few hundred. Without it, the broker floods one consumer's memory while others sit idle.

Measure lag, not just depth

Queue depth alone is misleading: 100 000 messages is fine for a fleet that drains 50 000 per second and a disaster for one that drains 50. Track the age of the oldest message and consumer lag in time, and set an objective such as 'the oldest unprocessed message is younger than 60 seconds'. Alert on that, and autoscale on it.

Key takeaways

  • If arrival rate exceeds capacity for long, no queue is big enough
  • Bound every buffer and choose what happens when it is full: slow, reject, drop or scale
  • Pull consumers and prefetch limits keep consumers from being overwhelmed
  • Alert and autoscale on the age of the oldest message, not raw depth

Sign in to track lessons and earn badges.

Dead-letter queues and poison messagesScheduling and delayed jobs

Challenge checkpoint

Course: 0/20 lessons · 0/10 challenges0%

Build it on the canvas, pass the review (70+) and mark it complete to count it toward the course badge.

  1. 3/10Design a Newsletter SenderDrain 5 million sends through a rate-limited email provider without overwhelming it or sending twice.Easy
All 10 challenges →