A wallet transfer request times out. The payment service has no confirmation that the debit succeeded.
The obvious response is to retry. But a timeout tells us only that a response did not arrive in time. It does not tell us whether the account service received the request, whether it is still processing it, or whether it completed the debit and lost the response on the way back.
Those cases can look identical to the caller. Treating every timeout as a failure can cause a second debit. Treating it as success can tell a customer that money moved when it did not.
A timeout is an unknown outcome, not proof of failure.
Start with a stable transaction ID
Every logical transfer needs an identity that stays the same across retries. The caller should generate a transaction ID once, then reuse it if the request times out.
type TransferRequest struct { TransactionID string AccountID string Amount int64 Currency string } A retry with the same TransactionID means “check or continue this transfer.” A retry with a new ID looks like a new transfer and may be processed as one.
The ID is useful only if the service applying the debit checks it. Adding a key to the request does not, by itself, prevent duplicate processing.
Make the debit idempotent
The account service should record that it has handled a transaction ID and avoid applying the same debit twice. The check and the debit need to be coordinated. If the service applies the debit but crashes before recording the transaction ID, a retry could still debit the account again.
A simplified handler might look like this:
func (s *Service) Debit(ctx context.Context, req TransferRequest) error { return s.store.WithTransaction(ctx, func(tx StoreTx) error { alreadyProcessed, err := tx.HasTransaction(ctx, req.TransactionID) if err != nil { return err } if alreadyProcessed { return nil } if err := tx.ApplyDebit(ctx, req.AccountID, req.Amount); err != nil { return err } return tx.RecordTransaction(ctx, req.TransactionID) }) } This is illustrative, not a complete payment implementation. The storage layer must make the transaction check, debit and record safe under concurrent requests. For example, two copies of the same request could arrive together. Both must not pass the duplicate check and apply the debit.
If the debit and transaction record cannot be committed together, the system needs a recovery design for partial completion. Duplicate prevention should happen where the side effect occurs.
Don’t turn “unknown” into “failed”
Suppose the account service records the debit, but the response is lost. The caller sees a timeout, while the account service has completed the operation. If the caller immediately marks the transfer as failed, its record no longer matches reality.
A payment workflow should distinguish a confirmed failure from an unresolved outcome. The exact state names are up to the system, but the distinction matters:
type TransferStatus string const ( StatusPending TransferStatus = "pending" StatusSucceeded TransferStatus = "succeeded" StatusFailed TransferStatus = "failed" StatusUnresolved TransferStatus = "unresolved" ) The caller can use the original transaction ID to check the operation’s status before deciding what to do next. If the service cannot confirm the result, it should preserve the uncertainty rather than claim success or failure without evidence.
Retry, but don’t retry blindly
Retries are useful for temporary problems, but they can increase load when a dependency is already struggling. A retry policy should limit attempts and space them out. Backoff, and jitter in systems with many clients, helps prevent repeated requests from arriving all at once.
Retries do not replace idempotency. Backoff may reduce pressure on the account service, but only the service handling the debit can prevent a repeated request from creating a second debit.
The request ID must also remain stable across attempts:
for attempt := 0; attempt < maxAttempts; attempt++ { err := accountService.Debit(ctx, request) if err == nil { return nil } if !isRetryable(err) { break } if err := waitWithBackoff(ctx, attempt); err != nil { break } } // The result may still be unknown. Check or reconcile using // request.TransactionID before declaring the transfer failed. return markUnresolved(request.TransactionID) The helper functions and policy are intentionally omitted because their implementation depends on the service and its error model. The key detail is that every attempt uses the same request.
Reconcile when the answer is still missing
Sometimes retries and status checks cannot establish what happened. Then the system needs to compare its records with those of the account service.
The transaction ID should connect the payment record, debit record and retry history. If one service shows a completed debit and another still shows a pending transfer, reconciliation can identify the mismatch and resolve it. Reconciliation should not blindly issue another debit. Its job is to establish the transaction’s state.
It is also worth recording enough operational information to investigate the case: the transaction ID, attempt times, state changes and relevant service responses. Logs should avoid exposing sensitive account data unnecessarily.
Test the failure path
A successful request is only one part of the behaviour to test. The more revealing case is a debit that succeeds while its response is lost. The caller should retry with the same ID, and the account service should return or expose the original result without applying the debit again.
Also test two retries arriving concurrently, and a service failure between applying the debit and recording the transaction. These cases expose whether duplicate handling is actually enforced or merely assumed.
The central question is not “Did the request fail?” It is “Did the operation fail, or did we fail to observe its outcome?” In a payment system, that distinction is what keeps a timeout from becoming a second transfer.


