Files
nexus/wiki/concepts/Idempotency.md

30 lines
1017 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "Idempotency"
type: concept
tags:
- Architecture
- Reliability
- Cloud
last_updated: 2026-04-14
---
## Definition
幂等性Idempotency是指一个操作被执行一次和被执行多次产生的结果是相同的。在分布式系统和事件驱动架构中这是确保系统可靠性的关键设计原则。
## Why It Matters in EDA
Lambda 异步调用会自动重试(通常重试 2-3 次),因此服务在处理事件时必须考虑幂等性,避免因重复处理导致的数据不一致或副作用(如重复下单、重复扣款)。
## Implementation Strategies
- 为每个事件分配唯一标识符Event ID消费者维护已处理事件记录
- 使用数据库唯一约束或乐观锁防止重复写入
- 基于业务语义的去重(如订单号唯一性检查)
## Applicable Scenarios
- 订单处理和支付
- 库存扣减
- 消息确认
- 状态更新
## Sources
- [[public-cloud-learning-sessions-opentext-event-driven-architecture-part-2-2024091]]