Transactions in NoSQL?

前端 未结 11 986
长情又很酷
长情又很酷 2020-12-22 18:39

I\'m looking into NoSQL for scaling alternatives to a database. What do I do if I want transaction-based things that are sensitive to these kind of things?

11条回答
  •  不思量自难忘°
    2020-12-22 19:36

    The problem with one transaction and two operations (for example one pay $5,000, second receive $5,000) - is that you have two accounts with same priority. You cannot use one account to confirm second (or in reverse order). In this case you can guaranty only one account will be correct (that is confirmed), second (that confirm) may have fails. Lets look why it can fails (using message aproatch, sender is confirmed by receiver):

    1. Write +$5,000 to receiver account
    2. If success - write -$5,000 to sender account
    3. If fails - try againt or cancel or show message

    It will guaranty save for #1. But who guaranty if #2 fails? Same for reverse order.

    But this is possible to implements to be safe without transactions and with NoSQL. You are always allowed use third entity that will be confirmed from sender and receiver side and guaranty your operation was performed:

    1. Generating unique transaction id and creating transaction entity
    2. Write +$5,000 to receiver account (with reference to transaction id)
    3. If success - set state of transaction to send
    4. Write -$5,000 to sedned account account (with reference to transaction id)
    5. If success - set state of transaction to receive

    This transaction record will guaranty that is was ok for send/receive massages. Now you can check every message by transaction id and if it has state received or completed - you take it in account for user balance.

提交回复
热议问题