资源 · 77

API配额与速率限制

限制影响容量、重试及客户间分配。

已更新 · 13 min

存放档案和地图的无人情报档案室 由人工智能生成的编辑插图

本指南有助于实现的目标

  • 明确预算
  • 限制恢复过程
  • 处理不确定操作
  • 指定重试责任层
  • 停止已无用工作

快速检查

  • 记录配额范围、响应头、等待和重试预算;测试受控耗尽。
  • 同时重试可能加剧过载。

分步指南

  1. 1

    明确预算

    记录配额针对账户、密钥还是资源,以及客户端允许的并发操作数。即使平均频率不高,无限队列也可能压垮服务。

    记录配额单位、并发数和队列大小。

  2. 2

    限制恢复过程

    收到429时,若有Retry-After则解析并等待再请求。把逐步增加的等待和客户端间的时间分散,与最大尝试次数和总期限结合。

    保留尝试顺序及时间。

  3. 3

    处理不确定操作

    写入响应丢失时,先检查状态再重试。在模拟测试中组合限流、迟到响应和取消,确认队列结束且效果不重复。

    保留最终效果和待处理操作。

  4. 4

    指定重试责任层

    列出应用、SDK、代理及依赖的自动尝试。选择负责层,按契约关闭或协调额外重试。将首次尝试计入总预算。保留业务操作ID,重试非幂等操作前检查不确定效果。

    重试地图与每操作总预算。

  5. 5

    停止已无用工作

    根据客户端真实限制,定义覆盖等待、连接和处理的整体期限。入队前拒绝过期任务。本地取消不能证明服务器停止,应核对状态。测试逐步恢复流量,避免同时重启所有队列。

    传递期限与停止或核对证据。

可复现的验收案例

虚构示例:数据不代表客户或实际观察结果。

查看案例输入
{
    "http_status": 429,
    "retry_after_seconds": 30,
    "workers": 20,
    "retry_budget": 3,
    "retry_budget_scope": "per_worker_retries_excluding_initial_request",
    "scheduling_example": {
        "response_received_at_seconds": 0,
        "illustrative_retry_seconds": [
            30,
            31,
            32,
            33,
            34
        ],
        "workers_per_second": 4,
        "alternative_deadline_seconds": 25,
        "total_attempts_if_all_retries_used": 80
    }
}

预期判断

遵守30秒等待,错开20个工作进程的重试,并限制次数。

二十个客户端:错开并计算重试

虚构示例:20个工作进程在 +0 s 各收到429响应和 Retry-After: 30。时间表仅展示一种分布,不保证服务器容量。每个进程除初次请求外可重试3次。

策略计算或时间安排效果与限制
同时重试20 × 1 = 20 @ +30 s等待30秒遵守了延迟要求,但20个请求会一起返回,流量尖峰可能再次出现。
错开重试4 × 5 = 20 @ +30…+34 s每秒4个请求,持续5秒,将20次重试分散,但不减少总数。这个示例时间表不能替代带随机抖动的退避策略。
25秒后截止25 s < 30 s → 0剩余时间少于要求的30秒时,应按契约停止或延期,不要为了截止时间缩短等待。
最大预算20 × (1 + 3) = 80用完所有尝试时,初次20个请求加60次重试,共80个请求。上限不保证成功或重复操作安全。

待验证的虚构变体

Retry-After包含HTTP日期,而不是秒数。

按文档解释格式并核对时钟。记录算出的等待时间,不要把日期替换成随意设置的延迟。

每个工作进程都有自己的重试预算。

同时计算总负载。协调共享配额并分散重试,避免再次出现同步请求高峰。

产生业务影响的操作没有返回响应。

重试前核实权威状态和幂等约定。超时并不能证明操作没有执行。

应保留的证据

  • 收到的响应头、等待格式及使用的时钟。
  • 累计尝试预算和最长持续时间。
  • 操作标识符与已核对的结果。

补充参考 : IETF — RFC 9110, Retry-After [EN]

验证步骤

  1. 记录配额范围及重试预算。
  2. 仅在授权测试中耗尽配额。
  3. 保留429、Retry-After及受限时间线。 区分技术限流与业务结果,配额耗尽不应被转换为库存为零的响应。
  4. 在无外部负载模拟中,统计四层各三次总调用的尝试。
  5. 使虚构排队任务过期,确认不启动新操作。
  6. 模拟故障后逐步恢复授权工作,保留调用、期限与唯一业务结果。

