A common anti-pattern in Rust backend development is using the asynchronous Tokio runtime for compute-intensive tasks, which inevitably leads to worker thread starvation.
Workload Classification
-
CPU-bound workloads (image processing, compression, parsing gigabytes of JSON):
- Use Rayon. Its work-stealing scheduler automatically distributes iterations across all available physical cores with near-zero synchronization overhead.
-
IO-bound workloads (HTTP servers, database queries, gRPC streaming):
- Use Tokio. Cooperative scheduling backed by
epoll/kqueueenables handling hundreds of thousands of concurrent connections on a tiny memory footprint.
- Use Tokio. Cooperative scheduling backed by
// Parallel array reduction with Rayon
use rayon::prelude::*;
fn compute_parallel_metrics(data: &[f64]) -> f64 {
data.par_iter()
.map(|&x| (x * 2.5).sin().powi(2))
.sum()
}
For long-running background tasks that run indefinitely throughout the application lifecycle, a dedicated OS thread created with std::thread::Builder is typically the cleanest approach.