Skip to content

title: CloudSlash - AIMD Concurrency Reference description: Cloud APIs throttle without warning. Swarm learns the rate limit at runtime using AIMD: the same congestion control algorithm TCP uses. A parallel loop execu...


AIMD Concurrency

Cloud APIs throttle without warning. Swarm learns the rate limit at runtime using AIMD: the same congestion control algorithm TCP uses.


The Fixed-Thread Limitation

A parallel loop executing \(N\) goroutines throws \(N\) requests per second blindly. If the provider enforces a token bucket with a refill rate of \(R\) where \(N > R\), you get HTTP 429s. Keep going under throttle and you hit compounding exponential backoff: the ingestion loop stalls indefinitely.

Swarm introduces a state machine defining the current permissible request window (\(cwnd\)), adapting TCP congestion control for HTTPS API calls.

The AIMD Implementation

Swarm measures execution metrics continuously. On a successful HTTP 20X, the window (\(cwnd\)) scales linearly (Additive Increase). On HTTP 429, \(cwnd\) halves unconditionally (Multiplicative Decrease).

// pkg/engine/swarm/aimd.go
type AIMD struct {
    concurrency int
    minWorkers  int
    maxWorkers  int
    lastChange  time.Time
}

func (a *AIMD) Feedback(lat time.Duration, throttled bool) {
    // Multiplicative Decrease
    if throttled {
        a.concurrency = a.concurrency / 2
        if a.concurrency < a.minWorkers {
            a.concurrency = a.minWorkers
        }
        a.lastChange = time.Now()
        return
    }

    // Additive Increase (Dampened to 100ms vectors)
    if time.Since(a.lastChange) < 100*time.Millisecond {
        return
    }
    if lat < 100*time.Millisecond {
        a.concurrency += 5
    }
}

The asymmetry is intentional. Hitting an API wall requires an immediate cutoff: continued requests risk permanent IP blacklisting. Increasing throughput proceeds linearly to map the provider's token bucket ceiling without overshooting it.

Slow Start Mechanisms

During a destructive s.a.u saga (e.g., terminating 50 EC2 instances), the risk calculus changes. Swarm injects TCP Slow Start to probe the API envelope before bulk mutation.

  1. Swarm establishes \(cwnd = 1\).
  2. It processes 1 termination.
  3. If successful, \(cwnd = 2\).
  4. Next iteration processes 2 terminations concurrently.
  5. Growth stays exponential (\(cwnd = 2^x\)) until the window hits \(16\). At \(16\), it switches to standard Additive Increase.

If an HTTP error comes back at any phase, \(cwnd\) collapses immediately to \(1\). Failed intents go directly to the s.a.u CRDT ledger for compensatory rollback.