FlowGuard
🎯 About the Project Created and developed by: Cleiton Augusto Correa Bezerra Adaptive Concurrency Control and Backpressure for Axum/Tower
The Problem: Static Limits are a Guessing Game
When building resilient microservices in Rust, setting a static concurrency limit (like semaphore::Permits or a fixed worker pool) is a common but fragile approach.
- Set the limit too high, and a sudden spike can overwhelm your database or external API, causing a cascading failure.
- Set it too low, and you're wasting resources and unnecessarily throttling valid traffic.
You're left tuning a magic number based on guesses rather than the actual health of your system.
The Solution: Adapt Based on Latency
FlowGuard is a Tower service layer that implements adaptive concurrency control. Instead of a fixed limit, it dynamically adjusts the number of concurrent in-flight requests by monitoring their latency (round-trip time).
- When latency increases, it reduces the concurrency limit, applying backpressure at the edge of your service.
- When the system is responsive, it cautiously increases the limit to utilize available capacity.
The core algorithm is inspired by TCP Vegas, a congestion control algorithm known for its efficiency and low latency.
Quick Start with Axum
Add FlowGuard to your Cargo.toml:
[]
= "0.1"
{routing::get, Router};
{FlowGuardLayer, strategy::VegasStrategy};
ServiceBuilder;
#[tokio::main]
{
// Start with an initial limit of 10 concurrent requests.
= VegasStrategy::new(10);
= Router::new()
.route("/api", get(|| async { "Hello, guarded world!" }))
.layer(ServiceBuilder::new().layer(FlowGuardLayer::new(strategy)));
// ... bind and serve as usual
}
the layer returns a 503 Service Unavailable response, signaling to callers (or upstream load balancers) to back off.
The Vegas Strategy
The minimum observed round-trip time (system's healthy baseline).
The latency of recent requests.
alpha), it infers the system is congested and reduces the concurrency limit. If everything is fast, it slowly probes for more capacity.
= VegasStrategy::new(10)
.with_alpha(2) // Lower = more sensitive to latency increases
.with_beta(4); // Higher = more aggressive in adding capacity
Not Static: Eliminates the need for static concurrency limits.
Axum Native: Works seamlessly with the Rust service ecosystem.
Built with performance in mind using efficient data structures.
Integrates with Tower's error handling to provide clear backpressure signals.
v0.1.x). It implements a proven algorithm, but its integration and edge cases are being refined. The current version is best suited for:
like a database).
you need a distributed coordinator (a planned future feature).
it needs traffic to "learn". Its behavior with very low traffic or bursty patterns is still being observed.
bug reports, and real-world deployment stories are incredibly valuable to help mature this project.
🤝 Contributing
Feel free to submit pull requests or open issues.
Cleiton Augusto Correa Bezerra
augusto.cleiton@gmail.com
cleiton-augusto-b619435b
📄 License
️ and Rust