Skip to content

[Performance] Add retry with exponential backoff for ARM API throttling #27

Description

@cocallaw

Summary

Invoke-AzPagedRequest has no retry logic. Any 429 (throttled) or transient 5xx response immediately terminates pagination with a partial result set via Write-Error.

Impact

Multi-subscription Advisor queries are exactly the workload pattern that can hit ARM throttling limits. Users with many subscriptions may get incomplete results without clear indication that throttling was the cause.

Location

  • Private/Invoke-AzPagedRequest.ps1 — paging loop catch block

Suggested fix

Add bounded retries with exponential backoff, honoring the Retry-After header when present:

$maxRetries = 3
$retryCount = 0

try {
    $response = Invoke-RestMethod -Uri $nextUri -Headers $Headers -Method Get -ErrorAction Stop
    $retryCount = 0  # Reset on success
}
catch {
    if ($retryCount -lt $maxRetries -and $_.Exception.Response.StatusCode -in @(429, 500, 502, 503, 504)) {
        $retryAfter = $_.Exception.Response.Headers["Retry-After"]
        $delay = if ($retryAfter) { [int]$retryAfter } else { [math]::Pow(2, $retryCount) }
        Write-Verbose "Throttled. Retrying in $delay seconds..."
        Start-Sleep -Seconds $delay
        $retryCount++
        continue
    }
    Write-Error "Azure API request failed for $nextUri: $_"
    return $results.ToArray()
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestperformancePerformance improvementpriority: mediumMedium priority — plan for near-term

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions