Idempotenza e deduplicazione dei messaggi
Renda sicure le operazioni dei microservizi in caso di retry progettando handler idempotenti e deduplicando i messaggi nei sistemi distribuiti.
Idempotenza e deduplicazione dei messaggi è una lezione Microservices Communication Patterns (Saga, Circuit Breaker) gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Microservices Communication Patterns (Saga, Circuit Breaker), e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Microservices Communication Patterns (Saga, Circuit Breaker) include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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 ranExpiring 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.
Impara Microservices Communication Patterns (Saga, Circuit Breaker) con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 48
Domande Frequenti
La lezione «Idempotenza e deduplicazione dei messaggi» è gratuita?
Sì — il testo completo di «Idempotenza e deduplicazione dei messaggi» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Microservices Communication Patterns (Saga, Circuit Breaker), passa a CoddyKit PRO. Il corso Microservices Communication Patterns (Saga, Circuit Breaker) include 4 lezioni in totale.
Cosa imparerò in «Idempotenza e deduplicazione dei messaggi»?
Renda sicure le operazioni dei microservizi in caso di retry progettando handler idempotenti e deduplicando i messaggi nei sistemi distribuiti. Eserciti Microservices Communication Patterns (Saga, Circuit Breaker) con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Microservices Communication Patterns (Saga, Circuit Breaker)?
Non è richiesta alcuna esperienza precedente. Microservices Communication Patterns (Saga, Circuit Breaker) su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Idempotenza e deduplicazione dei messaggi»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Microservices Communication Patterns (Saga, Circuit Breaker)?
Sì. Ogni lezione Microservices Communication Patterns (Saga, Circuit Breaker) include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Comunicazione sincrona vs. asincrona
- Sfide dei sistemi distribuiti
- API Gateway e Service Discovery
- Idempotenza e deduplicazione dei messaggi