AWS — Timeouts, retries, and backoff with jitter

API:防止多层重试相乘

应用、SDK、代理和服务都可能重试同一次调用。虚构的四层各执行三次总尝试,最多会向故障依赖发出81次调用。在既有配额管理上加入共享预算和有效截止时间。

虚构示例

示例情境

记录配额范围、响应头、等待和重试预算;测试受控耗尽。

决策及预期证据

不能。响应丢失的操作需要独立核对;其他尝试的响应不能证明其状态。

管理指标

指标衡量内容首要行动
每次操作的尝试数同一逻辑操作发出的尝试次数调整预算并终止循环
队列等待时间任务被接受后的等待时间取消或升级处理超期任务
调用放大依赖调用 / 同一窗口初始业务操作区分总尝试、重试和计划子调用
接受过期工作有效截止时间后开始的任务同时观察队列、取消和已开始效果

常见错误

  • 同时重试可能加剧过载。

常见问题解答

429能证明之前的写入没有执行吗?

不能。响应丢失的操作需要独立核对;其他尝试的响应不能证明其状态。

每层三次就是总计三次吗?

不是。虚构四层各三次总尝试会产生最多3⁴ = 81次最终调用。

任务过期能取消远端效果吗?

不一定。应阻止新的无用工作;已开始远端操作需检查状态并在可行时补偿。

Retry-After:等待、预算与重试决策

此语言版本的详细参考说明以英语提供。

Decide whether to retry after 429 or 503

A waiting period answers “when?”, not “may this operation be repeated?”. Before retrying, identify the intended effect, read the provider contract and bound total work. An error status or a lost response does not prove that no earlier attempt produced an effect.

Local teaching model with no request, timer, storage or real write. It considers one decision after 429 or 503 using six predefined headers. Dates are read at the fictional instant 22 October 2026, 10:00:00 UTC, assuming an accurate clock. This is neither a complete HTTP parser nor a Stripe client. Obsolete HTTP-date forms, clock skew, transport and SDK-specific policies are outside the calculation.

虚构的无人网络实验室,琥珀色灯光照亮深色工作台上的网关设备。 由人工智能生成的编辑插图
Generated, people-free conceptual network laboratory from the editorial collection. It depicts neither real infrastructure nor a throughput test.

Twelve ways to read Retry-After

Durations are seconds. Absolute dates need an explicit clock policy; calculations here assume the stated accurate clock. Past dates become zero in this model without permitting an immediate loop. Integer syntax alone does not define a client’s numeric limits.

Twelve ways to read Retry-After
Illustrative valueReading within this scopeInterpretation decision
30Non-negative decimal integer: wait 30 seconds after receipt.This means seconds, not 30 milliseconds.
0005The 1*DIGIT syntax accepts these digits: 5 seconds.Do not treat the leading zero as octal.
0A declared delay of zero seconds.Local policy, budgets and permission to repeat still require checking.
Thu, 22 Oct 2026 10:00:45 GMTAn HTTP date 45 seconds after the fictional receipt time.Clock uncertainty must not be silently treated as certainty.
Thu, 22 Oct 2026 09:59:50 GMTA date 10 seconds in the past; the model uses zero server wait.A past date requires neither negative waiting nor unlimited replay.
Field absentNo server wait is supplied.This model uses its selected local delay, not a normative default.
1.51.5 does not meet integer delay-seconds syntax.Do not silently round it and call the field valid.
-1-1 is not a non-negative decimal integer for this field.Do not interpret it as a special permission.
30sThe s unit suffix is not part of delay-seconds syntax.Seconds are already implied; 30s is not the form 30.
30, 60Two comma-separated durations are not one valid Retry-After value.Do not split every comma: a valid HTTP date itself contains one.
Empty valueThe field exists but contains neither expected form.Keep absent and malformed values distinct in records.
9999999999999999999999The digit string meets integer syntax but exceeds this model’s numeric bounds.Record the local limit without claiming the RFC sets it; never truncate to retry sooner.

Compare waiting, remaining budget and duplicate risk

Five parameters cover 144 combinations. Fictional policy: wait for the greater of an interpretable server delay and the already selected local delay, then reserve 10 seconds for one attempt. If the remaining budget is insufficient, stop this interactive retry without shortening the server wait. The reserve is not a measured duration and the selected local delay is not a jitter generator.

Teaching decision : Retry candidate

