<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Cleiton Augusto </title>
    <description>The latest articles on Forem by Cleiton Augusto  (@cleiton_augusto_).</description>
    <link>https://forem.com/cleiton_augusto_</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3547488%2Ffea3af52-a724-429f-af20-3a2121bd4a27.jpg</url>
      <title>Forem: Cleiton Augusto </title>
      <link>https://forem.com/cleiton_augusto_</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/cleiton_augusto_"/>
    <language>en</language>
    <item>
      <title>Implementing Adaptive Backpressure in Rust with FlowGuard</title>
      <dc:creator>Cleiton Augusto </dc:creator>
      <pubDate>Sat, 27 Dec 2025 15:43:34 +0000</pubDate>
      <link>https://forem.com/cleiton_augusto_/implementing-adaptive-backpressure-in-rust-with-flowguard-1iof</link>
      <guid>https://forem.com/cleiton_augusto_/implementing-adaptive-backpressure-in-rust-with-flowguard-1iof</guid>
      <description>&lt;p&gt;Implementing Adaptive Backpressure in Rust with FlowGuard&lt;br&gt;
Hey fellow Rustaceans! 👋&lt;/p&gt;

&lt;p&gt;I recently open-sourced FlowGuard, a library for adaptive concurrency control and backpressure in Rust services. In this post, I'll share why static rate limiting fails and how FlowGuard solves it with TCP Vegas congestion control.&lt;/p&gt;

&lt;p&gt;🤔 The Problem with Static Limits&lt;br&gt;
We've all done this:&lt;/p&gt;

&lt;p&gt;rust&lt;br&gt;
// "Maximum 100 concurrent connections"&lt;br&gt;
let max_connections = 100;&lt;br&gt;
But static limits are a trap:&lt;/p&gt;

&lt;p&gt;Set too high? Your system crashes before reaching the limit&lt;/p&gt;

&lt;p&gt;Set too low? You waste resources and refuse legitimate traffic&lt;/p&gt;

&lt;p&gt;Guessing game? You're always tuning based on hunches&lt;/p&gt;

&lt;p&gt;🚀 The Solution: Dynamic Backpressure&lt;br&gt;
Instead of guessing, what if your system could self-adjust based on real-time performance? That's where FlowGuard comes in.&lt;/p&gt;

&lt;p&gt;Introducing FlowGuard&lt;br&gt;
FlowGuard implements the TCP Vegas congestion control algorithm to dynamically adjust concurrency limits based on actual system latency.&lt;/p&gt;

&lt;p&gt;🎯 How It Works&lt;br&gt;
rust&lt;br&gt;
use flow_guard::{FlowGuard, VegasStrategy};&lt;br&gt;
use std::sync::Arc;&lt;/p&gt;

&lt;h1&gt;
  
  
  [tokio::main]
&lt;/h1&gt;

