Il pattern Saga per le transazioni distribuite
Impari a coordinare le modifiche ai dati tra più servizi senza transazioni distribuite, usando il pattern Saga e azioni compensative.
Il pattern Saga per le transazioni distribuite è una lezione Serverless AWS Lambda Development 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 Serverless AWS Lambda Development, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Serverless AWS Lambda Development include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Domande Frequenti
La lezione «Il pattern Saga per le transazioni distribuite» è gratuita?
Sì — il testo completo di «Il pattern Saga per le transazioni distribuite» è 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 Serverless AWS Lambda Development, passa a CoddyKit PRO. Il corso Serverless AWS Lambda Development include 4 lezioni in totale.
Cosa imparerò in «Il pattern Saga per le transazioni distribuite»?
Impari a coordinare le modifiche ai dati tra più servizi senza transazioni distribuite, usando il pattern Saga e azioni compensative. Eserciti Serverless AWS Lambda Development 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 Serverless AWS Lambda Development?
Non è richiesta alcuna esperienza precedente. Serverless AWS Lambda Development 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 «Il pattern Saga per le transazioni distribuite»?
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 Serverless AWS Lambda Development?
Sì. Ogni lezione Serverless AWS Lambda Development 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
- Creazione di microservizi basati su eventi
- Integrazione con Amazon EventBridge
- Elaborazione in tempo reale con Kinesis
- Il pattern Saga per le transazioni distribuite