Couchbase 2.1.0 Enterprise with Asynch Operation


Hello All,

We are using Couchbase 2.1.0 enterprise version with Java Couchbase Client 1.1.6 version. I am very new to couch base. We are using couchbase for document caching in a replicated environment.

When we are going through the API documentation, i observed that, the set, replace etc methods are asynchronous with an option of PersistTo and ReplicateTo. I believe the moment we provide these parameters, the operation will wait till it satisfies the PersistTo and ReplicateTo factors specified. Can you please help me understand the need for having these operations Asynchronous in such scenarios? Appreciate your inputs.




So you are correct the operations are asynchronous, even when you put a durability constraints in the call. (PersistTo, ReplicateTo parameters). Let me explain it.

If you look at the signature of the method, it returns an OperationFuture (a Java Future object). This means when you call this methods nothing is blocked or waits.

Then you can check what is the status of this asynchronous call using the OperationFuture, and you are right when you do this this is a blocking operation.

So what is the interest of this, look at this “code snippet”:

OperationFuture op = cb.set("mykey", myDocument, PersistTo.ONE, ReplicateTo.ONE);

// do some other operation or treatment here
// …

// then check the status
if (! op.getStatus().isSuccess()) {
System.out.println(“Key: key:”+ op.getKey() +" failed due to “+ op.getStatus().getMessage() +”. Please retry" );

So the benefits is that you can run the operation, and in the “same time” do other things, and then check the status of the operation.

Since the OperationFuture.get() or getStatus is synchronous, if you want to save the data in a synchronous way you can write

cb.set(“mykey”, myDocument, PersistTo.ONE, ReplicateTo.ONE).get();

but as you can guess it could have an impact on your application.

A pattern that we see quite often is a program where you inject execute many asynchronous operations. You keep a reference, for example in a list, to all the operationFuture objects, and then you check the status.

Let me know if you need more information