
Understand it
When a producer is faster than a consumer, an unbounded channel converts a throughput problem into a memory problem and then an OOM. Backpressure means the queue has a bound and the producer waits, sheds load, or fails fast when it is full. A bounded async send may wait for capacity; a synchronous bounded send may block, while a nonblocking send can fail immediately.
Backpressure keeps a fast producer from accumulating unlimited unfinished work. A bounded queue sets a maximum number of pending items; when full, the producer can wait, reject, drop according to a policy, or reduce its input rate. Those choices change user-visible behavior. Waiting can propagate pressure upstream; rejecting with a clear retry signal can protect latency for requests already accepted; dropping may be valid for telemetry but unacceptable for payments. The bound should reflect memory budget and acceptable queue delay, not an arbitrary round number.
Walk through a service that receives 1,000 jobs per second while workers complete 800. An unbounded queue grows by 200 jobs every second. If each job uses 50 KiB, memory grows rapidly even though the workers are healthy. A 1,000-item bound exposes overload after a short burst. Then the product must choose what happens to the next job. In an async channel, sending to a full bounded queue may wait; in a synchronous channel it may block, and a nonblocking try_send may return an error. Name the actual API.
In an interview, mention both throughput and latency. A queue can hide overload for a while while making every response slower. Measure queue depth, age of oldest item, reject rate, and worker utilization. Consider fairness so one noisy client cannot fill the queue for everyone. A good design states its overload behavior explicitly and tests it under sustained pressure, not just at a happy-path average rate.
A bounded queue alone does not guarantee fairness. One noisy producer can occupy every slot and starve another tenant. Consider per-client quotas or weighted scheduling when isolation matters, and measure the age of queued work as well as the count. The oldest item often reveals overload earlier than aggregate throughput does.
Interview follow-up: Choose between waiting, rejecting and dropping for telemetry versus financial work. How do you bound bytes when messages have different sizes?
Read the code
let (tx, rx) = tokio::sync::mpsc::channel(256);
// Waits once 256 are queued, instead of
// growing until the process dies.
tx.send(job).await?;Tokio channel fragment: supply job and the enclosing async Result context; a receiver and shutdown policy are still required.
Practice starter
// Arrival rate exceeds completion rate for a sustained interval.
// No rejection, upstream rate limit or queue-size bound is present.Try it
Replace an unbounded queue with a bounded one and decide what happens when full.
What success looks like: The system slows down under load instead of running out of memory.
Check your understanding
What risk remains when an unbounded queue has a producer that persistently outpaces its consumer?
Reveal the answer
- The queue silently drops the oldest accepted item at its capacity.
- Pending work and memory can grow without a configured queue limit.
- The sender automatically awaits a capacity slot in the unbounded queue.
The outcome may be increasing delay, allocation failure, shutdown or an OOM kill. Unbounded does not promise one specific failure mechanism.
Go deeper
Common misconception: The sender automatically awaits a capacity slot in the unbounded queue.
An unbounded queue has no capacity to wait for, so the send succeeds at once and the backlog grows. Only a bounded queue can make a sender wait.
- Identify the contract: A queue with no limit is a crash with a delay.
- Trace the concrete example: The bound is the backpressure; awaiting send is the signal.
- Test the changed constraint: Choose between waiting, rejecting and dropping for telemetry versus financial work. How do you bound bytes when messages have different sizes?
Transfer challenge: Choose between waiting, rejecting and dropping for telemetry versus financial work. How do you bound bytes when messages have different sizes?
Connections
- What TLS Guarantees: Identity, confidentiality, integrity
- Transactions and Isolation: Which anomaly each level allows
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Backpressure (courseId "interview", lessonId "q-backpressure") Link: https://crabrix.com/learn/interview/q-backpressure/ Goal: Every queue needs a bound and a policy for what happens at that bound. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=interview/q-backpressure) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.