0Pricing
Serverless AWS Lambda Development · Lekcja

Wzorzec Saga dla transakcji rozproszonych

Dowiedz się, jak koordynować zmiany danych w wielu usługach bez transakcji rozproszonych, korzystając ze wzorca saga i działań kompensacyjnych.

Wzorzec Saga dla transakcji rozproszonych to bezpłatna lekcja Serverless AWS Lambda Development 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 Serverless AWS Lambda Development, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Serverless AWS Lambda Development zawiera 4 lekcji w sumie.

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

The Distributed Data Problem

In event-driven systems each service owns its own data. A single business operation may span several services, but there is no shared transaction to roll them all back.

What is a Saga?

A saga breaks one big transaction into a sequence of local transactions. If a step fails, earlier steps are undone with compensating actions.

A Worked Example

Placing an order: reserve inventory, charge payment, create shipment. If payment fails, you must release the reserved inventory.

Compensating Actions

Every forward step needs a matching undo. Reserve has Release; Charge has Refund. Compensations make the system eventually consistent.

steps = [
  ('reserveInventory', 'releaseInventory'),
  ('chargePayment', 'refundPayment'),
  ('createShipment', 'cancelShipment')
]

Choreography Style

In choreography, each service reacts to events and emits the next event. There is no central coordinator, but the flow is implicit and harder to follow.

Orchestration Style

In orchestration, a coordinator (often AWS Step Functions) explicitly drives each step and triggers compensations on failure. The flow is centralized and visible.

Step Functions Fits Sagas

Step Functions supports a Catch on each task to invoke a compensation, making it a natural home for orchestrated sagas.

{
  "ChargePayment": {
    "Type": "Task",
    "Resource": "arn:...:chargePayment",
    "Catch": [{"ErrorEquals": ["States.ALL"], "Next": "ReleaseInventory"}],
    "Next": "CreateShipment"
  }
}

Idempotency is Essential

Saga steps and compensations may be retried, so each must be idempotent. Use a unique transaction id to detect and ignore duplicates.

Eventual Consistency

During a saga the system is temporarily inconsistent. Design the UI and downstream consumers to tolerate states like "payment pending".

Semantic Locks

To avoid conflicts mid-saga, mark records with a pending status (a semantic lock) so other operations know the data is in flight.

When to Use a Saga

Use sagas when a workflow truly spans services. If everything lives in one service or one database, a normal local transaction is simpler and safer.

Quick Check

Test your saga knowledge.

Recap

You learned the saga pattern: split a distributed operation into local steps with compensating undos, choose choreography or orchestration, and keep steps idempotent.

Często zadawane pytania

Czy lekcja „Wzorzec Saga dla transakcji rozproszonych” jest bezpłatna?

Tak — pełny tekst „Wzorzec Saga dla transakcji rozproszonych” 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 Serverless AWS Lambda Development, przejdź na CoddyKit PRO. Kurs Serverless AWS Lambda Development zawiera 4 lekcji w sumie.

Co nauczysz się w „Wzorzec Saga dla transakcji rozproszonych”?

Dowiedz się, jak koordynować zmiany danych w wielu usługach bez transakcji rozproszonych, korzystając ze wzorca saga i działań kompensacyjnych. Ćwiczysz Serverless AWS Lambda Development 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ąć Serverless AWS Lambda Development?

Nie wymagamy żadnego doświadczenia. Serverless AWS Lambda Development 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 „Wzorzec Saga dla transakcji rozproszonych”?

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 Serverless AWS Lambda Development?

Tak. Każda lekcja Serverless AWS Lambda Development 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. Tworzenie mikrousług sterowanych zdarzeniami
  2. Integracja z Amazon EventBridge
  3. Przetwarzanie w czasie rzeczywistym za pomocą Kinesis
  4. Wzorzec Saga dla transakcji rozproszonych
← Powrót do Serverless AWS Lambda Development