Ask a Question

Ask a Question
Back to All

How do we ensure transaction consistency across multiple operations?

Hello,

We are currently working on a scenario where we need to update user currency and perform operations on our Storage Objects within a single API call. It is crucial for us to ensure that both operations either succeed together or fail together, maintaining consistency.

Could you please guide us on how to handle this situation within Beamable microservices? Specifically, we are looking for the best practices or mechanisms provided by Beamable to ensure transactional consistency across multiple operations.

Thank you in advance for your assistance.

Admin

Hello,

We do not have a way to enforce transactions across multiple services. Within Beamable Inventory, each Update call is guaranteed to be transactional, and likewise in Micro Storage you can lean on MongoDB's transactional features. However, when using both in tandem, things get more complicated.
What you would have to do, if you want transactions across Inventory and Micro Storage, is build your own transaction-and-rollback system using C#MS for the logic and Micro Storage as the informational authority on transaction metadata. The rollback portion of things might be the most complex to reason about and test thoroughly: you could roll back inventory operations by essentially doing the opposite of a past operation: deduct currency and delete items. In the Micro Storage side of things, you have a bit more leeway; you can use techniques like optimistic locking, MongoDB's transaction support, and so on. You would still need enough C#MS logic to glue everything together and do stuff like, "if currency.coins properties indicate lastTransaction=X but the journal in MongoDB lacks transaction X, rectify it by doing Y and Z"
Ultimately, it may be simplest if you stick to one or the other (Inventory or Micro Storage) as your source of truth, if that is possible.

I forget whether I mentioned it before, but you might also find some use in Federated Inventory: https://docs.beamable.com/docs/inventory-federation
If you have a way of reliably establislhing ownership via external service, or even logic you can express in C#MS code, you could use inventory federation to keep things in sync.