If there are 2 bounded contexts: Spending
and Account management
, how to prevent so the user doesn't spend more than he has in balance?
I understand the case when we have Order > Payment > Shipment
sequence for a single product, but here it's more like Order > Balance Check > Shipment
and i don't understand how to check the balance in other bounded context and be sure user would never spend more than he has.
Is this situation even possible to solve with eventual consistency? I would prefer it with eventual consistency. If not, is the safe option to keep balance within the Spending
bounded context?
[UPDATE] I haven't mentioned, I'm thinking about the solution in event driven architecture where bounded contexts exchange information through events only.
In your case, eventual consistency for a balance check is not suitable.
Requirement: Do not ship goods until we know customer can pay.
Therefore you have a firm business requirement that the shipment process must wait for a response from the account balance check process. If balance check service is down then shipment cannot proceed.
In other business scenarios you may be able to continue with one part of the process and let the other part work on eventual consistency with an out-of-process message delivery in between the services. In your case, you cannot do this for the check balance part of the process.
To further complicate, your process would be:
Order > Check Balance > Ship > Deduct Funds.
You don't want to deduct the funds until shipment has occurred in case there is failure in shipment for some reason, but you definitely don't want to ship before the balance has been checked.
For this, I'd introduce the concept of an 'earmark' or 'reserved funds'.
So, your "Spending" context will send a 'reserve funds' request to "Account Manager" context and wait for response. That response can include a correlation id of the 'reservation of funds'. Your Account Management service would need to understand the concepts of "actual balance" and "reserved funds" to calculate an "available balance".
Once your shipment has completed you can then send a 'confirmation' to account management quoting the correlation id, so that account management can adjust "actual balance" and delete the "reserved funds". That step, in my opinion, could work with eventual consistency.