1

I have 5 instances of account microservice which transfers the money from account A to account B and then updates the data in the Postgres database. My problem is

A user sent five requests to the account service and all of my microservices are working in parallel each request went to all 5 services way but now the user doesn't have enough balance in the account and I am already checking if the user is having enough balance or not.

but after 2 requests user doesn't have enough balance so I am in confusion how to check this and implement data consistency first before the request goes to another instance of the same microservice.

I would love to have suggestions and other approaches also

Vikas
  • 975
  • 1
  • 10
  • 34
  • This should not happen at all! What is your deployment architecture? Irrespective of architecture, I would say, only one instance of your microservice should process the request at a time, and if you are simultaneously overloading requests, the update operation must be `synchronized` (assuming each request is a valid one) But one request must map to only one instance of microservice , other instances may receive 2nd, 3rd requests and treat them fairly ! `If your single request is being replicated to all 5 services` it is a problem then! – Harsh Jun 18 '22 at 07:25

1 Answers1

0

It's hard problem to solve. You can make load balancer use sticky sessions so that single user requests always go to single instance. Either way, you need to make sure db locking is implemented so that at any time only one instance can update one row in a application consistent manner.

nitin
  • 1