acid transactions ecommerce

ACID database transactions in Couchbase

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

 

 

What is ACID?

 

In a database, ACID is an acronym that describes how to process transactions consistently, securely, and reliably:

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 an ACID-compliant 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

transactions.run((txnctx) -> {
        // 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 
        txnctx.commit();
});