A launch carrying an Idempotency-Key starts at most one run
A client that starts a run on another implementation retries after a timeout or a crash. A key sent with the launch lets the retry get the first answer back instead of starting the workflow a second time.
The rule
- An implementation MAY honour an
Idempotency-Keyrequest header on theworkflow_runroute (API-9). - An implementation that honours it MUST do so as this rule says.
- It MUST NOT honour the key of a launch from a caller it cannot identify, because callers it cannot tell apart would share one scope and could replay each other's answers.
- It MUST refuse such a launch with 400 and the code
IDEMPOTENCY_KEY_NOT_ACCEPTEDrather than start a run, so that a client relying on the key is never given a second run unawares. - The header's value MUST be a String as Structured Field Values define it.
- An implementation MUST accept a key of at least 255 characters.
- A header that is repeated, is not a String, is an empty String or exceeds the implementation's limit MUST be refused with 400 and the code
INVALID_IDEMPOTENCY_KEY. - A key's scope is the caller, the tenant where the implementation keeps tenants apart, and the route together with its workflow identifier.
- A key in one scope MUST be treated as a different key from the same key in any other scope, so that no refusal ever tells a caller a key is in use elsewhere.
- A launch with a key claims that key in its scope where the key is not already claimed there.
- A launch that finds its key already claimed makes no claim of its own.
- Two launches are the same request when their bodies are equal as JSON values, not as octets, and their query parameters are equal as multisets of name and value pairs, each name and value compared after percent-decoding.
- A launch whose key is claimed, and which is not the same request as the claiming launch, MUST be refused with 422 and the code
IDEMPOTENCY_KEY_REUSED. - A launch whose key is claimed and which is the same request MUST NOT start a run.
- A claim is in flight from when it is made until the claiming launch is answered.
- An asynchronous launch's claim also stops being in flight once it persists its run.
- A synchronous launch's claim stays in flight while its run executes, until the launch answers or, where the launch never answers, until the run reaches a terminal status.
- While the claim is in flight, such a launch MUST be refused with 409 and the code
IDEMPOTENCY_KEY_IN_FLIGHT. - A claim that is no longer in flight has an answer: the claiming launch's answer where that launch was answered, and otherwise the answer API-10 gives for the persisted run.
- A claim therefore has one answer, which every replay returns.
- Such a launch MUST then be answered with the claim's answer, its status and body, and with the header
Idempotent-Replayed: true, even where the run has since ended or been cancelled. Idempotent-Replayedis defined by this rule.- Before it replays an answer, the implementation MUST check the caller's authority to launch the workflow again, as for a new launch, and MUST refuse a caller who no longer holds it as a new launch would be refused.
- A launch that finds its key already claimed releases nothing, whether it is refused, replayed or refused for want of authority.
- Only a launch that started a run keeps its claim.
- Any other launch that made a claim and is refused, with 422 for failed validation or for its inputs among them, MUST release the key it claimed, so that a later launch with that key is judged afresh.
- A claim whose launch has not persisted a run 5 minutes after the claim was made MUST be released, so that a launch which ended without persisting its run never blocks a retry for long.
- The launch that made a released claim MUST NOT persist a run under it afterwards, so that it never starts a run beside the retry.
- Such a launch MUST be answered with 503 and the code
IDEMPOTENCY_CLAIM_RELEASED, so that its caller retries. - A claim that is no longer in flight MUST be kept for at least 24 hours from the moment it stopped being in flight.
- An implementation that honours the header MUST publish in its discovery document the member
idempotent_launch: an object whose memberretention_secondsis an integer, the number of seconds it keeps a claim, at least 86400. - The member's presence means the implementation honours the header as this rule says.
What it means
A client that launches a run on another implementation cannot always tell whether its launch arrived. The connection drops after the request went out, or the client crashes before it reads the answer. Retrying blindly may start the workflow twice; not retrying may start it never. A key sent with the launch settles it: the retry carries the same key, and the implementation answers it with the first launch's answer rather than a second run.
The rule is optional, because keeping keys needs storage that not every implementation has. An implementation that keeps them says so in its discovery document (API-9), with how long it keeps them, so a client can tell an implementation that runs a retried launch exactly once from one that may run it again.
What counts as the same key. A key belongs to one caller, one tenant and one
workflow's launch route. Another caller sending the same string has sent a different
key, and gets an ordinary launch, never a refusal: a refusal would tell that caller
the key is already in use by someone. That only works where the implementation can
tell callers apart. Callers it cannot identify would all share one scope, and any
one of them could replay another's answer, so a launch with a key from
such a caller is refused with 400 IDEMPOTENCY_KEY_NOT_ACCEPTED. The refusal
starts no run, so a client that relies on the key is never given a second run
unawares.
What a retry gets. A retry with the same body and query parameters starts
nothing. If the first launch has been answered, the retry gets that answer again
(status and body) with Idempotent-Replayed: true, even if the run has finished or
been cancelled since: the answer says what the launch did, not where the run is
now. If the first launch is still being handled, the retry is told so with 409
and tries again later.
Bodies are compared as JSON values, so a client that re-serialises its body with
its members in another order or 1.0 for 1 is still sending the same body. Query
parameters are compared the same way, by meaning: decoded, and as a multiset of name
and value pairs in any order, so ?a=1&b=2 and ?b=2&a=%31 are the same request.
When "still being handled" ends. The case this rule exists for is a launch that
saved its run and then never answered: the process crashed, or the connection
dropped. For an asynchronous launch the answer is fixed the moment its run is
saved, since it can only ever be API-10's: the run's identifier and queued. So
its claim stops being in flight as soon as the run is saved, and a retry from then
on gets that answer, whether or not the first launch managed to send it.
A synchronous launch is different. It saves its run and then waits for it, and its
answer is whatever the run produced. Replaying a 202 queued while it waits would
give one retry an answer the launch never gave, and a later retry the real one. So
its claim stays in flight, and retries get 409, until the launch answers. Only if
the launch never answers, because it died while its run went on, does the claim end
when the run does, with API-10's answer. Either way a claim has one answer, and
every replay returns it.
A launch that dies before it saves a run leaves a claim with nothing behind it; that
claim is released after five minutes, and the launch, should it still be alive, may
not save a run under it: it answers 503 IDEMPOTENCY_CLAIM_RELEASED instead, and
its caller retries. The retry that follows starts the one and only run. The 24 hours
of retention count from the moment the claim stopped being in flight.
A replay is still a launch. Before it hands back a stored answer, the
implementation checks again that the caller may launch the workflow. A caller who
has lost that authority since the first launch is refused as a new launch would be,
rather than shown the answer. Idempotent-Replayed is this rule's own header; the
draft the rule is modelled on does not define one.
What a mistake gets. The same key with a different body is a client bug, most
often a key reused for a second launch, and is refused with 422 rather than
replayed, because replaying would hand back the answer to a request the client did
not send this time.
What a refusal leaves. Only a launch that started a run holds on to its key. A
launch that finds its key already claimed makes no claim of its own, so whatever
it is answered (a replay, a 409, a 422, a refusal for want of authority), it
releases nothing. A launch that made a claim and is refused because its inputs were wrong or its workflow failed validation
releases the key, so the client can fix the cause and launch again with the same
key.
Example
The workflow_run route, with a key, and a retry after the first answer was lost.
POST /api/flowdrop/workflow/{workflow_id}/run
Idempotency-Key: "6f1c2b8e-1d2a-4c55-9a51-0b7e4f1d9e20"
{"success": true, "data": {"execution_id": "4711", "status": "queued"}}POST /api/flowdrop/workflow/{workflow_id}/run
Idempotency-Key: "6f1c2b8e-1d2a-4c55-9a51-0b7e4f1d9e20"
{"success": true, "data": {"execution_id": "4711", "status": "queued"}}The retry carries Idempotent-Replayed: true and names the same run; no second run
exists. Sent with a different body, the same key is refused with 422
IDEMPOTENCY_KEY_REUSED.
{"idempotent_launch": {"retention_seconds": 86400}}Why
At least 255 characters. A key is usually a UUID, 36 characters, sometimes with a prefix the client adds to tell its own retries apart. 255 leaves room for both with a wide margin, and is the same floor a workflow's name has (STORE-2). A floor, not a ceiling: an implementation may accept longer keys, and a client that stays within 255 is accepted everywhere.
At least 24 hours. The retries this rule exists for come after a timeout, a
crash or an outage. A client that restarts reaches its unfinished launches within
hours, and a day covers an outage that lasts through a night. A key kept for less
would expire under exactly the retries it is for. An implementation that keeps keys
longer says so in retention_seconds.
Five minutes for a claim with no run. Before it saves its run, a launch only checks its body, its inputs and the workflow, which takes far less than a minute. A claim still waiting after five minutes belongs to a launch that is not coming back. Five minutes is also about as long as a client's retries back off for, so a client that retries after a crash is unblocked by the time it tries again, rather than being refused for a day.
Why
Recorded under OPEN-53.
References
Normative: incorporated into this rule
- RFC 9651Structured Field Values for HTTPWhat a String is (section 3.3.3): the form the header's value takes, and so what makes a key malformed.
Further reading
- draft-ietf-httpapi-idempotency-key-header-07The Idempotency-Key HTTP Header FieldThe header's name and the model this rule follows: a key that replays the first answer, 422 for a key reused with a different request and 409 for a key whose first request is still being processed. The
Idempotent-Replayedheader is not taken from the draft; this rule defines it. An Internet-Draft is work in progress; this rule pins revision 07 and states every behaviour it takes from it, so a later revision changes nothing here.