DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Why and When to Use GraphQL
  • GET, POST, PUT, DELETE: Choosing HTTP Verbs in a Real .NET Core Web API, Not Just by the Textbook
  • A Developer's Guide to Chrome Extension Manifest V3 Declarative Net Request API
  • From Microservices to Agent Services: The Next Architectural Shift

Trending

  • Claude Code Superpowers: The 5-Tool Stack That Ends Limits and Context Amnesia
  • Genkit Flows on Kubernetes, Routed by a Decision Model: senro + jev in Go
  • Beyond the Wall of Charts: How Advanced Observability Displaced the Telemetry Dashboard
  • Building a Windows App for a Resilient SSH SOCKS Proxy and Selective Browser Routing

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Fixing Duplicate API Requests

Fixing Duplicate API Requests

The first rule of distributed systems is: "Don’t distribute your system." Designing distributed systems correctly is infamously hard for multiple reasons.

By 
Nicolas Fränkel
·
Apr. 11, 24 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
2.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

The first rule of distributed systems is: "Don’t distribute your system." Designing distributed systems correctly is infamously hard for multiple reasons.

The Idempotency Concept

For example, a call to a function can succeed or fail in non-distributed systems. Once you move the called function to a remote component, a third option appears: you call the remote function but get no response from the component. At this point, it’s impossible to know whether the call reached the component or not; i.e., whether the problem occurred on the way to or the way back.

The only choice is to resend the request again. It’s a non-issue for reads; for calls that update the remote state, it’s "complicated." We need to describe the concept of idempotence:

Idempotence is the property of certain operations in mathematics and computer science whereby they can be applied multiple times without changing the result beyond the initial application.

— "Idempotence" on Wikipedia

In the realm of HTTP APIs:

  • GET, PUT, DELETE, HEAD, OPTIONS, and TRACE are idempotent. For example, if you repeatedly delete an entity from the system, whether the said entity exists or not, the end state will be the same: there will be no entity.
  • POST and PATCH are not idempotent. For example, posting multiple times a new entity will create that many new entities.

A Possible Solution

Imagine that the client sending a request sends a unique key along. The server keeps track of key-request pairs. Overall, two things can happen:

  • The server already has a record of such a pair and discards the request.
  • The server has no such previous record and stores the pair.

It’s precisely the idea behind the IETF specification The Idempotency-Key HTTP Header Field. The Idempotency-Key HTTP header’s value is a string; the specification uses a UUID as an example. It’s the client’s responsibility to generate such a value, which must be unique.

The spec describes the following flow:

Specification flow

The specification mentions the server can optionally fingerprint the request; i.e., hash it, and store the hash instead.

Error Scenarios

The nominal path is pretty straightforward, but the specification also defines three possible error scenarios that can happen.

Here they are:

Description HTTP status code Sample response
The request doesn't provide the idempotency key for a documented idempotent operation requiring this header 400
HTTP
 
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."
}


Attempt to reuse an idempotency key with a different request payload 422
HTTP
 
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."
}


Request is retried while the original request is still being processed 409
HTTP
 
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."
}


Conclusion

Distributed systems are complex in part because if a call to a remote component times out, it’s impossible to know whether it reached the said component. The only option is to repeat the call, but we risk executing a non-idempotent operation twice. In the realm of APIs, we can rely on the Idempotency-Key HTTP Header, an IETF specification currently in draft.

From an architect’s point of view, it makes sense to factor the behavior described in the above sequence diagram into a component, i.e., an API Gateway. In a future post, I’ll try implementing the behavior in Apache APISIX.

API entity POST (HTTP) Requests

Published at DZone with permission of Nicolas Fränkel. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Why and When to Use GraphQL
  • GET, POST, PUT, DELETE: Choosing HTTP Verbs in a Real .NET Core Web API, Not Just by the Textbook
  • A Developer's Guide to Chrome Extension Manifest V3 Declarative Net Request API
  • From Microservices to Agent Services: The Next Architectural Shift

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook