# Idempotency keys: the API contract that makes retries safe

DevFeed: [Idempotency keys: the API contract that makes retries safe](<https://devfeed.tech/articles/idempotency-keys-the-api-contract-that-makes-retries-safe-85160.md>)

Original publisher: [Read original article](<https://ankit-rana.com/logs/65-idempotency-keys-api-contract/>)

Author: hello@ankit-rana.com

Published: 2026-10-12T00:00:00Z

Content type: tutorial

Language: en

Sources: [Ankit Rana | Mechanical Sympathy](<https://devfeed.tech/sources/ankit-rana-mechanical-sympathy.md>)

Topics: [API](<https://devfeed.tech/topics/api.md>), [REST API](<https://devfeed.tech/topics/rest-api.md>), [database-consistency](<https://devfeed.tech/topics/database-consistency.md>), [retry](<https://devfeed.tech/topics/retry.md>), [Publish-subscribe pattern](<https://devfeed.tech/topics/pubsub.md>), [event-sourcing](<https://devfeed.tech/topics/event-sourcing.md>), [CAP theorem](<https://devfeed.tech/topics/cap-theorem.md>)

Tags: [api](<https://devfeed.tech/tags/api.md>), [api-design](<https://devfeed.tech/tags/api-design.md>), [application](<https://devfeed.tech/tags/application.md>), [concurrency](<https://devfeed.tech/tags/concurrency.md>), [concurrent](<https://devfeed.tech/tags/concurrent.md>), [database](<https://devfeed.tech/tags/database.md>), [distributed-systems](<https://devfeed.tech/tags/distributed-systems.md>), [exactly-once](<https://devfeed.tech/tags/exactly-once.md>), [idempotency](<https://devfeed.tech/tags/idempotency.md>), [keys](<https://devfeed.tech/tags/keys.md>), [postgresql](<https://devfeed.tech/tags/postgresql.md>), [reliability](<https://devfeed.tech/tags/reliability.md>), [retries](<https://devfeed.tech/tags/retries.md>), [transaction](<https://devfeed.tech/tags/transaction.md>)

## AI overview

The article explains how API idempotency keys make retries safe when a client cannot tell whether a timed-out request succeeded. It covers reserving keys atomically, validating repeated requests, replaying stored responses, coordinating external side effects with providers, and the limits imposed by key expiry.

## Source excerpt

A client that times out cannot distinguish a request that failed from one that succeeded with a lost response, so it must retry, and without an idempotency key that retry duplicates the write. The key has to be reserved before the work begins, in the same transaction, so two concurrent copies of the same request cannot both pass the check. The stored response must then be replayed verbatim, because recomputing it can return a different answer than the one the first caller received.