Urgent.News

What's breaking now, across thousands of outlets.

Tech

The Payment Succeeded. The Response Didn't. Now What?

An engineering deep dive into API retries, idempotency keys, PostgreSQL concurrency, and the failure that happens between two systems. You order something online, enter your card details, and click Pay . Five seconds later, the page gives up. No receipt. No confirmation. Just a timeout and a button that says Try again . There are two possibilities. The payment never happened, or it happened and…

When you purchase an item online and click "Pay," you expect a confirmation that the transaction has been successful. However, sometimes the payment process fails, leaving you with no receipt or confirmation, and only a timeout and a "Try again" button. This uncertainty can lead to confusion, as clicking "Try again" could either complete your purchase or initiate a second charge.

The issue at hand is how backend systems handle such scenarios. The solution appears simple at first glance: assign every payment a unique ID and remember it. But this is only the tip of the iceberg. The real challenge lies in dealing with various failure points and ensuring that the payment process remains correct even when things go wrong.

Consider a simplified checkout flow: the browser sends a request to an API, the API records the operation in its database, the database commits the transaction, and then the API returns a success response. However, if the response gets lost during transmission between the API and the browser, it creates a situation where the client observes a timeout, but the server may or may not have completed the payment process.

This is where idempotency comes into play. In simple terms, an idempotent operation produces the same effect whether it is executed once or multiple times. For instance, setting an account's status to "active" is idempotent, as repeating the operation does not change the final state. However, adding credits to an account is not idempotent, as repeating the operation would change the account's balance each time.

Idempotency plays a crucial role in payment APIs to ensure that two HTTP requests, even if they are sent multiple times due to timeouts or other failures, result in a single intended payment. To achieve this, a unique identifier called an "Idempotency-Key" is generated by the client when the payment process begins. If the request fails and needs to be retried, the same key is used.

This way, the server knows that the payment request is a retry and can handle it accordingly, without creating a new intended payment operation.

In summary, the payment succeeded, but the response did not. Understanding the intricacies of idempotency and how it helps in handling payment failures is crucial for building reliable and robust backend systems.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Local SEO Basics for Service Businesses

You build a fast, clean, beautiful website for a local plumber. Lighthouse scores in the 90s, proper headings, a tidy contact form. You launch it, hand over the keys and move on.

  • Proper website structure crucial for local SEO
  • Google Business Profile verification essential
  • Service-area businesses need separate profiles

More from Saturday 24 October →