acid transactions ecommerce

ACID transactions in Couchbase

Couchbase supports distributed multi-document ACID transactions at scale with performance and high availability. Migrate your relational database applications to Couchbase and achieve ACID guarantees while taking advantage of rich SQL support.

A Atomicity Update multiple documents with all-or-nothing guarantee.
C Consistency Automatically maintained consistency for replicas, indexes, and XDCR.
I Isolation Read committed isolation for concurrent readers.
D Durability Data protection under failures with tunable durability levels.

Transactions code sample


In this common debit/credit scenario, Beth is transferring money from her account to Andy's account. Doing it in a transaction ensures that either both Beth's and Andy's accounts are modified, or neither account is modified.

                                        // Transfer money from Beth’s account to Andy’s account -> {
        // get the account documents for Andy and Beth 	
        TransactionJsonDocument andy = txnctx.getOrError(collection, "Andy");
        JsonObject andyContent = andy.contentAsObject();
        int andyBalance = andyContent.getInt("account_balance");
        TransactionJsonDocument beth = txnctx.getOrError(collection, "Beth"); 
        JsonObject bethContent = beth.contentAsObject();
        int bethBalance = bethContent.getInt("account_balance");

        // if Beth has sufficient funds, make the transfer
        if (bethBalance > transferAmount) {
                andyContent.put("account_balance", andyBalance + transferAmount);
                txnctx.replace(andy, andyContent);
                bethContent.put("account_balance", bethBalance - transferAmount);
                txnctx.replace(beth, bethContent);
        else throw new InsufficientFunds();  
        // Commit transaction - if omitted will be automatically committed