Urgent.News

What's breaking now, across thousands of outlets.

Tech

Idempotency for Reliable APIs

Why idempotency matters When you build an API, retries are inevitable. Clients lose connections, timeouts happen, and users double-click buttons. Without idempotency, a retried request can create duplicate orders, charge a credit card twice, or send two emails. Idempotency ensures that the same request, applied multiple times, has the same effect as applying it once. What is idempotency? An…

Idempotency is crucial for reliable APIs, as it prevents duplicate actions and unintended side effects when retries occur. An operation is idempotent if executing it once or several times yields the same outcome. In the HTTP protocol, GET, PUT, and DELETE requests are inherently idempotent; however, POST requests are not, as they create new resources.

To achieve idempotency with POST requests, developers can employ the idempotency key pattern. This involves clients generating a unique identifier (often a UUID) and transmitting it in the request header, such as Idempotency-Key: 123e4567-e89b-12d3-a456-426614174000. The server retains this key alongside the response for a defined duration.

Upon receiving the same key subsequently, the server returns the stored response instead of executing the request again.

To illustrate, let's construct an idempotent endpoint in Node.js using the Express framework. For simplicity, we'll utilize an in-memory store, though production systems would typically employ Redis or a database. First, we require the `express` and `crypto` modules, then establish an Express application. We also define a map to store the idempotency keys and their corresponding responses, along with a time-to-live (TTL) of one hour.

Next, we set up an Express POST route to handle order creation. The endpoint first checks for the presence of the `Idempotency-Key` header. If absent, it returns a 400 error. If present, the application checks the store for an existing entry with the same key. If found and still valid, the stored response is returned. Otherwise, the application simulates processing an order by generating a UUID for the order ID and storing the response in the idempotency store with the updated TTL.

To mitigate a potential race condition where two identical requests might process the order twice, a cleanup mechanism is scheduled to delete expired entries.

For a more robust implementation in a real-world scenario, integrating a database with a unique constraint on the idempotency key is advisable. This ensures atomicity, preventing duplicate entries and ensuring that subsequent retries return the stored response. Using PostgreSQL as an example, a table named `idempotency_keys` can be created with a unique constraint on the `key` column.

In the service code, an attempt is made to insert the idempotency key into this table. If a duplicate key is detected, the existing response is retrieved, thereby atomically handling retries.

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

Failed Webhook Backoff and DLQ Redrive for 3 Marketplace Deadline Guarantees

Short answer: A marketplace renewal reminder should enter the dead-letter queue when another attempt cannot finish before its business deadline; use at-least-once delivery, a stable idempotency key…

  • Marketplace reminders must meet deadlines or go to dead-letter queue.
  • Exponential backoff with jitter prevents synchronized failure waves.
  • Idempotent delivery IDs ensure safe at-least-once processing.

Stop guessing your mana curve: Deterministic math vs LLM intuition

If you've ever played a competitive TCG—Magic: The Gathering, Pokémon, Lorcana—you know the feeling. You spend three hours tweaking a list.

  • TCG Mana Curve Analyzer tool uses deterministic hypergeometrical distribution logic
  • Eliminates guesswork in deckbuilding calculations
  • Provides precise land counts, mana consistency, and mulligan strategies

Building a High-Performance Fan Speed & Temperature Manager with C# and .NET

Hello, I'm Dr. Jhonatan. I recently published an open-source Windows Forms application built with C# / .NET designed for real-time hardware temperature monitoring and dynamic PWM fan speed control…

  • Dr. Jhonatan unveils open-source Windows Forms app called Cooler Speed Change & Management
  • Features real-time monitoring, dynamic PWM fan control, async/await for precise telemetry
  • Multi-language support with UI in 8 languages, source code on GitHub

More from Saturday 29 August →