levibostian / ExpressjsBlanky

Blank Express.js project to get up and running FAST.
MIT License
7 stars 0 forks source link

Add best practice to docs. Prevent duplicate entries. #26

Open levibostian opened 4 years ago

levibostian commented 4 years ago

I had a scenario happen the past couple of weeks with an app of mine. The mobile app would do the following logic:

  1. Send a HTTP POST call to the API. Response came back 200.
  2. The client side app parsed the response.
  3. Client side app marked the task as completed.

If step 1 or 2 fail, step 3 does not happen and the task will be retried.

Here is the problem. The client side app had a bug and step 2 always failed after step 1 was successful. The endpoint required you send in a body like this:

{
  name: "Sue",
  game: {},
  winner: "sue"
}

As you can tell, there is nothing unique here. You can send this body as many times as you want and there is nothing telling the API that this data is a duplicate of another entry. Therefore, the client side app was calling the API 100 times a day and the API would insert a row into the DB 100 times. The API should have instead queried the DB, found a duplicate, and sent back a 200.

Each endpoint that pushes data to the DB should have a way to determine if data is a duplicate or not.