Search code examples
spring-data-couchbase

Can I do a read from replica using spring data couchbase?


For reads, Couchbase recommends checking for certain exceptions and doing a read from a replica (in order to improve availability for operations that happen during a failover, as long as you're OK with possibly stale data).Does spring data provide anything for this? There's no getFromReplica operation exposed that I can find.


Solution

  • Indeed getFromReplica is not exposed in Spring Data. You have to go lower level to do that.

    Most people using spring data expect results to be consistent. So we want the developer to be very aware of when he is making a decision that will impact the consistency level. This is why getFromReplica is not available through Spring Data. And why you have to use the Couchbase bucket object directly. It has to be your decision because it might give you unconsistent result.

    Now this is just for Key/Value Get. If you are using queries, you can tune the consistency level by modifying a property in application.properties:

    # Default level of consistency (read-your-own-writes|eventually-consistent|strongly-consistent|update-after)
    spring.data.couchbase.consistency=read-your-own-writes
    

    Level of consistency are explained in the documentation: http://docs.spring.io/spring-data/couchbase/docs/current/reference/html/#couchbase.repository.consistency