Move Slow Work Out of the Request Path
Added 2026-10
Move Slow Work Out of the Request Path — Chinese original prompt
这个接口在返回前同步完成了 [耗时操作],平均多花 [耗时]。请把它移到请求路径之外:事务提交后投递任务、由消费方异步执行,并处理投递失败与重复执行。输出:改造后的调用流程、事务与消息的一致性保证方式、消费端幂等设计、以及用户如何得知最终结果。
Site original
Chinese original
Example
Sample input
下单接口返回前同步生成了发票和推送通知,平均多花两秒,帮我移到请求路径外。
The output should include
- 事务提交后再投递任务写清
- 投递失败与重复执行的处理
- 用户怎么得知最终处理结果
Replace these placeholders 2
Swap the bracketed parts for your own details before sending.
[耗时操作][耗时]
How to use
别在事务提交前发消息,回滚之后消息已经出去了,比不发更难收拾。
Try tweaking it
- Have it restate requirements and constraints before writing code.
- Ask for the smallest change plus a rollback path, not a full-file rewrite.
What you should get
- A minimal, runnable change with copy-ready code
- Impact list: which files and call sites must follow
Common mistakes
- Asking it to touch ten files at once, then losing track of what broke what.
- Pasting only the error, with no code or version, and getting an unfixable answer.
How to check the output
- The change runs, can be rolled back, and touches nothing unrelated.
- Edge cases (empty, oversized, concurrent) are covered or explained.
Where it works
- Good for: having it restate requirements and constraints, then propose the smallest runnable change.
- Watch out: include versions and a minimal reproduction with the error, or the fix may look plausible but not fit.