Outbox Pattern
When you use message brokers you might lose your messages. When you lost your messages customer operations don’t work properly. When they can’t their operations you might lose your customers.
If you don’t want to lose your messages store them in the database. You can check your message table regularly (hosted service, worker service, etc.) and publish them. After publishing you can mark the database record as published. It’s the usual way.
Outbox Pattern Step by step:
- Do something. (Create a new user, send a message, etc.)
- Create an event
- Store event to the database
- Read database regularly and publish messages
- After consuming mark messages as consumed
You can review this diagram:
I don’t want to use it like this. I changed this a little bit. After saving messages to the database I publish messages. They’re consuming and updating as consumed. When message publisher service is working it’s reading unconsumed messages. It’s publishing them and updating them as consumed.
You can review the customized outbox pattern diagram here:
I wanted to write code basically.
The message model is here:
You can see saving and publishing events steps in this code block:
When a published event is consuming, it’s updating as consumed. You can see it here:
PutMessageAsProcessed method is updating messages as consumed. It’s adding process time for the consumed message.
Okay. It’s time to check unconsumed messages and publish them. I created a hosted service for this. It’s working regularly.
PublishMissingMessages method runs once in ten minutes. The method is here:
CreatedOn < DateTime.UtcNow.AddHours(-1) && !m.ProcessTime.HasValue
This expression checks unconsumed messages in the last one hour. If it finds any message it publishes them again.
You can review the repository from this link.
If you’re thinking it’s not a good solution, I would like to know this. You can text.