Search code examples
javasdkcouchbasecouchbase-java-apijava-client

While Migrating from java client SDK2 to SDK 3, how can i implement RetryStrategy


I am migrating from couchbase java client SDK 2 to SDK3. in SDK2 code I have a method which is calling upsert method on AsyncBucket with retry mechanism.

below is the code [SDK2]

client.getBucket().async().upsert(doc).retryWhen(getRetryFunction()); // client.getBucket() return object of Bucket

The above mentioned code snippet defines a retry mechanism for handling specific Couchbase exceptions using the RetryBuilder class. This mechanism is part of the getRetryFunction method

getRetryFunction() is implemented like below

public RetryWhenFunction getRetryFunction()
    {

       return RetryBuilder.anyOf(TemporaryFailureException.class, TemporaryLockFailureException.class, BackpressureException.class, ReplicaNotAvailableException.class, RequestCancelledException.class, CouchbaseOutOfMemoryException.class)
      .delay(((CouchbaseRetryStrategy) retryStrategy)
      .determineRetryStrategy())
      .max(retryStrategy.getMaxAttempts())
      .build();
    }

// ((CouchbaseRetryStrategy) retryStrategy).determineRetryStrategy(), it returns object of Delay

This code sets up a retry mechanism that will attempt to retry operations when specific Couchbase exceptions occur, using a defined delay strategy and a maximum number of attempts.

Now I am migerating to SDK3 so I want similar behaviour as above code.

I am not able to find the retry mechanism in SDK3. my expectation is to write a code which will be behaving same.


Solution

  • One of the major changes in Couchbase SDK3 was that the SDK now retries automatically when it is safe to do so.

    We do provide a RetryStrategy interface for users that want to customise the behaviour, and please see this link for details on that.

    But almost all users are satisfied with the SDK out-of-the-box retry behaviour, which is implemented in the BestEffortRetryStrategy.