&lt;p&gt;async fn main() {&lt;br&gt;
    // Start with 10 concurrent operations&lt;br&gt;
    let strategy = Arc::new(VegasStrategy::new(10));&lt;br&gt;
    let guard = FlowGuard::new(Arc::clone(&amp;amp;strategy));&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;println!("Initial limit: {}", guard.current_limit());

// Execute tasks with adaptive backpressure
let result = guard.run(async {
    // Your database query, API call, etc.
    tokio::time::sleep(std::time::Duration::from_millis(100)).await;
    Ok::&amp;lt;_, &amp;amp;str&amp;gt;("Success!")
}).await;

println!("Final limit: {}", guard.current_limit()); // Adjusted!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
✨ Key Features&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Real-time Adjustment
rust
// Watch limits adjust dynamically
println!("Current limit: {}", guard.current_limit());
println!("Available permits: {}", guard.available_permits());&lt;/li&gt;
&lt;li&gt;Vegas Algorithm
Based on the difference between expected and actual throughput:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;✅ Increases limit when system has spare capacity&lt;/p&gt;

&lt;p&gt;✅ Decreases limit when latency indicates congestion&lt;/p&gt;

&lt;p&gt;✅ Self-tuning - no manual configuration needed&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Web Framework Integration
rust
// Axum 0.8 middleware
let strategy = VegasStrategy::new(50);
let flow_layer = FlowGuardLayer::new(strategy);&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;let app = Router::new()&lt;br&gt;
    .route("/api/data", get(handler))&lt;br&gt;
    .layer(flow_layer);&lt;br&gt;
📦 Getting Started&lt;br&gt;
Add to your Cargo.toml:&lt;/p&gt;

&lt;p&gt;toml&lt;br&gt;
[dependencies]&lt;br&gt;
flow-guard = "0.2.1"&lt;/p&gt;

&lt;h1&gt;
  
  
  With Axum/Tower support
&lt;/h1&gt;

&lt;p&gt;flow-guard = { version = "0.2.1", features = ["axum", "tower"] }&lt;br&gt;
🔧 Under the Hood&lt;br&gt;
FlowGuard replaces tokio::sync::Semaphore with a custom DynamicSemaphore that can adjust its limit up and down in real-time:&lt;/p&gt;

&lt;p&gt;rust&lt;br&gt;
pub struct DynamicSemaphore {&lt;br&gt;
    max_permits: AtomicUsize,&lt;br&gt;
    available_permits: AtomicUsize,&lt;br&gt;
    notify: Notify,&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;impl DynamicSemaphore {&lt;br&gt;
    pub fn set_limit(&amp;amp;self, new_limit: usize) {&lt;br&gt;
        // Adjusts permits dynamically based on Vegas calculations&lt;br&gt;
    }&lt;br&gt;
}&lt;br&gt;
🎯 Use Cases&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Database Protection
rust
// Prevent database overload
let db_guard = FlowGuard::new(VegasStrategy::new(20));&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;async fn query_database() -&amp;gt; Result {&lt;br&gt;
    db_guard.run(|| async {&lt;br&gt;
        // Your database query here&lt;br&gt;
        database.query("SELECT * FROM users").await&lt;br&gt;
    }).await``&lt;br&gt;
}&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;API Rate Limiting
rust
// Adaptive rate limiting for external APIs
let api_guard = FlowGuard::new(VegasStrategy::new(5));&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;async fn call_external_api() -&amp;gt; Result {&lt;br&gt;
    api_guard.run(|| async {&lt;br&gt;
        client.get("&lt;a href="https://api.example.com/data%22).await" rel="noopener noreferrer"&gt;https://api.example.com/data").await&lt;/a&gt;&lt;br&gt;
    }).await&lt;br&gt;
}&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Microservices
rust
// Protect services from cascading failures
let service_guard = FlowGuard::new(VegasStrategy::new(100));
📊 Benchmarks
In testing, FlowGuard showed:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;5 → 12 limit adjustment under optimal conditions&lt;/p&gt;

&lt;p&gt;Sub-millisecond overhead per request&lt;/p&gt;

&lt;p&gt;Zero allocation in hot path&lt;/p&gt;

&lt;p&gt;Thread-safe with atomic operations&lt;/p&gt;

&lt;p&gt;🚀 Try It Yourself&lt;br&gt;
bash&lt;/p&gt;

&lt;h1&gt;
  
  
  Clone and run examples
&lt;/h1&gt;

&lt;p&gt;git clone &lt;a href="https://github.com/cleitonaugusto/flow-guard" rel="noopener noreferrer"&gt;https://github.com/cleitonaugusto/flow-guard&lt;/a&gt;&lt;br&gt;
cd flow-guard&lt;br&gt;
cargo run --example basic_usage&lt;br&gt;
🔗 Resources&lt;br&gt;
GitHub: &lt;a href="https://github.com/cleitonaugusto/flow-guard" rel="noopener noreferrer"&gt;https://github.com/cleitonaugusto/flow-guard&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Crates.io: &lt;a href="https://crates.io/crates/flow-guard" rel="noopener noreferrer"&gt;https://crates.io/crates/flow-guard&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Documentation: &lt;a href="https://docs.rs/flow-guard/0.2.1/" rel="noopener noreferrer"&gt;https://docs.rs/flow-guard/0.2.1/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Examples: basic_usage.rs, server_demo.rs&lt;/p&gt;

&lt;p&gt;💭 Why I Built This&lt;br&gt;
After seeing too many services crash from static limits or waste resources with conservative settings, I wanted a solution that adapts to actual system performance. The TCP Vegas algorithm has been battle-tested for decades in networking - why not apply it to service concurrency?&lt;/p&gt;

&lt;p&gt;🤝 Contributing &amp;amp; Feedback&lt;br&gt;
FlowGuard is open source under MIT license. I'd love your:&lt;/p&gt;

&lt;p&gt;Feedback on the API design&lt;/p&gt;

&lt;p&gt;Use cases from your projects&lt;/p&gt;

&lt;p&gt;Contributions to the codebase&lt;/p&gt;

&lt;p&gt;Ideas for improvements&lt;/p&gt;

&lt;p&gt;What adaptive concurrency patterns have you used in your Rust projects? Share in the comments!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>webdev</category>
      <category>rust</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
