Urgent.News

What's breaking now, across thousands of outlets.

Tech

Idempotency keys: stop charging customers twice when they double-click Pay

A customer taps Pay . The spinner hangs for four seconds, so they tap it again. Or their phone drops from Wi-Fi to mobile data halfway through the request, and the app retries. Your server sees two identical POST requests, and unless something stops it, the customer is charged twice. Nothing is broken in that story. The network is doing what networks do, and the client is doing what good clients…

When a customer accidentally clicks the "Pay" button twice, or their internet connection fluctuates, they often cause their server to process the same payment request multiple times. This double-charge scenario can be avoided by implementing idempotent requests. An idempotent request is one that can be repeated without changing the outcome, ensuring the customer is only charged once.

Stripe's API and the IETF have proposed a standard for idempotency keys using an HTTP header named "Idempotency-Key." To implement this, the client generates a unique key, such as a UUID, for each action. The server stores the response for each key it receives. If the same key is sent again, the server returns the stored response instead of performing the action again.

This system requires a few conditions to be met: the key must be claimed atomically to prevent two requests from both appearing as the first; a repeat request during the processing of the first must wait or be told to retry; a key reused with different data must be rejected to prevent errors; and keys must be scoped per user to avoid collisions.

The server uses a PostgreSQL table with a primary key on (user_id, key) to ensure only one row exists for each unique key pair. This design guarantees that the "claim this key" action is atomic, preventing simultaneous requests from both being processed as the first. The middleware that handles this process checks for the presence of the "Idempotency-Key" header, generates a request hash from the request body, and attempts to claim the key.

If another request is already using the same key, the middleware determines whether to retry or return an error based on the current status of the key.

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

Firefox Dark Mode: Every Option Explained (and Which Actually Works)

Firefox Dark Mode: Every Option Explained (and Which Actually Works) Firefox has at least four different mechanisms for dark mode, and they work differently.

  • OS dark mode works for websites supporting CSS prefers-color-scheme, but not for others.
  • NightOwl extension provides universal dark mode for non-compliant sites, with per-domain controls.

How to Recover Lost Tabs in Firefox (And Prevent It Next Time)

How to Recover Lost Tabs in Firefox (And Prevent It Next Time) If Firefox crashed and you lost a bunch of open tabs, here's how to get them back.

  • Firefox automatically saves open tabs during crashes via Session Restore
  • Recently Closed Tabs list shows last 25 closed tabs for quick recovery
  • Sessionstore.jsonlz4 file can be decompressed for manual tab extraction

Dark Reader vs NightOwl: Choosing a Firefox Dark Mode Extension

Dark Reader vs NightOwl: Choosing a Firefox Dark Mode Extension Dark Reader is the default recommendation for browser-wide dark mode. NightOwl is a simpler alternative.

  • Dark Reader has been around since 2014 and boasts over 5 million users
  • It offers sophisticated rendering, fine-grained per-site configuration, and active development
  • NightOwl takes a simpler approach by applying a CSS filter: invert() to the entire page

Switching from Chrome to Firefox: What's Different and How to Adapt

Switching from Chrome to Firefox: What's Different and How to Adapt Most of Firefox just works the way you'd expect if you're coming from Chrome. A few things are different. Here's what to know.

  • Chrome's tab groups don't exist in Firefox, use Tab Manager and Session Saver instead
  • Firefox syncs via Firefox account, importing bookmarks and history from Chrome is possible

Moving a project to another org can leave the old org's API keys working

A lot of SaaS products let you move a project from one organization to another, often after an acquisition or when a team splits off. The move updates project.org_id and copies the members over.

  • Old organization's API keys may remain functional after project transfer.
  • API keys are tied to project ID, not organization ID.
  • Notify new org admins of surviving keys post-transfer.

More from Friday 16 October →