简体   繁体   中英

Correct way to POST data in database through REST API request from one server to another?

I want to create an entry in my database. That has to be done by one of my microservice. But the request is received from the frontend in a different microservice. Now microservices communicate using REST API calls. I've read that in java if you want to make a request to a different microservice from a microservice, you have to make the request in a separate thread and impose timeouts. The same thing in python, you have to impose timeouts.

But what if my request is successful but the request which I made times out in the thread? Suppose this is a critical insert into the database. And what if all the retries (suppose 3 retries) timeout because of network delay but all were successful insert operations. Then suppose in the example as in the picture a user places cash on delivery order, the order service updates the database with the new order, then order service makes an HTTP post request to payment service to add an entry into the database for payment history. Now as I described the case, there will be 3 duplicate entries in the payment table for each retry.

How do we handle this issue, isn't checking for duplicate entry expensive operation before each insert?

I mostly worked on the frontend but now I want to switch to the backend. This issue was in my head all the time please help!

Please have a look at this diagram for more clear understanding of the flow

you can send asynchronous calls , but in this case you will not be aware of the Success or Failure response.

For duplicate entries, you can add validation check on the receiving end to skip the insert if record already exists in db

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM