Run It Twice, Charge Once: Idempotency Keys in n8n
说明与用途
在 n8n 工作流中实现幂等键可防止在重试过程中出现重复操作,例如双重扣费和消息发送。
问题
A workflow retried after a timeout, causing duplicate side effects such as double charging, double messaging, and duplicate record updates, while logs only showed success.
结果
Implementing idempotency keys in n8n workflows eliminates duplicate side effects caused by retries, ensuring that each operation is performed only once even if the workflow is retried or replayed.
工作流
- Generate a stable idempotency key from job ID and operation name (e.g., "job-4821:charge").
- Before performing a side effect, query a durable shared store for the key.
- If a result exists, return it and skip the side effect.
- If no result, perform the side effect, then atomically write the result to the store under the key in the same step.
- Ensure the store is durable, shared, and visible to all retry paths (ordinary retry, DLQ replay, checkpoint resume).
- Apply a retention policy for keys that covers the longest retry/replay window.
- When a retry returns a stored result, record the outcome as a duplicate skip in the job record for visibility.
证据
The customer was charged twice, messaged twice, and every log said "success," because each execution had, in isolation, succeeded.
The fix is one extra field, carried through the whole workflow: an idempotency key.
If a completed result exists for the key, return that result and skip the side effect entirely.
The store does not need to be fancy. A table with the key, the operation, the result, and a timestamp is enough.
- 记录日期: 2026-11-01T20:44:10.009Z