Skip to main content

Posts

Idempotency in Web Applications

In web applications and user interfaces, idempotency often appears as duplicate submission protection. A common scenario is a user accidentally clicking a form’s submit button twice. If the backend simply inserts a record on each submission, the user would end up with duplicates. To prevent this, developers add safeguards: for example, disabling the submit button after one click, or generating a unique token for the form. Brandur’s post on Stripe’s approach illustrates adding a hidden <input> with an idempotency key to HTML forms; if the user resubmits, the server can detect the same key and ignore the duplicate request. 1. Disable repeated clicks: On the client side, disable or hide the button after the first click to avoid multiple submissions. 2. Hidden tokens/keys: Include an idempotency key or unique token in the form data; the server checks it like an Idempotency-Key header. 3. Idempotent endpoints: Where possible, use idempotent HTTP methods (e.g. PUT instead of POST) f...

Idempotency in Distributed Systems

In distributed systems, unreliable networks and concurrent execution make idempotency crucial. Consider an online payment: the client submits a charge but loses connection. The client retries, unsure if the first went through. Without idempotency, the user might be double-charged. Designing the service to be idempotent prevents such issues. As one tutorial notes, idempotency acts as a “safety net” against unexpected retries, ensuring system stability. Common strategies include unique request identifiers and deduplication. Each request carries a globally unique ID. When the server or message processor sees a repeated ID, it recognizes a duplicate and ignores it. For example, in a distributed message queue, the consumer can keep track of processed message IDs and skip any that it handled already. If an operation is not naturally idempotent (like withdrawing money), the service can explicitly check for a duplicate transaction ID to avoid reapplying it. Unique IDs: Attach a unique key to ...

Idempotent Database Operations

In databases, an idempotent operation is one that, when repeated, produces the same result as a single execution. A common idempotent pattern is the UPSERT (update-or-insert) operation. For example, running INSERT … ON CONFLICT UPDATE with the same values multiple times will not create duplicates – it either inserts a new row or updates the existing row, but repeating it makes no further change. In contrast, a naïve INSERT statement without conflict handling will create duplicate rows on each execution, which is non-idempotent. Similarly, UPDATE queries that set a field to a fixed value (e.g. SET status = 'active') are idempotent: once the value is set, running the update again has no new effect. Deleting a row by its primary key is idempotent as well – after the first delete, further deletes simply find nothing to remove. Database constraints and keys also help: for instance, a unique constraint can silently ignore or reject duplicate inserts. Transactions contribute by making...