OBLYTECH Logo
OBLYTECH Logo

Idempotency Keys for Inbound Integration Requests

An idempotency key is a unique value a sender attaches to an inbound request so the receiving service can recognize a repeat. When a retry carries a key the service has already processed, the service returns the original result instead of creating a second record. That keeps one business event from becoming two contacts, cases or orders.

Why a retry creates duplicate records

Many duplicate records in integrations start with a timeout rather than a defect in the business logic. The sender posts a request, the receiver saves the record, and the response is lost on the way back. The sender cannot tell whether the write happened, so it sends the request again. Without a guard on the receiving side, the second copy looks like new work.

Consider an order intake endpoint that a partner calls with a JSON body. The partner’s client gives up after eight seconds, but the receiver has already created the order. The client retries, and without a key the receiver creates a second order and a second invoice that finance then has to unwind by hand. With a key, the retry returns the identifier of the original order and nothing new is written.

Message brokers that redeliver unacknowledged messages, scheduled jobs that rerun after a partial failure and people who double-click a slow submit button produce the same pattern. An idempotency key gives the receiver a way to ask “has this operation already run” instead of “did a request arrive.”

Deciding which inbound requests need a key

Not all inbound calls need protection. A read that returns the same data on each call creates no side effect, so repeating it costs nothing beyond the call itself. Writes deserve attention. Start by listing the endpoints and message types that create or change something a person would later have to find, correct or reverse.

A quick screen for each request type

  • Creates a new record: protect it with a key.
  • Adds to or subtracts from a running value, such as a quantity, a balance or a count: protect it with a key, because a repeat doubles the change.
  • Triggers an outbound action, such as a notification or a call to a third system: protect it with a key, or add a check on the downstream side.
  • Sets a field to a fixed value, such as a status: a repeat is often harmless, but it can overwrite a change that happened after the first request, so review it with the same care as the other types.

Designing the key and its scope

Choosing what the key contains

The sender generates the key before its first attempt and reuses the same value on each retry of that operation. A random version 4 UUID works well for this. A deterministic value built from the sender’s own record identifier and the action name also works, as long as the same logical operation reliably produces the same string. The rule behind the value matters more than its format: one logical operation, one key, for its whole life.

Setting the scope

Scope each key to the sending system, the endpoint and the operation. Two senders can generate the same string by chance, and one integration can reuse a key template across unrelated actions. In either case the receiver would hand back the wrong stored result. Store the sender identifier, the endpoint name and the operation name with the key, and match on all of them on each lookup.

Decide how long the receiver keeps each key. The window should cover the sender’s full retry schedule plus the time a person might need to look into a failed job. Base the number on your own retry settings and your own investigation lead time, not on a figure copied from another project.

Handling a key in the receiving flow

The check, claim and store sequence

  1. Read the request and extract the key, the sender identifier and a fingerprint of the request body.
  2. Look up the key within its scope. If no entry exists, claim it as in progress before any business work begins.
  3. If the entry holds a completed result, return that stored result and skip the work.
  4. If the entry shows work still in progress, return a response that tells the sender to wait, rather than starting the work a second time.
  5. When the work finishes, store the outcome against the key, including the identifier of any record created.

Comparing the request body

A key reused with a different payload is a sender defect, not a retry. Store a fingerprint of the original body with the key and compare it on each attempt. When the key matches and the body differs, reject the request with an error that names the conflict. Returning the first stored result silently hides the bug, and the wrong data tends to surface days later in a report where nobody connects it to the retry.

Concurrency, timeouts and partial failure

Two copies of one request can arrive close together, for example when a sender times out and retries while the first attempt is still running. The claim step is what keeps both copies from doing the work. The claim has to be an atomic write in whatever store holds the keys, so a single attempt wins it. A check followed by a separate write leaves a gap in which both attempts pass the check.

Partial failure needs its own treatment. If the receiver creates the record and then fails before it stores the outcome, the key still shows in progress, or shows no result. A retry may then create a second record. Where the record and the key entry live in the same database, write both in one transaction. Where they live in different systems, use a unique field on the record, such as an external identifier, to detect the earlier write before creating another one.

Set a limit on how long an in-progress claim is trusted. If a worker crashes mid-task, its claim can stay in place indefinitely. Decide what happens when the limit passes, such as moving the entry to a failed state that a person reviews, and write that rule down with the integration.

Each retry is another call against whatever allowance the sender runs under, so check the budget before raising retry counts. The API call budgets checklist is a good place to start that planning.

Coordinating keys across systems that share records

A key guards one sender’s retries. It does not stop a second system from creating a conflicting record for the same customer or case. When two systems both write to one object, agree on which one owns creation and which one updates. Write that rule down where both teams can read it. Duplicates often trace back to two integrations that each behaved correctly and each assumed the other was the owner.

Bulk loads raise a related problem. A job that reruns from the start after a mid-file failure resends the records the first run already created. Give each row in the load file a stable source identifier and treat that identifier as the key for the load. For the wider load design, see the data migration integration pages.

When ServiceNow and Salesforce both touch the same request or case, settle the ownership question before writing any key logic. The ServiceNow and Salesforce integration boundaries page is a sensible starting point for that discussion.

For help planning this work, see Salesforce implementation.

Testing and monitoring the pattern

Test the pattern against the failures it exists to handle, not just the happy path. Send one request, send the identical request again, and count the records that exist afterwards. Repeat the test while the first request is still running to exercise the claim. Send a reused key with a changed body and confirm the conflict response. Finally, simulate a receiver failure between the write and the stored outcome, retry, and count again.

Count the records, not just the response codes. A receiver can return a clean success on a retry while still creating a second record, and the response alone will not show it. In production, watch these signals:

  • Log the key, the sender identifier and the lookup outcome for each request, so support staff can trace a duplicate back to its first attempt.
  • Alert on a rising count of conflict responses. A spike often points to a sender that regenerates keys when it should reuse them.
  • Report in-progress entries older than the claim limit, since these are the entries that tend to block a legitimate retry.

Questions about idempotency keys for inbound integrations

What response should a repeated request receive?

Choose one response for a repeat that matches a stored result, and have it return the identifier of the original record. Choose a separate response for a key reused with a different body. Document both in the sender’s contract so the sending team can code its retry logic against them.

Does an idempotency key replace duplicate-matching rules on the object?

No. A key recognizes retries from one sender. Duplicate-matching rules catch the same real-world person or account arriving from different sources, such as a web form and a partner feed. Keep both, because each covers a gap the other leaves open.

Can keys be added to requests that already run without them?

You can add them to new traffic, but a key cannot identify retries that happened before it existed. For duplicates already in the system, run a report on the natural fields of the object, such as a name and email pair, and review each match before merging or deleting anything.

Should a sender back off between retries?

Spacing retries out gives the first attempt time to finish, which reduces the number of in-progress responses the sender receives. Pick intervals that fit inside the retention window, and cap the total number of attempts so a stuck request does not keep the sender busy indefinitely.