An idempotency key is an identifier that a receiving system uses to recognize repeated requests for the same intended operation. When the mechanism is properly enforced, a client can retry a request without unintentionally starting that operation again.
This matters when a response goes missing. A service may have completed the requested action even though the client never received confirmation. Repeating the request with the same key gives the service a way to connect the retry to the earlier attempt.
The key alone does not provide that protection. Its behavior depends on how the service defines the operation, coordinates concurrent requests, records outcomes, handles uncertainty, and retains the record.
Identifying the same intended operation
An idempotency key carries the operation identity across separate network requests. Several attempts to submit that operation should use the same key. A genuinely new operation should receive a new one.
For example, a publishing application might generate a key when a person deliberately submits an article for publication. If the connection fails, the application retains that key for any permitted retries of the same submission. A repeated click for the same pending submission should reuse its existing key rather than generate a new one. If recovery must survive a client restart, the key and its association with the submission must be retained somewhere that survives that restart.
- Same submission, missing response: retain the original key.
- Same submission, temporary connection problem: retain the original key.
- A separate, deliberate publication: generate a new key.
- A revised article or different destination: check whether this is now a different operation requiring renewed review and a new key.
Generating a new key for every retry defeats duplicate recognition. Reusing one key for every action can cause legitimate new operations to be mistaken for earlier ones.
Clients commonly generate opaque, high-entropy identifiers, such as appropriately generated UUIDs. Other arrangements are possible, including a server issuing an operation identifier before execution. What matters is that the identifier is available before the consequential request and remains recoverable if that request loses its response.
Idempotency is the broader property of repeated execution not adding effects beyond the intended result. An idempotency key is one mechanism for supporting that property; it is not required for every naturally idempotent operation.
How a keyed request is handled
A service implementing idempotency keys generally needs more than a list of previously seen identifiers. It needs an operation record and a defined response to each relevant state.
A typical lifecycle looks like this:
- Receive and validate the request. Establish the caller’s identity, authorization, key scope, and relevant request parameters.
- Claim the operation atomically. Create the key’s record only if another request has not already claimed it.
- Perform or schedule the work. Associate execution with the operation record.
- Record the outcome durably. Preserve enough information to identify what happened and answer subsequent requests appropriately.
- Handle repeats according to the recorded state. Return the established result, report that work is still running, reject incompatible parameters, or expose an unresolved outcome.
An example: publication without confirmation
Suppose an application submits an approved article to a publishing service. Its API contract accepts a request header such as:
Idempotency-Key: 9ad467d8-3b0a-4c10-9f24-8dc560b95be2
The header name and format here are illustrative; clients must follow the receiving API’s actual contract.
The service creates the article, records its published identifier, and sends a response. The connection breaks before the application receives that response.
The application retries using the same key. The service checks the caller’s authorization and looks for the key’s record within the applicable scope. If the record is still available, the API’s contract determines the response.
| What the service finds | What it can return |
|---|---|
| The matching article is already published | Return the existing publication result or a reference to the article. |
| The original request is still running | Report that it is in progress and provide a status reference if the API supports one. |
| The retry changes the article revision or destination | Report a conflict or another documented mismatch response. Preserve the original operation. |
Lucent note. Keep the key with the submission. When the confirmation goes missing, the key is how the service can recognize the retry.
Key scope and changed parameters
A key has meaning within a defined scope. Depending on the API, that scope might include an account, tenant, endpoint, operation type, or destination.
For example, a service might identify an operation using the combination of account identifier, operation type, and client-supplied key. Another service might require keys to be unique across every write operation within an account. Neither arrangement should be assumed without documentation.
Scope also establishes an isolation boundary. A request from one account must not retrieve another account’s stored response merely because the same key was supplied. An idempotency key is not an authentication credential and must not replace authorization checks.
Comparing the request with the original operation
A repeated key should not silently authorize a different action. Services often retain a request fingerprint: a representation or digest of the fields that define the intended operation.
For a publication request, those fields might include the article revision, destination site, publication status, and scheduled time. If the same key arrives with a different destination or different article revision, the service can reject it as incompatible with the original operation.
Fingerprinting requires a documented comparison rule. Comparing raw request bytes may treat harmless formatting differences as changes. Comparing too few fields may overlook a consequential difference. The service needs to identify which fields define the operation and how equivalent values are normalized.
If the intended action has changed, the client should first establish the earlier operation’s state before creating a replacement. A new key does not cancel the old operation, reverse its effects, or establish that the revised action has been approved. Cancellation Semantics explains why stopping earlier work requires its own defined behavior. This is also a stale-decision concern when execution depends on human review.
Coordinating concurrent requests
Two requests with the same key can arrive before either has completed. This can happen through parallel retries, repeated interface submissions, or multiple workers recovering the same task.
A simple “check whether the key exists, then insert it” sequence is unsafe if those steps are not coordinated. Both requests may observe that no record exists and both begin work.
The service needs an atomic way to let one request claim the operation. Depending on the architecture, this may involve a database uniqueness constraint, transactional insertion, or another coordination mechanism with suitable consistency guarantees.
A request that didn’t claim the key should follow an explicit policy. It might:
- Wait for the existing operation for a bounded period.
- Return an in-progress response with a status reference.
- Return a documented conflict indicating that the operation is already running.
It should not independently execute the same effect merely because the first result is not yet available.
Recovering an abandoned operation
An in-progress record may outlive its worker. Recovery therefore needs to distinguish an abandoned task from a slow task or an operation whose external effect already occurred.
A lease expiration can indicate that a worker no longer holds execution authority under the service’s rules. It does not, by itself, prove that the worker stopped or that no effect occurred. Safe takeover may require additional coordination, inspection of existing effects, or reconciliation before execution resumes.
Retaining results and handling failures
Services differ in what they retain for a keyed operation. A record might contain the original response, a resource identifier, an operation status, a request fingerprint, timestamps, or a combination of these.
Returning the original response can make retries predictable. Returning a resource reference may be more suitable when the resource can change after creation. Either approach needs a clear contract: is the client receiving the original outcome or the resource’s current state?
Error handling also needs explicit rules:
- Rejected before execution: a validation failure may occur before the key is claimed. Whether a corrected request can reuse that key depends on the API.
- Known failure without a committed effect: the service may permit another attempt under the same operation or retain the failure as its terminal result.
- Known success: subsequent requests should resolve to the established operation rather than execute it again.
- Uncertain outcome: the service should preserve that uncertainty rather than assume the operation is safe to repeat.
An error status alone does not establish which case occurred. A service may report an internal error after committing a change. Some APIs retain and replay such responses; others provide a separate recovery path.
Clients should follow documented retry-safety rules, including retry limits and backoff. Duplicate prevention does not make unlimited retries harmless: requests still consume capacity and can interfere with recovery.
Where protection stops
An idempotency key enforced by one service does not automatically prevent duplicate effects in another service. End-to-End Invariants examines the conditions that must hold across components and their interactions.
Consider a publishing coordinator that records a key locally, calls a separate publishing platform, and then records the platform’s response. If the platform publishes the article but the coordinator crashes before recording the result, the local operation may remain unresolved.
The local key prevents a second independent operation record. It does not necessarily tell the coordinator whether calling the publishing platform again is safe.
Possible protections include:
- Passing a stable key to a downstream service that enforces compatible duplicate-prevention rules.
- Using a stable external reference that allows the existing effect to be found.
- Committing the local effect and idempotency result together when both fit within one database transaction.
- Holding uncertain work for reconciliation or human review instead of repeating it automatically.
A transactional outbox can keep a local state change and its pending follow-up work in one transaction. It does not independently guarantee that the recipient will apply the effect only once.
The same boundary matters for operations with several effects. An article may be published while a notification remains unsent. The operation record should preserve that partial completion rather than treat the whole operation as an untouched failure.
Retention and expiration
Idempotency records are often retained for a limited period. Once a record is removed, the service may treat the same key as new. A delayed retry can then repeat an earlier effect.
The key retention window should therefore be part of the API contract. Clients need to know:
- How long a key remains recognized.
- When that period begins and whether retries extend it.
- How in-progress and unresolved operations are retained.
- What happens when a request arrives after expiration.
- How to investigate an earlier operation when the key can no longer protect a retry.
After the documented window ends, blindly resubmitting the old request is not a reliable recovery strategy. Checking a durable operation identifier, inspecting the resulting resource, or obtaining human review may be necessary.
Retention also has privacy and security consequences. Keys should not contain personal information or secrets. Stored requests and responses should retain only what recovery requires, with access controls and deletion rules appropriate to their contents.
A practical idempotency-key review
Before relying on idempotency keys for a consequential operation, review the complete contract:
- Operation identity: is one key created per intended operation and preserved across retries?
- Scope: which account, operation type, and destination boundaries determine uniqueness?
- Request compatibility: what happens when the same key accompanies changed parameters?
- Concurrency: can only one request claim execution, including across multiple service instances?
- Durability: does the record survive the failures it is intended to help recover from?
- Outcome handling: are running, completed, failed, and uncertain operations distinguishable?
- External effects: what prevents duplication beyond the receiving service’s own records?
- Expiration: when does recognition end, and what should clients do afterward?
- Testing: have concurrent retries, lost responses, worker crashes, changed parameters, and expired records been exercised?
Idempotency keys are useful because they let a service recognize an earlier intention despite unreliable communication. Their reliability comes from the behavior surrounding the identifier: coordinated execution, durable evidence, bounded retention, and honest handling of outcomes that remain unknown.