How to work around the lack of transactions in MongoDB?

后端 未结 10 593
梦毁少年i
梦毁少年i 2020-11-27 09:19

I know there are similar questions here but they are either telling me to switch back to regular RDBMS systems if I need transactions or use atomic operations or two-phase c

相关标签:
10条回答
  • 2020-11-27 09:46

    As of 4.0, MongoDB will have multi-document ACID transactions. The plan is to enable those in replica set deployments first, followed by the sharded clusters. Transactions in MongoDB will feel just like transactions developers are familiar with from relational databases - they'll be multi-statement, with similar semantics and syntax (like start_transaction and commit_transaction). Importantly, the changes to MongoDB that enable transactions do not impact performance for workloads that do not require them.

    For more details see here.

    Having distributed transactions, doesn't mean that you should model your data like in tabular relational databases. Embrace the power of the document model and follow the good and recommended practices of data modeling.

    0 讨论(0)
  • 2020-11-27 09:46

    Now what's the problem with that? MongoDB can do atomic updates only on one document. In the previous flow it could happen that some kind of error creeps in and the message gets stored in the database but the user's balance is not gets reduced and/or the transaction is not gets logged.

    This is not really a problem. The error you mentioned is either a logical (bug) or IO error (network, disk failure). Such kind of error can leave both transactionless and transactional stores in non-consistent state. For example, if it has already sent SMS but while storing message error occurred - it can't rollback SMS sending, which means it won't be logged, user balance won't be reduced etc.

    The real problem here is the user can take advantage of race condition and send more messages than his balance allows. This also applies to RDBMS, unless you do SMS sending inside transaction with balance field locking (which would be a great bottleneck). As a possible solution for MongoDB would be using findAndModify first to reduce the balance and check it, if it's negative disallow sending and refund the amount (atomic increment). If positive, continue sending and in case it fails refund the amount. The balance history collection can be also maintained to help fix/verify balance field.

    0 讨论(0)
  • 2020-11-27 09:48

    Transactions are absent in MongoDB for valid reasons. This is one of those things that make MongoDB faster.

    In your case, if transaction is a must, mongo seems not a good fit.

    May be RDMBS + MongoDB, but that will add complexities and will make it harder to manage and support application.

    0 讨论(0)
  • 2020-11-27 09:49

    This is late but think this will help in future. I use Redis for make a queue to solve this problem.

    • Requirement:
      Image below show 2 actions need execute concurrently but phase 2 and phase 3 of action 1 need finish before start phase 2 of action 2 or opposite (A phase can be a request REST api, a database request or execute javascript code...).

    • How a queue help you
      Queue make sure that every block code between lock() and release() in many function will not run as the same time, make them isolate.

      function action1() {
        phase1();
        queue.lock("action_domain");
        phase2();
        phase3();
        queue.release("action_domain");
      }
      
      function action2() {
        phase1();
        queue.lock("action_domain");
        phase2();
        queue.release("action_domain");
      }
      
    • How to build a queue
      I will only focus on how avoid race conditon part when building a queue on backend site. If you don't know the basic idea of queue, come here.
      The code below only show the concept, you need implement in correct way.

      function lock() {
        if(isRunning()) {
          addIsolateCodeToQueue(); //use callback, delegate, function pointer... depend on your language
        } else {
          setStateToRunning();
          pickOneAndExecute();
        }
      }
      
      function release() {
        setStateToRelease();
        pickOneAndExecute();
      }
      

    But you need isRunning() setStateToRelease() setStateToRunning() isolate it's self or else you face race condition again. To do this I choose Redis for ACID purpose and scalable.
    Redis document talk about it's transaction:

    All the commands in a transaction are serialized and executed sequentially. It can never happen that a request issued by another client is served in the middle of the execution of a Redis transaction. This guarantees that the commands are executed as a single isolated operation.

    P/s:
    I use Redis because my service already use it, you can use any other way support isolation to do that.
    The action_domain in my code is above for when you need only action 1 call by user A block action 2 of user A, don't block other user. The idea is put a unique key for lock of each user.

    0 讨论(0)
  • 2020-11-27 09:51

    Check this out, by Tokutek. They develop a plugin for Mongo that promises not only transactions but also a boosting in performance.

    0 讨论(0)
  • 2020-11-27 09:54

    The project is simple, but you have to support transactions for payment, which makes the whole thing difficult. So, for example, a complex portal system with hundreds of collections (forum, chat, ads, etc...) is in some respect simpler, because if you lose a forum or chat entry, nobody really cares. If you, on the otherhand, lose a payment transaction that's a serious issue.

    So, if you really want a pilot project for MongoDB, choose one which is simple in that respect.

    0 讨论(0)
提交回复
热议问题