Distributed patterns: recap and quiz
The module in four sentences
Section titled “The module in four sentences”- Sagas. A business transaction across services is a sequence of local transactions, each with a compensation, run by an orchestrator or by events; a compensation is a new action, it must be idempotent and retried, and the steps have no isolation from each other.
- The transactional outbox. A service writes the event to a table in the same transaction as its data, and a relay publishes it, so the event exists if and only if the data change committed, and is published at least once.
- The inbox. A consumer records each message id in the same transaction as its effect, so a repeat changes nothing; exactly-once is at-least-once delivery plus idempotent processing.
- The strangler fig. A routing facade lets a legacy system be replaced one path at a time, with the same address for clients and a routing change as the way back.
The circuit breaker and the bulkhead, which protect a saga from a failing provider, are in the reliability module. CQRS and event sourcing, which are other ways to keep state across services, are in the architecture patterns module.
How the first three fit together
Section titled “How the first three fit together”A checkout across services uses all three. The order service starts the saga. Each step is a local transaction in a service, and the message to the next step is written to that service’s outbox in the same transaction, so a step cannot commit without announcing itself or announce without committing. A relay publishes the message at least once. The service that receives it keeps an inbox, so a repeated command, or a repeated compensation, changes nothing. The saga supplies the what to do when a step fails, the outbox supplies the message is never lost, and the inbox supplies the message is never applied twice. Take away any one and the checkout can lose an order, or charge a customer twice.
How each one breaks
Section titled “How each one breaks”| Pattern | What breaks | What you see | What helps |
|---|---|---|---|
| Saga | A compensation that fails, or a crash with no log | An order half-undone: money taken, stock held | Idempotent, retried compensations, a durable saga log, an alert for a person |
| Saga | No isolation between steps | A customer told “out of stock” for units that are released a moment later | A pending state or semantic lock, a sensible order of the steps |
| Outbox | A relay that is down or slow | Events late, rows piling up | Monitor the age of the oldest unsent row |
| Outbox | A crash between publish and mark | The same event published twice | An idempotent consumer |
| Inbox | The id saved apart from the effect | An effect lost for ever on the retry | One transaction for both |
| Inbox | An id deleted too early | A late duplicate applied again | A retention longer than the longest redelivery |
| Strangler fig | Data both systems write | Values that differ between the systems | One owner per table at each step |
| Strangler fig | A facade that fails or slows | Every request affected | Keep it simple and redundant |
A checklist for a change across services
Section titled “A checklist for a change across services”- Does the change really need to span services? The cheapest saga is the one you avoid by drawing the boundary so that data which must be consistent lives together. (Sagas)
- For each step, what is its compensation, and what if it can never be undone? Put irreversible steps after the steps that can still fail. (Sagas)
- Is every command and compensation idempotent, with a key? (Sagas)
- Where is the saga’s state, and what happens when the orchestrator restarts? (Sagas)
- What can another request see between the steps? Decide on a pending state or a lock. (Sagas)
- Is every event written in the same transaction as the data that caused it? (The outbox)
- Who publishes the outbox, how often, and who notices when it stops? (The outbox)
- Does every consumer record the message id in the same transaction as its effect, and for how long? (The inbox)
- Does anything depend on message order, and what enforces it? The inbox does not. (The inbox)
- For a migration, which path moves first, who owns each table, and how do you go back? (The strangler fig)
Sort the problems
Section titled “Sort the problems”Four patterns, twelve problems. Put each problem under the pattern that fixes it.
Challenge
Saga, outbox, inbox or strangler fig?
Four patterns each answer one problem. Put each problem under the pattern that fixes it.
Sorting needs JavaScript.
Categories
- Saga
- Local transactions in several services, with compensations when a step fails.
- Outbox
- Write the event in the same transaction as the data, and publish it afterwards.
- Inbox
- Record the message id with the effect, so a repeat changes nothing.
- Strangler fig
- Move a legacy system piece by piece behind a routing facade.
Check your answers at least once before looking at the answers.
Check your understanding
Section titled “Check your understanding”Nine questions cover the whole module. Every option is explained, whether it is right or wrong.
Distributed patterns: module quiz
This quiz needs JavaScript to check your answers.
Interview practice
Section titled “Interview practice”Write your answer before you open the model answer.
Interview question
Writing and reviewing an answer needs JavaScript.
The interviewer asks
A checkout reserves stock, charges a card and creates a shipment. Each step belongs to a different service with its own database. How do you keep the order consistent when the shipment is refused after the card was charged?
Assume 200 orders per second at the peak, a payment provider that is sometimes unavailable for a few seconds, and a message broker that delivers at least once.
Write your answer first. The strong answer unlocks once you have written a real attempt.
0 of 80 characters written to unlock the strong answer
Interview question
Writing and reviewing an answer needs JavaScript.
The interviewer asks
A ten-year-old monolith serves your whole product. The team wants to move to services. How do you migrate without stopping feature work or taking a big-bang risk?
Assume a single HTTP entry point, one shared database, and a product that has to stay up throughout.
Write your answer first. The strong answer unlocks once you have written a real attempt.
0 of 80 characters written to unlock the strong answer
Where this goes next
Section titled “Where this goes next”This module closes the part on architecture. The next module, the case studies, applies everything in the course to complete systems, and several of them lean on these patterns: the e-commerce case study has a checkout saga, and the payment system case study is about idempotency and reconciliation. Ask the questions of this module of each of them: what is the unit that must be atomic, what keeps a message from being lost, and what keeps a repeat from doing harm.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.