Skip to content

Latest commit

 

History

History
367 lines (223 loc) · 15.9 KB

draft-ietf-httpapi-idempotency-key-header.md

File metadata and controls

367 lines (223 loc) · 15.9 KB

coding: utf-8

title: The Idempotency-Key HTTP Header Field docname: draft-ietf-httpapi-idempotency-key-header-latest category: std ipr: trust200902 stand_alone: yes pi: [toc, tocindent, sortrefs, symrefs, strict, compact, comments, inline]

venue: group: HTTPAPI type: Working Group home: https://ietf-wg-httpapi.github.io/ mail: [email protected] repo: https://github.com/ietf-wg-httpapi/idempotency

author:

ins: J. Jena
name: Jayadeba Jena
email: [email protected]

normative:

informative:

--- abstract

The HTTP Idempotency-Key request header field can be used to make non-idempotent HTTP methods such as POST or PATCH fault-tolerant.

--- middle

Introduction

In mathematics and computer science, an idempotent operation is one that can be applied multiple times without changing the result beyond the initial application. It does not matter if the operation is called only once or tens of times over.

Idempotency is important in building fault-tolerant HTTP APIs. An HTTP request method is considered idempotent if the intended effect on the server of multiple identical requests with that method is the same as the effect for a single such request. Per {{!RFC9110}}, the methods OPTIONS, HEAD, GET, PUT and DELETE are idempotent while methods POST and PATCH are not.

Let's say a client of an HTTP API wants to create (or update) a resource using a POST method. Repeating the request multiple times can result in duplication or incorrect updates. Consider a scenario where the client sent a POST request to the server, but the request timed out. The client does not know if the resource was created (or updated), because it does not know when the timeout occured from the server's perspective. Furthermore, the client does not know if it can safely retry the request.

Notational Conventions

{::boilerplate bcp14-tagged}

This specification uses the Augmented Backus-Naur Form (ABNF) notation of {{!RFC5234}} and includes, by reference, the IMF-fixdate rule as defined in Section 7.1.1.1 of {{!RFC7231}}.

The term "resource" is to be interpreted as defined in Section 2 of {{!RFC7231}}, that is identified by an URI.

The Idempotency-Key HTTP Request Header Field

An idempotency key is a unique value generated by the client which the resource uses to recognize subsequent retries of the same request. The Idempotency-Key HTTP request header field carries this value.

Syntax

Idempotency-Key is an Item Structured Header {{!RFC8941}}. Its value MUST be a String (Section 3.3.3 of {{!RFC8941}}).

Uniqueness of Idempotency Key

The idempotency key MUST be unique and MUST NOT be reused with another request with a different request payload.

Uniqueness of the key MUST be defined by the resource owner and MUST be implemented by the clients of the resource. It is RECOMMENDED that a UUID {{!RFC4122}} or a similar random identifier be used as an idempotency key.

The following example shows an idempotency key whose value is a UUID {{!RFC4122}}:

Idempotency-Key: "8e03978e-40d5-43e8-bc93-6894a57f9324"

Idempotency Key Validity and Expiry

The resource MAY require time based idempotency keys to be able to purge or delete a key upon its expiry. The resource SHOULD define such expiration policy and publish it in the documentation.

Idempotency Fingerprint

An idempotency fingerprint MAY be used in conjunction with an idempotency key to determine the uniqueness of a request. Such a fingerprint is generated from request payload data by the resource. An idempotency fingerprint generation algorithm MAY use one of the following or similar approaches to create a fingerprint.

  • Checksum of the entire request payload.
  • Checksum of selected element(s) in the request payload.
  • Field value match for each field in the request payload.
  • Field value match for selected element(s) in the request payload.
  • Request digest/signature.

Responsibilities

Client

Clients of HTTP API requiring idempotency SHOULD understand the idempotency related requirements as published by the server and use appropriate algorithm to generate idempotency keys.

Clients MAY choose to send an Idempotency-Key field with any valid value to indicate the user's intent is to only perform this action once. Without a priori knowledge, a general client cannot assume the server will respect this request.

For each request, a client SHOULD:

  • Send a unique idempotency key in the HTTP Idempotency-Key request header field.

Resource

Resources MUST publish a idempotency related specification. This specification MUST include expiration related policy if applicable. A resource is responsible for managing the lifecycle of the idempotency key.

For each request, a server SHOULD:

  • Identify idempotency key from the HTTP Idempotency-Key request header field.
  • Generate idempotency fingerprint if required.
  • Enforce idempotency (see below)

Idempotency Enforcement

  • First time request (idempotency key and fingerprint has not been seen)

    The resource SHOULD process the request normally and respond with an appropriate response and status code.

  • Duplicate request (idempotency key and fingerprint has been seen)

    Retry

    The request was retried after the original request completed. The resource SHOULD respond with the result of the previously completed operation, success or an error. See Error Scenarios for details on errors.

    Concurrent Request

    The request was retried before the original request completed. The resource SHOULD respond with a resource conflict error. See Error Scenarios for details.

Error Handling

If the Idempotency-Key request header is missing for a documented idempotent operation requiring this header, the resource SHOULD reply with an HTTP 400 status code with body containing a link pointing to relevant documentation. Following examples shows an error response describing the problem using {{!RFC7807}}.

    HTTP/1.1 400 Bad Request
    Content-Type: application/problem+json
    Content-Language: en
    {
      "type": "https://developer.example.com/idempotency",
      "title": "Idempotency-Key is missing",
      "detail": "This operation is idempotent and it requires correct
       usage of Idempotency Key.",
    }