The selected operation type and budget allow considering a retry in this model. This proves neither success nor authorisation on a real service.

Interpretable server wait
30 s
Selected wait
30 s
Attempt reserve
10 s
Wait + reserve
40 s
Budget margin
20 s

Eight reproducible cases

Eight reproducible cases
Fictional situationRelevant parametersDecision and limit
429, read and 30-second wait429 · 30 seconds

Conforming GET read

Remaining budget after receipt : 60 s
Already selected local delay : 5 s

Retry candidate

With 60 seconds remaining, 30 + 10 leaves a 20-second margin.

Waiting exceeds the budget429 · 30 seconds

Conforming GET read

Remaining budget after receipt : 20 s
Already selected local delay : 5 s

Stop this retry

With 20 seconds remaining, do not shorten 30 just to force an attempt.

Write with unknown contract503 · Future date: +45 seconds

POST write: retry contract unverified

Remaining budget after receipt : 60 s
Already selected local delay : 5 s

Check business state

Even positive margin does not validate repeating POST.

Write with verified contract503 · Future date: +45 seconds

POST write: retry contract verified

Remaining budget after receipt : 60 s
Already selected local delay : 5 s

Retry candidate

Verified contract is an assumption, not a test of your provider.

Invalid fractional value503 · Invalid value: 1.5

Conforming GET read

Remaining budget after receipt : 20 s
Already selected local delay : 5 s

Retry candidate

The five-second fallback is a local teaching choice; retain the parse error.

Absent field, long local delay429 · Field absent

Conforming GET read

Remaining budget after receipt : 60 s
Already selected local delay : 40 s

Retry candidate

The 40-second delay comes from the client rather than the server.

HTTP date already passed503 · Past date: −10 seconds

Conforming GET read

Remaining budget after receipt : 20 s
Already selected local delay : 5 s

Retry candidate

The model retains a five-second local delay; the past date triggers no immediate call.

Zero does not remove the budget429 · 0 seconds

Conforming GET read

Remaining budget after receipt : 20 s
Already selected local delay : 40 s

Stop this retry

The 40-second local delay plus reserve exceed the 20 seconds remaining.

Five distinctions that change client behaviour

Five distinctions that change client behaviour
Reference pointDocumented meaningPractical limit
429RFC 6585 describes too many requests within a period. Retry-After may be present; a 429 response must not be stored by a cache.The RFC does not set the provider’s counted identity, window or algorithm.
503Retry-After can indicate the expected duration of service unavailability.A 5xx alone does not establish whether a write can be replayed.
HTTP idempotencePUT, DELETE and safe methods are idempotent in their intended effect.Responses and logs may differ. Controlled POST retries need an additional contract.
Retries at multiple layersClient, SDK and proxy attempts can multiply load.In a fictional three-layer example with three total attempts at each layer, one request can generate up to 3 × 3 × 3 = 27 calls at the last layer.
Jitter and budgetAWS describes spreading retries to avoid synchronised clients.Randomness makes no write idempotent and replaces neither a deadline nor an attempt limit.

Five items for retry acceptance evidence

  • Method, route, contract version and logical operation identity; keep secrets out of keys and logs.
  • Status, raw field, receipt time, interpretation and clock assumption.
  • Selected delay, current attempt, remaining budget and the layer actually controlling retries.
  • Known or unknown business state, retry key, associated context and parameters, and reconciliation evidence when needed.
  • Verified final outcome and user behaviour: understandable waiting, retained input and no button that unintentionally recreates an operation.

Compare idempotency contracts →

Primary sources and limits

The RFCs define the cited HTTP elements. AWS describes design choices and Stripe documents its own APIs. Budgets, local waits, cases and diagrams are educational: they neither measure your service nor execute requests.

References reviewed

GraphQL:错误、列表和部分数据

此语言版本的详细参考说明以英语提供。

GraphQL cost and partial results: two separate checks

An authorized query can still be too costly; a partial response measures none of its resource consumption.

  • Compare absent data, partial data and data: null.
  • Keep the original path even when a parent becomes null.
  • Test permissions, quotas and interface states separately.

Explore GraphQL responses and nullability →

GraphQL Specification · September 2025 · Published edition · September 2025

官方参考文献

参考文献支持该方法。 请根据您的实际情况调整检查;它们并非认证。 原始参考文献标题和源文档可能使用其他语言。

参考资料查阅日期 .