Senior · Network services

Idempotent API operations

How should POST /payments behave when the caller retries?

Try explaining this aloud before opening the answer. State your assumptions and trace a boundary case.

Reveal a strong answer
  1. Accept a stable idempotency key with an explicit scope, retention policy and request fingerprint. Two requests using the same key for different payloads should conflict rather than silently share a result.

  2. Claim the operation and record its outcome durably in the same consistency boundary as the state transition when possible. Concurrent requests need a uniqueness rule, not a check followed by an unprotected insert.

  3. Return the prior outcome or an explicit in-progress state. When a remote provider owns the side effect, forward a stable provider key and reconcile unknown outcomes; a local transaction alone cannot roll back the remote payment.

Common trap

An in-memory map of keys fails across restarts and multiple replicas.

Try it

The provider charged successfully but your process crashed before persisting its response. What happens on retry?

Worked solution

Reuse the provider idempotency key or query its operation ID, reconcile the durable local record and return the known outcome. Avoid blindly charging again. Test this crash window with a controllable fake provider.

Change the constraint

How long must keys be retained if clients can retry after a day?

How did you do?

Self-assessment, not a compiler pass or interview score. Explain the reasoning unaided before marking understood.

Not marked. Progress stays on this browser, separate from course completion.

Primary references

Checked 2026-10-20. Use documentation for your pinned runtime, driver and tool version.