How to save message into database and send response into topic eventually consistent?
Here is an example of Try Cancel Confirm pattern https://servicecomb.apache.org/docs/distributed_saga_3/ that should be capable of dealing with your problem. You should tolerate some chance of double submission of the data via the queue. Here is an example:
- Define abstraction Operation and Assign ID to the operation plus a timestamp.
- Write status Pending to the database (you can do this in the same step as 1)
- Write a listener that polls the database for all operations with status pending and older than "timeout"
- For each pending operation send the data via the queue with the assigned ID.
- The recipient side should be aware of the ID and if the ID has been processed nothing should happen.
6A. If you need to be 100% that the operation has completed you need a second queue where the recipient side will post a message ID - DONE. If such consistency is not necessary skip this step. Alternatively it can post ID -Failed reason for failure.
6B. The submitting side either waits for a message from 6A of completes the operation by writing status DONE to the database.
- Once a sertine timeout has passed or certain retry limit has passed. You write status to operation FAIL.
- You can potentialy send a message to the recipient side opertaion with ID rollback.
Notice that all this steps do not involve a technical transactions. You can do this with a non transactional database.
What I have written is a variation of the Try Cancel Confirm Pattern where each recipient of message should be aware of how to manage its own data.