Using Couchbase Transactions

  • how-to
    +
    Distributed ACID Transactions in Couchbase SDKs

    This guide will show you examples of how to perform multi-document ACID (atomic, consistent, isolated, and durable) database transactions within your application, using the Couchbase PHP SDK.

    Refer to the Transaction Concepts material for a high-level overview.

    Prerequisites

    • Couchbase Capella

    • Couchbase Server

    • Couchbase Capella account.

    • You should know how to perform key-value or query operations with the SDK.

    • Your application should have the relevant roles and permissions on the required buckets, scopes, and collections, to perform transactional operations. Refer to the Organizations & Access page for more details.

    • If your application is using extended attributes (XATTRs), you should avoid using the XATTR field txn — this is reserved for Couchbase use.

    • Couchbase Server (6.6.1 or above).

    • You should know how to perform key-value or query operations with the SDK.

    • Your application should have the relevant roles and permissions on the required buckets, scopes, and collections, to perform transactional operations. Refer to the Roles page for more details.

    • If your application is using extended attributes (XATTRs), you should avoid using the XATTR field txn — this is reserved for Couchbase use.

    • NTP should be configured so nodes of the Couchbase cluster are in sync with time.

    Single Node Cluster

    When using a single node cluster (for example, during development), the default number of replicas for a newly created bucket is 1. If left at this default, all key-value writes performed with durability will fail with a DurabilityImpossibleException. In turn, this will cause all transactions (which perform all key-value writes durably) to fail. This setting can be changed via:

    If the bucket already exists, then the server needs to be rebalanced for the setting to take affect.

    Creating a Transaction

    To create a transaction, an application must supply its logic inside a lambda, including any conditional logic required. Once the lambda has successfully run to conclusion, the transaction will be automatically committed. If at any point an error occurs, the transaction will rollback and the lambda may run again.

    try {
      $cluster->transactions()->run(
        function (TransactionAttemptContext $ctx) use ($collection) {
          // Inserting a doc:
          $ctx->insert($collection, 'doc-c', []);
    
          // Getting documents:
          $docA = $ctx->get($collection, 'doc-a');
    
          // Replacing a doc:
          $docB = $ctx->get($collection, 'doc-b');
          $content = $docB->content();
          $newContent = array_merge(
            ["transactions" => "are awesome"],
            $content
          );
          $ctx->replace($docB, $newContent);
    
          // Removing a doc:
          $docC = $ctx->get($collection, 'doc-c');
          $ctx->remove($docC);
    
          // Performing a SELECT N1QL query:
          $selectQuery = 'SELECT * FROM `travel-sample`.inventory.hotel WHERE country = $1 LIMIT 5';
          $qr = $ctx->query(
            $selectQuery,
            TransactionQueryOptions::build()
              ->positionalParameters(["United Kingdom"])
          );
          foreach ($qr->rows() as $row) {
            printf("Name: %s, Country: %s\n", $row["hotel"]["name"], $row["hotel"]["country"]);
          }
    
          // Performing an UPDATE N1QL query:
          $updateQuery = 'UPDATE `travel-sample`.inventory.route SET airlineid = $1 WHERE airline = $2 LIMIT 5';
          $ctx->query(
            $updateQuery,
            TransactionQueryOptions::build()
              ->positionalParameters(['airline_137', 'AF'])
          );
        }
      );
    } catch (\Couchbase\Exception\TransactionFailedException $e) {
      echo "Transaction did not reach commit point: $e\n";
    } catch (\Couchbase\Exception\TransactionCommitAmbiguousException $e) {
      echo "Transaction possibly committed: $e\n";
    }

    The transaction lambda gets passed a TransactionAttemptContext object — generally referred to as ctx in these examples. Since the lambda could be rerun multiple times, it is important that it does not contain any side effects. In particular, you should never perform regular operations on a Collection, such as collection.insert(), inside the lambda. Such operations may be performed multiple times, and will not be performed transactionally. Instead, you should perform these operations through the ctx object, e.g. ctx.insert().

    The result of a transaction is represented by a TransactionResult object, which can be used to expose debugging and logging information to help track what happened during a transaction.

    In the event that a transaction fails, your application could run into the following errors:

    • TransactionCommitAmbiguousException

    • TransactionFailedException

    Refer to Error Handling for more details on these.

    Logging

    To aid troubleshooting, raise the log level on the SDK.

    Please see the PHP SDK logging documentation for details.

    Key-Value Operations

    You can perform transactional database operations using familiar key-value CRUD methods:

    • Create - insert()

    • Read - get()

    • Update - replace()

    • Delete - remove()

    As mentioned previously, make sure your application uses the transactional key-value operations inside the lambda — such as ctx.insert(), rather than collection.insert().

    Insert

    To insert a document within a transaction lambda, simply call ctx.insert().

    $cluster->transactions()->run(
      function (TransactionAttemptContext $ctx) use ($collection) {
        $adoc = $ctx->insert($collection, "docId", []);
      }
    );

    Get

    From a transaction context you may get a document:

    $cluster->transactions()->run(
      function (TransactionAttemptContext $ctx) use ($collection) {
        $docA = $ctx->get($collection, "doc-a");
      }
    );

    If the document does not exist, the transaction will fail with a TransactionFailedException (after rolling back any changes, of course).

    Gets will "Read Your Own Writes", e.g. this will succeed:

    $cluster->transactions()->run(
      function (TransactionAttemptContext $ctx) use ($collection) {
        $docId = 'docId';
        $ctx->insert($collection, $docId, []);
    
        $doc = $ctx->get($collection, $docId);
      }
    );

    Replace

    Replacing a document requires a $ctx→get() call first. This is necessary so the SDK can check that the document is not involved in another transaction, and take appropriate action if so.

    $cluster->transactions()->run(
      function (TransactionAttemptContext $ctx) use ($collection) {
        $doc = $ctx->get($collection, "doc-b");
        $content = $doc->content();
        $newContent = array_merge(
          ["transactions" => "are awesome"],
          $content
        );
    
        $ctx->replace($doc, $newContent);
      }
    );

    Remove

    As with replaces, removing a document requires a $ctx→get() call first.

    $cluster->transactions()->run(
      function (TransactionAttemptContext $ctx) use ($collection) {
        $doc = $ctx->get($collection, "docId");
        $ctx->remove($doc);
      }
    );

    SQL++ Queries

    If you already use SQL++ (formerly N1QL), then its use in transactions is very similar. It returns the same QueryResult you are used to, and takes most of the same options.

    As mentioned previously, make sure your application uses the transactional query operations inside the lambda — such as ctx.query(), rather than cluster.query() or scope.query().

    Here is an example of selecting some rows from the travel-sample bucket:

    $cluster->transactions()->run(
      function (TransactionAttemptContext $ctx) {
        $st = "SELECT * FROM `travel-sample`.inventory.hotel WHERE country = $1";
        $qr = $ctx->query(
          $st,
          TransactionQueryOptions::build()
            ->positionalParameters(["United Kingdom"])
        );
    
        foreach ($qr->rows() as $row) {
          // do something
        }
      }
    );

    And an example combining SELECT and an UPDATE.

    $cluster->transactions()->run(
      function (TransactionAttemptContext $ctx) use ($hotelChain, $country) {
        // Find all hotels of the chain
        $qr = $ctx->query(
          'SELECT reviews FROM `travel-sample`.inventory.hotel WHERE url LIKE $1 AND country = $2',
          TransactionQueryOptions::build()
            ->positionalParameters([$hotelChain, $country])
        );
    
        // This function (not provided here) will use a trained machine learning model to provide a
        // suitable price based on recent customer reviews.
        function priceFromRecentReviews(Couchbase\QueryResult $qr)
        {
          // this would call a trained ML model to get the best price
          return 99.98;
        }
        $updatedPrice = priceFromRecentReviews($qr);
    
        // Set the price of all hotels in the chain
        $ctx->query(
          'UPDATE `travel-sample`.inventory.hotel SET price = $1 WHERE url LIKE $2 AND country = $3',
          TransactionQueryOptions::build()
            ->positionalParameters([$updatedPrice, $hotelChain, $country])
        );
      }
    );

    As you can see from the snippet above, it is possible to call regular PHP functions from the lambda, permitting complex logic to be performed. Just remember that since the lambda may be called multiple times, so may the method.

    Like key-value operations, queries support "Read Your Own Writes". This example shows inserting a document and then selecting it again:

    $cluster->transactions()->run(
      function (TransactionAttemptContext $ctx) {
        // Query INSERT
        $ctx->query(
          "INSERT INTO `travel-sample`.inventory.airline VALUES ('doc-c', {'hello':'world'})" (1)
        );
    
        // Query SELECT
        $ctx->query(
          "SELECT hello FROM `travel-sample`.inventory.airline WHERE META().id = 'doc-c'" (2)
        );
      }
    );
    1 The inserted document is only staged at this point, as the transaction has not yet committed.
    Other transactions, and other non-transactional actors, will not be able to see this staged insert yet.
    2 But the SELECT can, as we are reading a mutation staged inside the same transaction.

    Query Options

    Query options can be provided via TransactionQueryOptions, which provides a subset of the options in the PHP SDK’s QueryOptions.

    $cluster->transactions()->run(
      function (TransactionAttemptContext $ctx) {
        $txQo = TransactionQueryOptions::build()
          ->readonly(false)
          ->positionalParameters(["key", "value"]);
    
        $ctx->query(
          "UPSERT INTO `travel-sample`.inventory.airline VALUES ('docId', {\$1:\$2})",
          $txQo
        );
      }
    );
    Table 1. Supported Transaction Query Options
    Name Description

    positionalParameters(array<string|int, mixed>)

    Allows to set positional arguments for a parameterized query.

    namedParameters(array<string|int, mixed>)

    Allows you to set named arguments for a parameterized query.

    scanConsistency(string)

    Sets a different scan consistency for this query.

    clientContextId(string)

    Sets a context ID returned by the service for debugging purposes.

    scanWaitMilliseconds(int)

    Allows to specify a maximum scan wait time.

    scanCap(int)

    Specifies a maximum cap on the query scan size.

    pipelineBatch(int)

    Sets the batch size for the query pipeline.

    pipelineCap(int)

    Sets the cap for the query pipeline.

    profile(int)

    Allows you to enable additional query profiling as part of the response.

    readonly(bool)

    Tells the client and server that this query is readonly.

    adHoc(bool)

    If set to false will prepare the query and later execute the prepared statement.

    raw(string)

    Escape hatch to add arguments that are not covered by these options.

    Mixing Key-Value and SQL++

    Key-Value operations and queries can be freely intermixed, and will interact with each other as you would expect.

    In this example we insert a document with a key-value operation, and read it with a SELECT query.

    $cluster->transactions()->run(
      function (TransactionAttemptContext $ctx) use ($collection) {
        // Key-Value insert
        $ctx->insert($collection, "doc-greeting", ["greeting" => "Hello World"]); (1)
    
        // Query SELECT 
        $selectQuery = "SELECT greeting FROM `travel-sample`.inventory.airline WHERE META().id = 'doc-greeting'";
        $ctx->query($selectQuery); (2)
      }
    );
    1 The key-value insert operation is only staged, and so it is not visible to other transactions or non-transactional actors.
    2 But the SELECT can view it, as the insert was in the same transaction.

    Configuration

    Transactions can optionally be globally configured when configuring the Cluster. For example, if you want to change the level of durability which must be attained, this can be configured as part of the connect options:

    $CB_USER = getenv('CB_USER') ?: 'Administrator';
    $CB_PASS = getenv('CB_PASS') ?: 'password';
    $CB_HOST = getenv('CB_HOST') ?: 'couchbase://localhost';
    
    $options = new ClusterOptions();
    $options->credentials($CB_USER, $CB_PASS);
    
    $transactions_configuration = new TransactionsConfiguration();
    $transactions_configuration->durabilityLevel(Couchbase\DurabilityLevel::PERSIST_TO_MAJORITY);
    $options->transactionsConfiguration($transactions_configuration);
    
    $cluster = new Cluster($CB_HOST, $options);

    The default configuration will perform all writes with the durability setting Majority, ensuring that each write is available in-memory on the majority of replicas before the transaction continues. There are two higher durability settings available that will additionally wait for all mutations to be written to physical storage on either the active or the majority of replicas, before continuing. This further increases safety, at a cost of additional latency.

    A level of None is present but its use is discouraged and unsupported. If durability is set to None, then ACID semantics are not guaranteed.

    Additional Resources