Spring couchbase (multiple buckets)


I am using spring boot(2.0.0.RELEASE) with Spring 5, JDK 1.8 and spring data couchbase and reactive api.

I have one active bucket and one backup bucket(the records are pushed from active to backup every hour).

The issue is that I need to switch between the active/backup buckets based on a flag without restarting of the server and each repository bean should be mapped to both buckets(as backup is the replica of active).

Is it possible ?


Hi @himanshu.mps, I haven’t tested this, but is it possible for you to create two Spring Data repositories, connected to two different Couchbase configs (one for each bucket), and then in your code you choose which repository to use depending on your flag?


This will lead to same repo code being done 2 times which I am not in favor of. What I am looking for is something on the lines of

  Use this template for the repo
  Use default template for the repo

I am also thinking that if I need above mentioned functionality, then I am better of with not using spring repo (ReactiveCouchbaseSortingRepository, ReactiveCouchbaseRepository, ReactiveSortingRepository, ReactiveCrudRepository & …) and rather use RxJavaCouchbaseTemplate to get the data that I need. Anyone thinks/knows a better/cleaner/faster approach than that ?

@himanshu.mps you’d need to declare the same functions in both repos, true, but could the function bodies not be factored out and shared?

Hi @graham.pople

How do I link a spring repo with a particular couchbase template ?


Hi @himanshu.mps,

You can configure the bean repositoryOperationsMapping on the configuration, override the method here in the configuration and the mapping for the operations (template) and repository can be added.

Hi, I’m new to couchbase. @himanshu.mps Did you try this and could you please tell me whether these buckets belong to different clusters or same cluster?

In my case they were on different cluster