Let's assume i app have with basic flow:
- Client send request to server
- Server do some business logic
- Server puts message into some message queue
- Server returns success to the client
- microservice A reads the message from message queue and process it - be it some email
microservice that generates and sends email to the client , or some audit/logging microservice that process the message and log the data somewher - be it whatever
Now my question is as follow: what happens when our microservice A fails, but we want to have as much uptime as possible and dont want to process data more than once?
The most basic solution ( but without much uptime ) is to have queue that requires confirmation from client before it deletes the message from queue. In that case the flow for microservice would be
- Reads message
- Proces it
- send email/log the data whatever
- confirm to the queue to delete the message
This is however flow without error, if this flow fails in the 2. step, the microservice just restarts and just do whole flow again, since message in the message queue stayed there and wasnt deleted.
Now what happens when the microservice fails after 4th step? Everything is done, email is sent/audit is logged but after that microservice fails and message in queue remains, thus if the microservice restarts, it sees message in queue and process same message twice, if we add some additional DB layer to the flow ( like storing what was processed and what not ) it can still fail before the microservice stores the data into the DB.
Another solution that has better uptime is to have load balancer with multiple instances of microservice A behind it, and these microservices shares the state between them, but as far as im aware, these microservices have to exchange heartbeat and confirm they processed the request from client - but since "processing" here means sending data somewhere, it can process it and then fail , so the heartbeat wont go on, replicated instances gets promoted to the main, reads the message again and process it again and now we are processing it multiple times, which may not be wanted at all in many cases.
I may be out of the loop and my solutions i wrote here may be completely wrong, but how are things like this dealed with? The more i read about distributed systems and so on the more questions i have.
This is not problem i am facing and the system does not have to use message queues/ or the microservices doesnt have to neccessary send email or log data - this is more of general problem question.
Thanks for help and answers!