0Pricing
Microservices Communication Patterns (Saga, Circuit Breaker) · 课时

幂等性与消息去重

通过设计幂等处理器并在分布式系统中对消息去重,让微服务操作可以安全重试。

幂等性与消息去重 是 CoddyKit 上的免费 Microservices Communication Patterns (Saga, Circuit Breaker) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Microservices Communication Patterns (Saga, Circuit Breaker) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Microservices Communication Patterns (Saga, Circuit Breaker) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Retries Cause Duplicates

Distributed systems deliver messages at least once. A timeout or retry can make the same request arrive twice — and charge a card or create an order twice.

What Idempotency Means

An operation is idempotent if doing it many times has the same effect as doing it once. Repeating it changes nothing beyond the first application.

Naturally Idempotent Operations

Some operations are idempotent by nature — absolute SET, HTTP PUT and DELETE. Others, like balance = balance + 10, double-apply on a retry.

The Idempotency Key

The standard fix is an idempotency key: the client generates a unique id per operation and resends it on every retry, so the server processes it only once.

POST /payments
Idempotency-Key: 9f1c-7a22-...
{ "amount": 4999 }

Storing Processed Keys

The server records each processed key. If the same key reappears, it returns the stored result instead of re-running the work.

if (store.exists(key)) return store.result(key);
Result r = process(request);
store.save(key, r);
return r;

Deduplication at the Consumer

For queues, the consumer does deduplication: it tracks message ids it has already handled and silently skips any repeats.

if (seen.contains(msg.id())) { ack(msg); return; }
handle(msg);
seen.add(msg.id());
ack(msg);

Atomicity Matters

Recording the key and doing the work must be atomic. If they are separate, a crash between them reopens the duplicate window — use a transaction or unique constraint.

INSERT INTO processed(key) VALUES (?) -- unique constraint
-- if it fails, the work already ran

Expiring the Dedup Store

You cannot store keys forever. Give the dedup store a TTL sized to your max retry window — Redis with expiry is a common choice.

redis.set("idem:" + key, result, "EX", 86400, "NX");

Idempotency vs Exactly-Once

True exactly-once delivery is extremely hard. The practical pattern is at-least-once delivery plus idempotent processing, giving effectively-once behavior.

Designing Idempotent Events

Make event handlers idempotent: include a unique event id, prefer absolute state changes, and guard side effects so a redelivered event never repeats them.

Where to Apply It

Apply idempotency at every retry boundary — API gateways, queue consumers, saga steps, webhooks. Anywhere a message can arrive twice needs a dedup strategy.

Quick Check

The same message just arrived twice — does your design still do the right thing? Check your idempotency grasp.

Recap

You made operations retry-safe: at-least-once causes duplicates, idempotent operations repeat harmlessly, use keys and dedup, keep record-and-process atomic with a TTL.

常见问题解答

「幂等性与消息去重」课时是免费的吗?

是的 — 「幂等性与消息去重」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Microservices Communication Patterns (Saga, Circuit Breaker) 课程的其余内容,请升级到 CoddyKit PRO。 Microservices Communication Patterns (Saga, Circuit Breaker) 课程共包含 4 节课。

「幂等性与消息去重」这节课中我会学到什么?

通过设计幂等处理器并在分布式系统中对消息去重,让微服务操作可以安全重试。 你通过在浏览器中直接运行的动手代码来练习 Microservices Communication Patterns (Saga, Circuit Breaker),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Microservices Communication Patterns (Saga, Circuit Breaker) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Microservices Communication Patterns (Saga, Circuit Breaker) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「幂等性与消息去重」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Microservices Communication Patterns (Saga, Circuit Breaker) 课中编写并运行代码吗?

能。每节 Microservices Communication Patterns (Saga, Circuit Breaker) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 同步通信与异步通信
  2. 分布式系统中的挑战
  3. API 网关与服务发现
  4. 幂等性与消息去重
← 返回 Microservices Communication Patterns (Saga, Circuit Breaker)