Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Fixing duplicate API requests
Apache APISIX profile imageNicolas Fränkel
Nicolas Fränkel forApache APISIX

Posted on • Originally published atblog.frankel.ch

     

Fixing duplicate API requests

The first rule of distributed systems is "Don’t distribute your system". Designing distributed systems right 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 ofidempotence:

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, andTRACE 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 andPATCH 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 specificationThe Idempotency-Key HTTP Header Field. TheIdempotency-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:

Sequence diagram of the Idempotency Key

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:

DescriptionHTTP status codeSample response
The request doesn't provide the idempotency key for a documented idempotent operation requiring this header400
HTTP/1.1 400 Bad RequestContent-Type: application/problem+jsonContent-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 payload422
HTTP/1.1 422 Unprocessable ContentContent-Type: application/problem+jsonContent-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 processed409
HTTP/1.1 409 ConflictContent-Type: application/problem+jsonContent-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 theIdempotency-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.

To go further:


Originally published atA Java Geek on March 31st, 2024

Top comments(5)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
androaddict profile image
androaddict
  • Joined

Can you tell me one thing our db is located in server part right ?

CollapseExpand
 
nfrankel profile image
Nicolas Fränkel
Dev Advocate | Developer & architect | Love learning and passing on what I learned!
  • Location
    Geneva
  • Work
    Developer Advocate
  • Joined

I don't undestand the "db" part. What do you mean?

CollapseExpand
 
gustavo_priftis_8f1b529a7 profile image
Gustavo Priftis
  • Joined

db stands for databases

Thread Thread
 
nfrankel profile image
Nicolas Fränkel
Dev Advocate | Developer & architect | Love learning and passing on what I learned!
  • Location
    Geneva
  • Work
    Developer Advocate
  • Joined

Yes, I know that "db" stands for database, but I don't understand the question. I mean, I'm not aware of client-side DB, just LocalStorage API on the browser.

CollapseExpand
 
gustavo_priftis_8f1b529a7 profile image
Gustavo Priftis
  • Joined

In this case could be a Redis or something like that probaly

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

More fromApache APISIX

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp