返回提示词目录第 1447 页

分布式事务补偿流程设计

针对跨服务业务梳理事务步骤与补偿动作,输出协调机制、失败恢复方案和时序图。

完整提示词

<context>
When one business operation writes to several services, there is no shared database transaction, and two-phase commit across services is rarely available or wise. A saga replaces it: a sequence of local transactions, each with a compensating action that semantically undoes it. What novices miss: compensations are new business actions (a refund, a cancellation email), not rollbacks, and some steps cannot be compensated at all, so order matters. Put retryable and compensable steps before the pivot (the point of no return, often the payment capture or a third-party booking) and only steps that are guaranteed to eventually succeed after it. Sagas also give up isolation: other requests can see half-finished state, so the design needs semantic locks such as a PENDING status. Every step and compensation must be idempotent because messages are delivered at least once.
</context>

<task>
Design the saga for:

<operation>
{{operation}}
</operation>

<services>
{{services}}
</services>

{{#constraints}}
<constraints>
{{constraints}}
</constraints>
{{/constraints}}

1. If the operation's success condition, the services involved, or which steps touch third parties are unclear, ask up to four questions and stop. Otherwise continue and label assumptions.
2. Check first whether a saga is needed: if the data could live in one service and one transaction, or the steps could be reordered so only one service writes synchronously, say so and recommend that instead.
3. List the steps in order. For each: the service, the local transaction, its compensation (or "none, cannot be undone"), whether it is retryable, and the semantic lock it leaves on the record (for example order status PENDING_PAYMENT).
4. Identify the pivot step. Reorder so that compensable steps come before it and only retryable, must-succeed steps come after it. Explain the order.
5. Choose orchestration (a coordinator that owns the state machine, or a workflow engine) or choreography (services react to each other's events). Base the choice on the number of steps, who needs to see saga state, and the coupling each option creates; choreography past three or four steps is hard to follow and debug.
6. Specify failure handling: timeouts per step, retry policy with backoff, idempotency keys and deduplication, what happens if a compensation itself fails (retry until success, then park for a human), and how concurrent sagas on the same entity are prevented or ordered.
7. Specify the saga state record, how messages are published reliably with the state change (transactional outbox or the engine's guarantee), and how stuck sagas are detected, alerted and resumed or compensated by an operator.
8. Draw the happy path and one failure path as a Mermaid sequence diagram.
</task>

<constraints>
- Never call a compensation a rollback without explaining what the user or business sees, for example a charge followed by a refund shows on a card statement.
- Do not recommend two-phase commit or distributed locks across services unless you state why the usual objections do not apply here.
- Do not invent the behaviour of a third-party API (refund windows, cancellation rules, idempotency support); list it as a question to confirm.
- Name the isolation anomalies that remain (dirty reads of PENDING records, lost updates) and how the design limits them.
{{> output/uncertainty}}
</constraints>

<output_format>
## Recommendation
Saga or not, orchestration or choreography, and why, in at most 5 lines.
## Saga steps
Table: order, service, local transaction, compensation, retryable, semantic lock.
## Compensations and the pivot
The pivot step and the reasoning for the order, plus steps that cannot be undone and how the design copes.
## Coordination
The state machine or event flow, with a Mermaid sequence diagram of the happy path and one failure path.
## Failure handling
Bullets: timeouts, retries, idempotency, failed compensations, concurrent sagas.
## Observability and recovery
Saga state, outbox, stuck-saga detection, the operator runbook in brief.
## Open questions
Numbered, each with what it affects.
</output_format>

可替换内容

  • {{operation}}
  • {{services}}
  • {{#constraints}}
  • {{constraints}}
  • {{/constraints}}
  • {{> output/uncertainty}}

分类与标签

编程开发、分布式事务、补偿机制、服务协调、幂等重试、故障恢复

前往 AI 图片创作