Skip to content

Distributed patterns: recap and quiz

  1. 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.
  2. 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.
  3. 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.
  4. 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.

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.

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
  1. 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)
  2. 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)
  3. Is every command and compensation idempotent, with a key? (Sagas)
  4. Where is the saga’s state, and what happens when the orchestrator restarts? (Sagas)
  5. What can another request see between the steps? Decide on a pending state or a lock. (Sagas)
  6. Is every event written in the same transaction as the data that caused it? (The outbox)
  7. Who publishes the outbox, how often, and who notices when it stops? (The outbox)
  8. Does every consumer record the message id in the same transaction as its effect, and for how long? (The inbox)
  9. Does anything depend on message order, and what enforces it? The inbox does not. (The inbox)
  10. For a migration, which path moves first, who owns each table, and how do you go back? (The strangler fig)

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.
  1. Line 1 of 12The payment went through, then shipping refused the address. The customer must get the money back and the stock must go on sale again.
  2. Line 2 of 12One checkout must change the order, stock and payment databases, which belong to three services, and no transaction can span them.
  3. Line 3 of 12A refund must be retried until it works, and an orchestrator must remember how far each order got.
  4. Line 4 of 12The service saved the order and crashed before it published OrderPlaced, so no other service ever hears of it.
  5. Line 5 of 12An event was published, and then the order transaction rolled back: other services act on an order that does not exist.
  6. Line 6 of 12A service must update its database and publish to a broker as one step, and two-phase commit between them is not available.
  7. Line 7 of 12The broker redelivered "reserve 2 units" after a lost acknowledgement, and the stock fell by 4.
  8. Line 8 of 12The relay crashed after publishing and published the same row again after its restart.
  9. Line 9 of 12A unique key on the message id makes the second insert fail, the transaction is rolled back, and the message is acknowledged without effect.
  10. Line 10 of 12A ten-year-old billing system must be replaced without freezing features for two years.
  11. Line 11 of 12Clients keep calling the same address while /invoices moves to a new service and /orders stays where it is.
  12. Line 12 of 12A rewrite from scratch would take two years to specify behaviour that nobody has fully written down.

Check your answers at least once before looking at the answers.

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.

Question 1 of 9

An order spans the order, stock, payment and shipping services, each with its own database. One team proposes a distributed transaction with two-phase commit.

Why is a saga the usual choice instead?

Choose one answer.

Question 2 of 9

In the orchestration flow, the orchestrator crashes after the refund and before releasing the stock. It takes 5 s to restart and then reads its saga log. The round trip to each service is 20 ms and each service takes 20 ms to handle a command. The shipment failed, so the saga compensates.

How long after the start until the order is cancelled?

Enter a number and choose its unit.

Question 3 of 9

A saga has seven steps, conditional branches (digital goods skip shipping), and the steps belong to four teams. Operations wants to see where every order is.

Which style fits better?

Choose one answer.

Question 4 of 9

An order saga has four steps: reserve stock, charge payment, email the customer a confirmation, create the shipment.

Which step is the hardest to compensate, and so should come after the steps that can fail?

Choose one answer.

Question 5 of 9

A service must update its own database and tell other services with an event. It currently saves the row and then publishes, and once in a while an event is missing.

What does the outbox change?

Choose one answer.

Question 6 of 9

An account holds 100. A message "debit 30" is delivered, the consumer commits and crashes before acknowledging, and the message is delivered again. The consumer has no inbox.

What is the balance?

Enter a number and choose its unit.

currency units
Question 7 of 9

The inbox table grows by one row per message.

How long should the ids be kept?

Choose one answer.

Question 8 of 9

A moved path takes 74 ms through the facade and a legacy path takes 184 ms. A fifth of the requests (20%) have a moved path.

What is the average time?

Enter a number and choose its unit.

Question 9 of 9

A team moves /invoices to a new service while the legacy system still reads and writes the invoices table.

What is the hardest part of this step?

Choose one answer.

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

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.