How to Avoid Double Payment: Techniques and Tips for Payment Systems

Roman Glushach
4 min readMay 28, 2023

--

Payment systems are essential for enabling transactions between customers and businesses in the digital world. They allow customers to buy goods and services online or pay their bills with ease and convenience. However, payment systems also face the challenge of preventing double payment, which can damage their credibility and annoy their customers.

Retry Technique

Retry Technique

Retry is a technique that allows payment systems to attempt to execute a payment order multiple times until it succeeds or reaches a maximum number of retries. This technique ensures that the payment order is executed at least once, even if there are network failures or timeouts. For example, imagine a customer wants to pay $10, but the payment fails due to a network error. In this case, the payment system will retry the payment until it succeeds or gives up after a certain number of attempts.

Implementation tips:

  • set a maximum number of retries: To prevent the payment system from retrying indefinitely, it’s essential to set a maximum number of retries. This number can vary depending on the payment system’s requirements and the nature of the transaction
  • use exponential backoff: Exponential backoff is a technique that can help to avoid overwhelming the system with retries. This technique involves increasing the delay between each retry, starting with a shorter delay and gradually increasing it. This approach can help to reduce the load on the system and improve the chances of a successful payment.
  • monitor and log retries: Monitoring and logging retries can help payment systems to identify patterns and potential issues. Payment systems can use this information to optimize their retry strategy and improve their overall performance

Pros:

  • ensures that the payment order is executed at least once
  • helps to recover from network failures and timeouts
  • can improve the overall success rate of payments

Cons:

  • can increase the load on the system
  • may result in duplicated payments if not implemented correctly
  • may cause delays in payment processing if the maximum number of retries is too high

Idempotency Technique

Idempotency Technique

Idempotency is a technique that ensures that the payment system produces the same result regardless of how many times it executes a payment order. This technique helps to prevent duplicate payments and ensures that the payment order is executed at most once. For example, imagine a customer wants to pay $10, but the payment system receives two identical requests due to a network glitch. In this case, the payment system will only charge the customer $10 once and ignore the second request.

Implementation tips:

  • Idempotency Key: An idempotency key is a unique value that is generated by the client and attached to the HTTP header. The server will check if the idempotency key has been used before and only process the request if it is new. A UUID (Universally Unique Identifier) is a common choice for an idempotency key and is recommended by many tech companies such as Stripe and PayPal or Twitter’s Snowflake Service
  • Idempotent Methods: Payment systems should use idempotent methods for any operation that can result in a state change, such as charging a credit card or transferring funds. These methods should always produce the same result, regardless of the number of times they are executed
  • Store Idempotency Keys: Payment systems should store idempotency keys for a certain period to ensure that the same key is not used again. This period can vary depending on the nature of the transaction and the payment system’s requirements

Pros:

  • ensures that the payment order is executed at most once
  • prevents duplicate payments and potential fraud
  • improves the overall reliability and accuracy of payment systems

Cons:

  • requires additional development effort and infrastructure
  • may increase the complexity of the payment system
  • may result in rejected payments if the idempotency key is not unique or is invalid

Conclusion

Double payment is a serious issue that can harm the reputation of payment systems and cause frustration among customers. To prevent double payment, payment systems need to ensure that payment orders are executed exactly once. Retry and idempotency are two techniques that can help payment systems to achieve this goal. Retry ensures that payment orders are executed at least once, even in the presence of network failures or timeouts, while idempotency ensures that payment orders are executed at most once, preventing duplicate payments. By implementing these techniques, payment systems can improve their reliability, accuracy, and customer satisfaction.

In conclusion, payment systems need to prioritize preventing double payment to maintain their reputation and customer satisfaction. By implementing retry and idempotency techniques, payment systems can improve their reliability, accuracy, and overall performance. Payment systems should carefully consider the tips and best practices outlined in this article to ensure that they implement these techniques effectively and efficiently.

--

--

Roman Glushach

Senior Software Architect & Engineer Manager at Freelance