Alternately, using the HTTP header Link, the client can be informed about the error as shown below.

    HTTP/1.1 400 Bad Request
    Link: <https://developer.example.com/idempotency>;
      rel="describedby"; type="text/html"

If there is an attempt to reuse an idempotency key with a different request payload, the resource SHOULD reply with a HTTP 422 status code with body containing a link pointing to relevant documentation. The status code 422 is defined in Section 15.5.21 of {{!RFC9110}}.

    HTTP/1.1 422 Unprocessable Content
    Content-Type: application/problem+json
    Content-Language: en
    {
      "type": "https://developer.example.com/idempotency",
      "title": "Idempotency-Key is already used",
      "detail": "This operation is idempotent and it requires
      correct usage of Idempotency Key. Idempotency Key MUST not be
      reused across different payloads of this operation.",
    }

The server can also inform the client by using the HTTP header Link as shown below.

    HTTP/1.1 422 Unprocessable Content
    Link: <https://developer.example.com/idempotency>;
    rel="describedby"; type="text/html"

If the request is retried, while the original request is still being processed, the resource SHOULD reply with an HTTP 409 status code with body containing problem description.

    HTTP/1.1 409 Conflict
    Content-Type: application/problem+json
    Content-Language: en
    {
      "type": "https://developer.example.com/idempotency",
      "title": "A request is outstanding for this Idempotency-Key",
      "detail": "A request with the same Idempotency-Key for the
      same operation is being processed or is outstanding.",
    }

Or, alternately using the HTTP header Link pointing to the relevant documentation

    HTTP/1.1 409 Conflict
    Link: <https://developer.example.com/idempotency>;
    rel="describedby"; type="text/html"

Error scenarios above describe the status of failed idempotent requests after the resource prcocesses them. Clients MUST correct the requests (with the exception of 409 where no correction is required) before performing a retry operation, or the resource MUST fail the request and return one of the above errors.

For other 4xx/5xx errors, such as 401, 403, 500, 502, 503, 504, 429, or any other HTTP error code that is not listed here, the client SHOULD act appropriately by following the resource's documentation.

IANA Considerations

The Idempotency-Key HTTP Request Header Field

The Idempotency-Key field name should be added to the "Hypertext Transfer Protocol (HTTP) Field Name Registry".

Field Name: : Idempotency-Key

Status: : permanent

Specification document: : This specification, Section 2

Implementation Status

Note to RFC Editor: Please remove this section before publication.

This section records the status of known implementations of the protocol defined by this specification at the time of posting of this Internet-Draft, and is based on a proposal described in {{?RFC7942}}. The description of implementations in this section is intended to assist the IETF in its decision processes in progressing drafts to RFCs. Please note that the listing of any individual implementation here does not imply endorsement by the IETF. Furthermore, no effort has been spent to verify the information presented here that was supplied by IETF contributors. This is not intended as, and must not be construed to be, a catalog of available implementations or their features. Readers are advised to note that other implementations may exist.

According to RFC 7942, "this will allow reviewers and working groups to assign due consideration to documents that have the benefit of running code, which may serve as evidence of valuable experimentation and feedback that have made the implemented protocols more mature. It is up to the individual working groups to use this information as they see fit".

Organization: Stripe

Organization: Adyen

Organization: Dwolla

Organization: Interledger

Organization: WorldPay

Organization: Yandex

Organization: http4s.org

Organization: Finastra

Organization: Datatrans

Implementing the Concept

This is a list of implementations that implement the general concept, but do so using different mechanisms:

Organization: Django

Organization: Twilio

Organization: PayPal

Organization: RazorPay

Organization: OpenBanking

Organization: Square

Organization: Google Standard Payments

Organization: BBVA

Organization: WebEngage

Security Considerations

This section is meant to inform developers, information providers, and users of known security concerns specific to the idempotency keys.

Resources that do not implement strong idempotency keys, such as UUIDs, or have appropriate controls to validate the idempotency keys, could be victim to various forms of security attacks from malicious clients:

  • Injection attacks - When the resources does not validate the idempotency key in the client request and performs a idempotent cache lookup, there can be security attacks (primarily in the form of injection), compromising the server.
  • Data leaks-When an idempotency implementation allows low entropy keys, attackers MAY determine other keys and use them to fetch existing idempotent cache entries, belonging to other clients.

To prevent such situations, the specification recommends the following best practices for idempotency key implementation in the resource.

  • Establish a fixed format for the idempotency key and publish the key’s specification.
  • Always validate the key as per its published specification before processing any request.
  • On the resource, implement a unique composite key as the idempotent cache lookup key. For example, a composite key MAY be implemented by combining the idempotency key sent by the client with other client specific attributes known only to the resource.

Examples

The first example shows an idempotency-key header field with key value using UUID version 4 scheme:

Idempotency-Key: "8e03978e-40d5-43e8-bc93-6894a57f9324"

Second example shows an idempotency-key header field with key value using a random string generator:

Idempotency-Key: "clkyoesmbgybucifusbbtdsbohtyuuwz"

--- back

Acknowledgments

{:numbered="false"}

The authors would like to thank Mark Nottingham for his support for this Internet Draft. We would like to acknowledge that this draft is inspired by Idempotency related patterns described in API documentation of PayPal and Stripe as well as Internet Draft on POST Once Exactly authored by Mark Nottingham.

The authors take all responsibility for errors and omissions.