Scan Consistency Using the .NET SDK with Couchbase Server

    +
    You can balance performance against consistency in N1QL queries via the Couchbase .NET client and the AtPlus option.

    Setting a Scan Consistency, lets you choose between NotBounded (the default), for sharpest performance; RequestPlus, for strongest consistency; and AtPlus, for a good balance between increased performance and completeness of results.

    == Request_Plus

    If consistency is more important than perforance, the REQUEST_PLUS ensures that all indexes are updated before a query is completed. It also has the advantage of being simpler to use than the Read Your Own Writes (RYOW) option of AT_PLUS — below.

            {
                // get the current count
                var result1 =
                    _bucket.Query<dynamic>("SELECT COUNT(1) as airportCount FROM `travel-sample` WHERE type=&apos;airport&apos;")
                        .Rows.First();
                Console.WriteLine($"Initial count: {result1.airportCount}");
    
                // insert a new airport
                var doc = new Document<dynamic>
                {
                    Id = "ScanConsistency::airport::" + _random.Next(10000),
                    Content = new
                    {
                        type = "airport"
                    }
                };
                _bucket.Insert(doc);
    
                // get the count again
                var request =
                    QueryRequest.Create("SELECT COUNT(1) as airportCount FROM `travel-sample` WHERE type='airport'");
                request.ScanConsistency(ScanConsistency.RequestPlus);
                var result2 = _bucket.Query<dynamic>(request).Rows.First();
                Console.WriteLine($"Count after insert with RequestPlus: {result2.airportCount}");
            }

    == At_Plus

    In order to use at_plus, you’ll need to make sure that you explicitly set UseEnhancedDurability to true when setting up your ClientConfiguration:

    config.BucketConfigs = new Dictionary<string, BucketConfiguration> {
        {
            "travel-sample", new BucketConfiguration
            {
                UseEnhancedDurability = true
            }
        }
    };

    In the example below, the N1QL query will wait only for the new document to be indexed.

    Setting at_plus consistency
    private static void AtPlusExample()
    {
        Console.WriteLine("========= AtPlus");
    
        // get the current count
        var getResult = _bucket.Query<dynamic>("SELECT COUNT(1) as airportCount FROM `travel-sample` WHERE type='airport'")
                .Rows.First();
        Console.WriteLine($"Initial count: {result1.airportCount}");
    
        // insert a new airport
        var doc = new Document<dynamic>
        {
            Id = "ScanConsistency::airport::" + _random.Next(10000),
            Content = new
            {
                type = "airport"
            }
        };
        var insertResult = _bucket.Insert(doc);
    
        // get the count again, creating mutation state from insert result
        var state = MutationState.From(insertResult.Document);
        var request = new QueryRequest("SELECT COUNT(1) as airportCount FROM `travel-sample` WHERE type='airport'")
            .ConsistentWith(state);
        var queryResult = _bucket.Query<dynamic>(t).Rows.First();
        Console.WriteLine($"Count after insert with AtPlus: {result2.airportCount}");
    }