obaid
obaid

Reputation: 361

How to design a database idempotent microservice (POST)

This is a very general and basic question regarding how to design a POST database service (new to implementing microservice). Say you have a SaveCustomer microservice. You so a POST to pass the customer data, and the SaveCustomer service receives the data (JSON) and inserts it into the database.

Due to network congestion etc, the client might retry and send duplicate requests, so how do you ensure that you dont insert duplicate records in the database?

Thanks Obaid

Upvotes: 3

Views: 1273

Answers (1)

Constantin Galbenu
Constantin Galbenu

Reputation: 17683

Although POST is not idempotent you could implement this by assigning each command a unique ID, for example a GUID. Then, on the microservice, before you execute a command, you check that the command was not processed already. If it was, then you ignore it otherwise you send it to the real component that do the real update and mark the command as executed.

This solution is elegant as it can be implemented as an additional component that can decorate the core logic, following the Open/close principle and Single responsibility principle. Additionally, you can use the Dependency injection root configuration to use or not this idem-potency protection.

Upvotes: 2

Related Questions