K
Principle 2Transactions(eventual consistency)TransfersThis is the simplest option, and if there is no evidence (see below), it is easier to use.Enough to make a function. doSomethingwhich operates, is performed in the OBD transaction.scala, I don't know, so java examples to illustrate:@Transactional
void doSomething() {
doStep1();
doStep2();
doStep3();
doStep4();
doStep5();
}
If, for example, after step 3, a mistake is made, the transaction will just drop off, and all the footage in the OBD system that has taken steps 1-3 will also be down. It'll look like nothing ever started.If all steps are successfully completed, after step 5, all changes made in the OBD steps will be recorded (to be committed).I mean, either everything that's been taken will stay in the OBD system or nothing is what we need.It's important that decisions from try-catch aren't good. So, catch Not always performed, for example, if the server on which the function was performed at all has collapsed, i.e. burnt, food lost, etc., it is not possible to calculate the clean-up and clean-up if the intermediate steps come in the OBD system, the catch is not possible.With trespassing in the OBD system, even in this case, everything will work properly. The OBD will roll off the transaction and everything.Unfortunately, that doesn't always work. For example:You have more than one OBD.The vault you use does not support the transaction.External services are used (e.g. external ticket service). For example, step 3 needs to challenge the external service via the Internet. It's a potentially long operation, the service can be inaccessible, and it's a long time to keep the transaction open differently.The OBD productivity is not sufficient, i.e. it is a narrow pulp (transactionality needs to be paid with productivity).This situation uses eventual consistency.Eventual consistencyIn this approach, each step is taken separately and remains in effect. If there's been a mistake somewhere, the process is either repeated or reset. But it's all manual.Look at the example:void buyOnline(Cart cart, User user) {
// эта операция помечает товары как зарезервированные
reserveItems(cart);
// эта операция грубо говоря блокирует необходимую сумму в банке
PaymentTransaction payment = blockAmount(cart.getTotalPrice(), user.getPaymentInfo());
if (payment.isSuccess()) {
// эта операция помечает товары как купленные и создает задачу отделу доставки
createShipment(cart.getItems(), user.getAddress());
// эта операция собственно подтверждает банку, что заблокированные средства нужно списать у плательщика
finalizePayment(payment);
} else {
// эта операция отменяет резервацию
cancelReservationForItems(cart);
}
}
Plus, there's a few more things to do, and I'll mention them on my way.Let's see how different error scenarios are handled.If, for example, the server has been well reserved, but then the server has closed down at all, the situation will be handled by a background process that regularly examines and cancels the reserved items if it has been made long enough (i.e. we make it clear that from the reserve to the point of time. createShipment may pass for a maximum of 1 hour.If you managed to block the money, and then it all fell down, there's a process. createShipment + finalizePayment Reset manually, for example, the support service can be made a special button after the user's call. If the user doesn't call at all, the bank will roll over the block in a while if we haven't. finalizePayment♪ On our part, we also need a task that will periodically seek old status orders. payed and remove them, or they can be automatically reset, is a part of the business logic.If we're done, we couldn't. finalizePaymentthis operation will be repeated automatically. I know what the question is, and what if we can never send a confirmation after many attempts? We may have shipped it. In this case, it will be necessary to deal manually with the payment system. It is important to maintain the system ' s confirmation that the payment has been made (this is usually some transaction identifier or there may be payment information on the signed digital signature of the payment system). This acknowledgement is also evidence for such global inaccessibility issues, finalizePayment This is done in blatantly by telephone through support of the payment system.There are cases where it is not possible, in principle because of the limited API, to ensure that two operations (paid + shipment) are completed even with repeated attempts. Or it's impossible to cancel, for example, the e-mail we wrote that the goods are purchased. Or, for example, we might have to ship the goods, but we can't pay.In such cases, we do countervailing. For the unpaid goods shipped, write to the user to pay or return the goods (or bring them to court because he has no proof of payment). For email, we send a new email where we write, I'm sorry, but there's a problem and the goods won't be shipped.