What is the best way to know hard failover of primary node?

Following is what I have been doing to serve the document from replica if primary node hard fail. Works well but the replica read happens only after kvTimout hits resulting TimeOutException. I am not convince with this solution and wonder if there is quicker way to know if primary node has failed for docId so that I can fallback to replica sooner rather waiting for timeout. Appreciate any suggestion :slight_smile:

try {
return bucket.get(docId).toBlocking().first();
} catch (RuntimeException e) {
return bucket.getFromReplica(docId, ReplicaMode.FIRST, JsonDocument.class, environment.kvTimeout(), TimeUnit.MILLISECONDS) .toBlocking() .first();
}
return null;

Hi Rupesh,

I’m not sure if version 2.x of the Java SDK provides a good way to deal with this kind of situation. One possible solution would be to apply the Circuit Breaker design pattern so requests to unresponsive nodes can be made to fail quickly instead of timing out.

Java SDK 3.0 (now in beta) has built-in circuit breakers. I’m afraid the documentation for this feature isn’t available yet, I’m pretty sure the code is there. Might be worth taking a look.

Thanks,
David

1 Like

Thanks David, I able to use resilence4j to apply CB, but found the side effect… circuit get opened for every request :smile: I can’t find or think the way to break circuit only for the doc request whose primary node is failed. As a result every request is being retrieve from replica. :frowning:
I am also wondering if there is any events I can consume to know when the node recover back. We see there is continuous retry attempt to connect socket every 4 sec. Is this something I can tap into? either I believe my issue will still be same… for a given doc Id, there is no way if the primary node is down unless I sent the request to cb