A lot of things could go wrong in these 3 lines of code.
Here's why:
- Database could fail
- Email service could be down
- Event bus could be unavailable
Let's do a thought experiment on what happens when each step fails.
𝟭. 𝗗𝗮𝘁𝗮𝗯𝗮𝘀𝗲 𝗳𝗮𝗶𝗹𝘀
The transaction aborts and nothing ends up in the database.
But also:
- The email isn't sent
- Nor is the message sent over the bus
The system remains in a consistent state.
All good. Right?
𝟮. 𝗗𝗮𝘁𝗮𝗯𝗮𝘀𝗲 𝘀𝘂𝗰𝗰𝗲𝗲𝗱𝘀, 𝗘𝗺𝗮𝗶𝗹 𝗳𝗮𝗶𝗹𝘀
We already store the changes in the database, so that's good.
But we fail to notify the user - this might be something we can live with.
However, we don't publish a message over the bus.
Downstream systems won't be able to react to this event.
The system is no longer in a consistent state.
We have one state in the internal database.
But the external systems don't know about it.
𝟯. 𝗗𝗮𝘁𝗮𝗯𝗮𝘀𝗲 𝗮𝗻𝗱 𝗘𝗺𝗮𝗶𝗹 𝘀𝘂𝗰𝗰𝗲𝗲𝗱, 𝗘𝘃𝗲𝗻𝘁 𝗯𝘂𝘀 𝗳𝗮𝗶𝗹𝘀
We store the data in the database and notify the user via email. All good.
However, the message isn't sent over the bus.
External systems won't be able to react to the event.
This could be bad, depending on how critical processing the event is.
Thankfully, I've encountered this problem before and used a proven technique to solve it.
It's called the Outbox pattern.
It lets you decouple the main database transaction from triggering side effects.
In our example, the side effects are sending an email and publishing an event.
If you're ready to improve the reliability of your system using the Outbox pattern, I have an excellent article you should check out next.
Read it here: https://t.co/S1Y0AhcEZf
---
✍️ Would you use this pattern?
♻️ Reshare if this was helpful.
@ardalis Hey Steve, would love to read your way of implementing auth/access token mechanisms in applications. Please share if you have done already. Thanks.
Tailwind css is quite interesting if used with angular or react. Also if you are someone who has the bad habit of using inline css more often, tailwind is for you.
@theonemule Hello, i would like to know if it is possible to get users who belong to the organization(AD account) and also its consumers authenticated in Azure AD B2C? Also more inputs on the same. Please share your throughts. thanks.