ADR-00034 — Adopt Temporal SDK vs. Hand-Built Saga/Compensation Coordination Code
- Status: Approved / Implemented
- Date: 2026-10-08
- Authors: Engineering Team
Context
ADR-00033 settled that the checkout transaction is coordinated by a single central orchestrator rather than choreography. ADR-00032 settled that the orchestration engine is Temporal rather than Camunda. This ADR settles a third, still-separate question underneath both: given that order-service needs some orchestrator, should that orchestrator be built on a dedicated saga-coordination tool (Temporal) at all, or should the team instead hand-build its own saga coordinator — a state machine persisted to MongoDB, with its own retry loop, its own timeout/polling mechanism, and its own compensation bookkeeping, using only go-lang-core-lib’s existing primitives and no external workflow engine.
Decision Drivers (FRs & NFRs)
See SR-014 and SR-018 for the full requirement lists.
- NFR-01 (Saga Execution Requires Its Own Running Worker): whichever approach is chosen, the saga’s progress must survive a worker-process restart mid-transaction without losing track of which step it reached.
- FR-01 (Order Creation Schedules, Never Executes, the Saga): the coordinator must support a step sequence that can legitimately take minutes (the customer’s own payment-completion time) without blocking the request that started it.
- SR-018 FR-01 (Retry, Durable-Timer, and Signal Primitives MUST Be Supplied by the Coordination Engine): these three primitives must be built-in engine guarantees, not bespoke application code — the exact question this ADR answers for
order-servicespecifically. See BRD-017 BR-01. - SR-018 FR-02 (Crash Recovery MUST Be an Engine Property): an unexpected worker restart mid-transaction must not lose track of which step a transaction had reached, without the service having to implement that guarantee itself.
- BR-04 (BRD-013 — Nothing Is Left Stuck When a Step Fails): retries and compensation must be reliable, not best-effort application code that can silently stop retrying or forget a timeout once the process that was tracking it dies.
Options Evaluated
Option 1 — Temporal SDK: durable execution, retries, timers, and signals as SDK primitives (CHOSEN)
CheckoutSagaWorkflow is ordinary Go code, but every primitive it needs to survive the real-world failure modes of a long-running, multi-step transaction is supplied by Temporal’s SDK rather than hand-written: workflow.ActivityOptions{RetryPolicy: &temporal.RetryPolicy{MaximumAttempts: 3}} retries a failed activity call automatically; workflow.NewTimer/workflow.NewSelector implements the 15-minute payment-wait deadline as a durable timer that survives a worker restart, racing it against workflow.GetSignalChannel’s payment_captured/shipment_created signal channels; and Temporal’s own server persists the workflow’s full execution history, so a worker crash mid-saga resumes exactly where it left off rather than losing track of progress.
- Pros:
- Retries, timers, and crash-recovery are already-tested SDK behavior, not new code this team has to get right.
MaximumAttempts: 3is a one-line declaration ondefaultActivityOptions; the equivalent hand-built behavior — tracking attempt counts, deciding what counts as a retryable vs. terminal failure, persisting that count somewhere that survives a crash — would be new, untested application logic duplicated across every activity the saga calls. - A durable timer is a genuinely hard primitive to hand-build correctly.
paymentWaitTimeout = 15 * time.Minuteracing against a signal viaworkflow.NewSelectormeans the wait itself survives a worker restart — Temporal’s server, not the worker process, is what’s actually tracking the deadline. A hand-built equivalent needs either a persisted “resume at timestamp T” record plus a separate polling/cron process to notice when T has passed, or an in-memory timer that is lost outright if the worker process restarts while waiting — reintroducing exactly the failure mode NFR-01 exists to avoid. - Workflow history replay gives “what happened to this saga” almost for free. Temporal’s own execution history (distinct from, and complementary to, this platform’s own
order_orchestrations/SagaHistoryaudit record — see Architecture-00014) already gives Temporal Web UI-level visibility into every activity attempt, retry, and signal a given workflow run received, withoutorder-servicehaving to build any of that instrumentation itself. - Signal delivery to a specific, addressable, possibly-long-waiting workflow instance is a first-class SDK feature.
PaymentSignalService.SignalPaymentCaptured/ShipmentSignalService.SignalShipmentCreatedresolve directly totemporalClient.SignalWorkflow(workflowID, ...)— the SDK already knows how to route a signal to the correct, currently-suspended workflow execution by ID. A hand-built equivalent needs its own addressing scheme (how does an incoming webhook find “the in-progress saga for order X” and wake it up) and its own mechanism for a process that’s blocked waiting to actually receive that wake-up — in practice, a polling loop checking a database row’s status column at some interval, trading latency for implementation simplicity.
- Retries, timers, and crash-recovery are already-tested SDK behavior, not new code this team has to get right.
- Cons & Accepted Trade-offs:
- A new category of infrastructure (Temporal server + worker) must be run, monitored, and upgraded, which a hand-built MongoDB-backed state machine would not need — this platform already accepts this cost (see ADR-00032’s Option 1 cons).
- Workflow code must follow Temporal’s determinism rules (no direct time/randomness/goroutine use inside workflow code outside the SDK’s own
workflow.*equivalents) — a hand-built state machine in plain Go would have no such constraint, at the cost of having to re-derive the exact guarantees those constraints exist to protect. - Learning curve for a team unfamiliar with the SDK’s workflow-vs-activity execution model — a real, one-time cost, accepted because the alternative’s hidden costs (below) recur on every future saga step added to this or any other workflow, not just once at adoption time.
Option 2 — Hand-built saga coordinator: a persisted state machine with custom retry/timeout/compensation logic
Model the saga as a checkout_saga MongoDB collection holding current_step/status/attempts/next_retry_at fields. A dedicated poller (a cron job or a long-running goroutine) periodically scans for rows whose next_retry_at has passed or whose step has been “in progress” too long, drives each step’s RPC call directly, and on failure decides — in application code — whether to retry, wait, or run a compensating call, writing the updated state back to the same row.
- Pros:
- No new infrastructure class to run — only MongoDB, which
order-servicealready depends on for everything else. - No SDK-specific determinism constraints to learn — the coordinator is just application code calling other services’ RPCs directly.
- No new infrastructure class to run — only MongoDB, which
- Cons (Why Rejected):
- Durable timers become a polling problem. The payment-wait timeout’s exact equivalent — “if 15 minutes pass with no signal, compensate” — needs a poller checking
next_retry_at-style timestamps at some interval, trading immediate reaction for polling latency, and that poller itself becomes new load-bearing infrastructure (its own crash-recovery story, its own “what if two poller instances race on the same row” concurrency problem) that Temporal’s server already solves once, platform-wide, rather than once per hand-built saga. - Retry-count tracking, backoff, and “what counts as retryable” all have to be designed and tested from scratch, and get redesigned again for the next saga this platform ever needs, since none of it is reusable SDK behavior — every one of Temporal’s
RetryPolicyfields (MaximumAttempts, backoff coefficients, non-retryable error types) is a decision this option would have to make, implement, and maintain independently. - Signal delivery requires reinventing addressable wake-up. An incoming webhook resolving “which in-progress saga instance does this belong to, and how do I wake up whatever’s waiting on it” needs either a poller noticing a status-column flip (adding latency) or a bespoke pub/sub-plus-in-memory-registry mechanism inside
order-serviceitself — essentially rebuilding a narrow slice of whattemporalClient.SignalWorkflowalready provides as a single, addressable, server-routed call. - No crash-recovery guarantee beyond whatever this option specifically implements. Temporal’s workflow-history replay exists because the SDK team has already solved “resume a suspended, multi-step execution correctly after a crash” as a general problem; a hand-built coordinator re-derives this guarantee bespoke, and a subtle bug in that logic (a race between the poller and a concurrent signal-handling write, for instance) is this platform’s own bug to find and fix, not a defect to report upstream against a widely-used SDK.
- Every future saga this platform ever adds pays this cost again. Temporal’s primitives are reusable the moment a second workflow is needed; a hand-built coordinator’s retry/timeout/signal logic, built once for the checkout saga specifically, would need to be generalized (itself nontrivial work) or re-built per future saga — the opposite of ADR-00032’s premise that this platform is adopting a durable-execution capability once, platform-wide.
- Durable timers become a polling problem. The payment-wait timeout’s exact equivalent — “if 15 minutes pass with no signal, compensate” — needs a poller checking
Final Decision
Option 1 is selected and implemented. order-service’s checkout saga is built on Temporal’s SDK primitives — RetryPolicy, durable timers, signal channels, and workflow-history persistence — rather than a hand-built, MongoDB-backed coordinator reimplementing the same guarantees from scratch. The one-time cost of adopting an external workflow engine and learning its determinism rules is accepted in exchange for not having to design, test, and maintain retry/timeout/signal/crash-recovery logic as bespoke application code — logic that a hand-built coordinator would need to get right once for this saga and then re-derive again for any future one.
Related Documents
- BRD-013 — Checkout Completion: Real Payment Capture and Reliable Fulfillment Handoff
- BRD-017 — Dependable, Auditable Coordination of Multi-Step Order Transactions
- SR-014 — Checkout Completion: Payment Capture & Fulfillment Handoff System Requirements
- SR-018 — Dependable, Auditable Multi-Step Order Transaction Coordination: System Requirements
- ADR-00027 — Asynchronous Saga + Webhook Relay vs. Synchronous Payment Capture for Checkout Completion
- ADR-00032 — Temporal vs. Camunda for Checkout Saga Workflow Orchestration
- ADR-00033 — Saga/Orchestration vs. Choreography for Checkout Distributed Transaction Coordination
- Architecture — Checkout Saga Orchestration Pattern & Audit Trail
- Microservices: order-service