3

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

obaid
  • 361
  • 3
  • 10
  • `POST` is not supposed to be [idempotent](http://restcookbook.com/HTTP%20Methods/idempotency/) – salparadise Sep 19 '17 at 05:11
  • Ok, so how should I then implement a service that insert record into database? Any recommendations, since I am new to microservices – obaid Sep 25 '17 at 04:23

1 Answers1

2

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.

Constantin Galbenu
  • 16,951
  • 3
  • 38
  • 54
  • Thanks for your response, is there any resource/example/tutorial which could show how usually people implement such a service (rest service inserting db records), showing how to implement such a service and how to call this service from another service (meaning handling retries etc). – obaid Sep 25 '17 at 04:02
  • My scenario: I have a microservice that has to insert a record after processing, however I don't want the user/client to experience any latency (which sometimes might be the case if DB is overwhelmed) while calling this service. Hence I am thinking to make the inserts asynchronous i.e send insert statements to a queue (Kafka) and process them through a consumer client. Is this approach a good solution? Is there any other solution where I don't loose data and don't have to degrade the user experience due to database performance issues? – obaid Sep 25 '17 at 04:22
  • 1
    @obaid Using a command queue is a solution. Other solution could be to use an Event-driven architecture. So, your client service should publish an event. Then a Saga/Process manager should pickup (by subscribing or polling) that event and issue a command to the next microservice, with retrying if something goes wrong. This architecture is better but implies also big refactoring because your microservices should be split by the domain/sub-domain. You should take a look at the DDD approach (Domain driven design) that splits a monolith by bounded context. – Constantin Galbenu Sep 25 '17 at 06:32