Bounded concurrency and backpressure
Quick answer
Backpressure slows or rejects producers when consumers cannot keep up. Bounded channels limit queued messages; semaphores limit simultaneous resource users.
Limit queues and active work separately
A bounded Tokio mpsc send awaits capacity, while try_send reports failure immediately. A semaphore permit represents a unit of capacity and releases it on drop. Acquiring permits inside an unlimited number of spawned tasks still allows an unlimited number of waiting tasks; acquire before spawning or use bounded workers. Count limits alone do not bound message bytes.
#[tokio::main(flavor = "current_thread")]async fn main() { let (tx, mut rx) = tokio::sync::mpsc::channel(1); tx.send(3).await.unwrap(); assert!(matches!(tx.try_send(4), Err(tokio::sync::mpsc::error::TrySendError::Full(4)))); println!("{}", rx.recv().await.unwrap());}Manual local Rust/Cargo candidate; not executed. Expected behavior:
3The queue has no room for a second item yet.
use tokio::sync::Semaphore;#[tokio::main(flavor = "current_thread")]async fn main() { let limit = Semaphore::new(1); let permit = limit.acquire().await.unwrap(); assert!(limit.try_acquire().is_err()); drop(permit); assert!(limit.try_acquire().is_ok()); println!("capacity restored");}Manual local Rust/Cargo candidate; not executed. Expected behavior:
capacity restoredDropping the permit returns capacity.
Common mistake
Do not replace an unbounded queue with an unbounded collection of waiting tasks.
Try it yourself
Design a pipeline with queue capacity 8 and two active workers; explain how a producer is slowed.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Bounded concurrency and backpressure — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
An application spawns a million tasks that each wait on a semaphore. What remains unbounded?
Bounded concurrency and backpressure — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust #[tokio::main(flavor = "current_thread")] async fn main() { let (tx, mut rx) = tokio::sync::mpsc::channel(1); tx.send(3).await.unwrap(); assert!(matches!(tx.try_send(4), Err(tokio::sync::mpsc::error::TrySendError::Full(4)))); println!("{}", rx.recv().await.unwrap()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Bounded concurrency and backpressure — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use tokio::sync::Semaphore; #[tokio::main(flavor = "current_thread")] async fn main() { let limit = Semaphore::new(1); let permit = limit.acquire().await.unwrap(); assert!(limit.try_acquire().is_err()); drop(permit); assert!(limit.try_acquire().is_ok()); println!("capacity restored"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.