幂等性 ·
三次重试应得到一个结果,而不是三次副作用Idempotency · Three Retries Should Produce One Outcome, Not Three Side
Effects
same operation + same idempotency key → one durable order · missing
key → duplicates
幂等性(Idempotency)是同一操作重复执行一次或多次,对最终状态产生等同效果的性质。没有幂等设计的重试可能重复扣款、发信或写入。Idempotency means repeating the same operation produces the same final
state as doing it once. Retries without idempotent design can
duplicate charges, messages, or writes.
01 · 电梯按钮按三次,会来三部电梯吗01 · Press an elevator button three times—should three cars
arrive?
网络卡顿时用户连点三次“创建订单”。系统怎样知道这是一个意图的重试,而不是三笔新生意?A network delay makes someone click “Create order” three times.
How does the system know these are retries of one intent rather
than three purchases?
×3→1
02连续提交三次,对比有幂等键的一条订单与无键的三条副作用Submit Three Times and Compare One Keyed Order with Three Unkeyed
Side Effects
重复请求 · 单一结果repeated requests · one outcome
状态 AMetric A
—
—
状态 BMetric B
—
—
状态 CMetric C
—
—
所有概率、成本、成功率与输出均为固定种子的教学模拟,不代表真实模型输出。把主变量拖回即可复核结论。All probabilities, costs, success rates, and outputs are
fixed-seed teaching simulations, not real model output. Move the
main variable back to verify the conclusion.
03把刚才的变化拆成 4 个零件Break the Change into Four Parts
Idempotency
INTENT
业务意图Business intent
一次用户意图生成一个稳定键。One user intent gets one stable key.
KEY
幂等键Idempotency key
相同键把重复请求映射到同一结果。The same key maps repeated requests to one result.
STORE
结果记录Result record
首次成功后保存键与订单 ID 的映射。After first success, persist the key-to-order mapping.
REPLAY
安全重放Safe replay
后续请求返回既有结果,不再触发副作用。Later requests return the existing result without another side
effect.
04最小机制:主交互能逐项验证Minimal Mechanism: Verify Each Link in the Interaction
MECHANISM
result = store.get(key) ?? execute_once_and_store(key)
有键时,第二与第三次提交命中同一
ORDER-001;关闭键后,每次请求都被当成新意图并新增订单。With a key, attempts two and three return ORDER-001. Without a
key, every request is treated as a new intent and creates
another order.
生成稳定键Create key绑定一次意图bind one intent
首次执行Execute once保存键→结果store key→result
重复命中Replay返回旧结果,无副作用return result, no side effect
05关闭幂等键后快速提交三次Disable the Key and Submit Three Times Quickly
主动制造失败Create a failure
边界实验Boundary experiment
重试机制本身不会识别重复意图。红色计数会显示额外订单、扣款与通知各发生两次。Retry logic does not identify repeated intent by itself. Red
counters show two extra orders, charges, and
notifications.
尚未执行。先在主交互中观察正常机制。Not run yet. Observe the normal mechanism in the main interaction
first.
✗ 同一个按钮就是同一个请求。The same button means the same request.→ ✓ 服务端需要稳定幂等键识别同一业务意图。The server needs a stable idempotency key for the same
intent.
✗ 只要重试次数有限就安全。Limited retries are safe.→
✓ 一次重复扣款也不可接受;副作用需要幂等保护。Even one duplicate charge is unacceptable; side effects need
idempotency.
✗ HTTP 成功就能丢掉幂等记录。Discard the key record after HTTP success.→ ✓ 记录要覆盖客户端可能重试的有效窗口。Keep the record through the client's retry
window.
06 · 一句话带走06 · One line to keep
幂等键把同一业务意图的重复请求映射到一个已保存结果;没有键,三次提交就可能制造三份真实副作用。An idempotency key maps repeated requests for one business intent
to one stored result. Without it, three submissions can create
three real side effects.