I'm trying to flesh out an eCommerce system using microservices (.NET Core and Kubernetes), event sourcing (Kafka), and CQRS. The particular use case I've been thinking about is as follows.
There is an inventory microservice designed to use CQRS. Updates are fed into Kafka as events, which the inventory microservice consumes to update its materialized view, and reads are executed directly against the materialized view. The issue I'm trying to work through is how to handle orders. In my current design, orders are created in an order microservice which then emits events which the inventory microservice would consume and deduct the inventory that was part of the order. However, there's a race condition here. It's possible for another buyer to buy the same products before the inventory from the previous order can be deducted.
How does one handle this type of transactional operation that spans microservices? I've read that one should verify stock against the stream instead of the materialized view (as the stream is the actual source of truth), but I'm a little fuzzy on how that would be practical (as the stream might be huge). If I'm going to do that then why even have the materialized view if I can't trust it?