Microservices Communication Patterns (Saga, Circuit Breaker) · Lekcja

Idempotencja i deduplikacja wiadomości

Sprawią Państwo, że operacje mikrousług będą bezpieczne przy ponawianiu, projektując idempotentne handlery i usuwając duplikaty wiadomości w systemach rozproszonych.

Lekcja 4 z 413 kroki

Idempotencja i deduplikacja wiadomości to bezpłatna lekcja Microservices Communication Patterns (Saga, Circuit Breaker) na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Microservices Communication Patterns (Saga, Circuit Breaker), a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Microservices Communication Patterns (Saga, Circuit Breaker) zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.

Bezpłatny start

Ucz się Microservices Communication Patterns (Saga, Circuit Breaker) dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
12
Lekcje
48

Często zadawane pytania

Czy lekcja „Idempotencja i deduplikacja wiadomości” jest bezpłatna?

Tak — pełny tekst „Idempotencja i deduplikacja wiadomości” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Microservices Communication Patterns (Saga, Circuit Breaker), przejdź na CoddyKit PRO. Kurs Microservices Communication Patterns (Saga, Circuit Breaker) zawiera 4 lekcji w sumie.

Co nauczysz się w „Idempotencja i deduplikacja wiadomości”?

Sprawią Państwo, że operacje mikrousług będą bezpieczne przy ponawianiu, projektując idempotentne handlery i usuwając duplikaty wiadomości w systemach rozproszonych. Ćwiczysz Microservices Communication Patterns (Saga, Circuit Breaker) z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Microservices Communication Patterns (Saga, Circuit Breaker)?

Nie wymagamy żadnego doświadczenia. Microservices Communication Patterns (Saga, Circuit Breaker) w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Idempotencja i deduplikacja wiadomości”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Microservices Communication Patterns (Saga, Circuit Breaker)?

Tak. Każda lekcja Microservices Communication Patterns (Saga, Circuit Breaker) zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Komunikacja synchroniczna a asynchroniczna
  2. Wyzwania systemów rozproszonych
  3. API Gateway i Service Discovery
  4. Idempotencja i deduplikacja wiadomości
← Powrót do Microservices Communication Patterns (Saga, Circuit Breaker)