← Back to journal
Backend / 2026.11.19

What happens when a payment request is sent twice?

Explore how idempotency helps backend engineers prevent duplicate payments, handle network failures, and build reliable REST APIs using database transactions, unique request identifiers, and intelligent retry mechanisms.

A technical illustration of an idempotent financial API architecture, showing duplicate transaction requests passing through a secure backend system and producing a single successful database transaction.

In modern software development, building an API that works is only the beginning. Building an API that remains reliable when something goes wrong is a much greater engineering challenge.

This is particularly important in banking systems, e-commerce platforms, payment gateways, and other applications that process critical transactions.

1. Understanding the problem

Imagine a customer transferring money through a mobile banking application.

The backend successfully processes the transaction, but a network interruption prevents the application from receiving the response.

The customer presses the transfer button again. Without proper safeguards, the backend might process the same transaction twice.

This could result in duplicate payments, incorrect account balances, financial losses, and a poor customer experience.

One effective way to address this problem is through idempotency.

2. What is idempotency?

Idempotency is a property that allows an operation to be repeated multiple times without producing additional unintended effects.

For example, when a payment request is submitted multiple times using the same idempotency key, the backend should process the intended payment only once and return the existing result for subsequent requests.

This makes retrying requests much safer.

3. Designing an idempotent REST API

Consider the following API endpoint:

POST /api/v1/transfers

The client generates a unique identifier and includes it in the HTTP request header:

Idempotency-Key: 550e8400-e29b-41d4-a716-446655440000

The request contains the sender account, recipient account, transfer amount, and other necessary information.

The backend follows these steps:

  1. Authenticate the user and validate the request.
  2. Check the idempotency key within the user’s account scope.
  3. Verify whether the same key has already been used.
  4. Process the transaction if it is a new request.
  5. Store the transaction result and idempotency record.
  6. Return the existing result when an identical request is retried.

If the same key is reused with different transaction details, the API should reject the request rather than process a different payment.

4. Database transactions and concurrency

Checking whether a request already exists is not sufficient on its own.

Two identical requests could arrive at the server simultaneously. Both requests might pass the initial validation before either one creates a transaction record.

To prevent this race condition, backend engineers should consider:

  • Database unique constraints
  • Atomic database transactions
  • Row-level locking where appropriate
  • Request payload verification
  • Transaction status management

For example, in a Laravel application, DB::transaction() can help ensure that related database operations are committed or rolled back together.

A unique database constraint on the account identifier and idempotency key provides an additional safeguard against concurrent duplicate requests.

When external payment providers are involved, their own idempotency mechanisms and transaction reconciliation may also be necessary.

5. Handling failures and retries

Network failures are unavoidable in distributed systems.

Instead of assuming every request will succeed immediately, applications should be designed to handle temporary failures gracefully.

Useful techniques include:

  • Exponential backoff with jitter
  • Request timeouts
  • Limited automatic retries
  • Transaction status tracking
  • Structured application logging
  • Background job processing

The client must reuse the original idempotency key when retrying the same logical operation.

Retries should never blindly repeat financial operations without appropriate safeguards.

6. Testing API reliability

Reliable backend systems require more than successful test cases.

Engineers should also test unexpected scenarios, including:

  • Duplicate API requests
  • Simultaneous transaction submissions
  • Invalid request parameters
  • Insufficient account balance
  • Database connection failures
  • Network timeouts after successful processing
  • Reused idempotency keys with modified request data

Tools such as Postman, PHPUnit, and automated integration tests can help verify that the system behaves correctly under both normal and abnormal conditions.

7. Engineering beyond functionality

Backend engineering is not simply about writing code or connecting an application to a database.

It involves understanding how systems behave under pressure, anticipating failures, protecting data integrity, and making architectural decisions that improve reliability.

My experience working with banking-related systems has strengthened my interest in secure backend architecture, database management, API development, and software quality.

I believe that a reliable system should be designed not only for successful operations but also for the situations where things go wrong.

Conclusion

Idempotency is a powerful technique for building dependable APIs, particularly in financial and distributed systems.

By combining idempotency keys, database constraints, atomic transactions, and carefully designed retry mechanisms, developers can significantly reduce the risk of duplicate operations.

Great software is not just software that works. It is software that continues to behave correctly when unexpected failures occur.

Get in touch