Deepayan Ghosh
Deepayan Ghosh

Reputation: 185

Should all POST APIs be idempotent or it depends on use-case?

I am new to designing APIs and seeking guidance from all. I read about idempotency and I want to understand whether it is always required or it depends on the whether we are okay with having duplicate resources?

I am designing a multi-page form where a customer will input information in multiple pages and can navigate between pages using Previous/Next buttons. The final page will have multiple file input fields and against each the customer will upload a file. After successful upload, he/she will submit the form.

To model this flow I have created a class CustomerInfo which holds all the information entered by the customer. For the file upload part, CustomerInfo has a list of document ids. When a file is uploaded, the API uploads the document to s3 and creates an entry into a table which holds information regarding the document like document type and generates a new document id. This document id is also the key of the S3 object. I store this document id in CustomerInfo object.

So does the API handling the document upload need to be idempotent? Is it okay to have multiple entries? Or, Should it create one single entry in the table and for request with same customerId and documentType, it should overwrite the previous document in S3 and update that same table record and not create a new record?

Lets think of a scenario where for one input field, the customer uploads a file and then re-uploads another file, or the first upload failed and the customer reties the upload, then multiple entries will be created in the document table but it does not matter to me if I have duplicate records, as long as I am able to store the final document id correctly on the CustomerInfo object. Or, in this case also, do I need to implement some kind of idempotency so that only one document id is generated for a particular customer and document type and in case of re-uploads and retries, I overwrite this one entry?

Upvotes: 0

Views: 130

Answers (0)

Related Questions