Hibernation Configuration for Balancing MongoDB

I have a small mongoDB cluster with 3 nodes (no shards, only replication). Now inserting into the primary node propagates the new data to the secondary node, as expected (basic replication). I am using java and hibernate.

Now I want to load read request balancing among the entire set of replicas instead of the primary node, which is always used to serve data. Is there a way that I tell hibernate (via the query string) about the available servers and somehow hibernate distributes the request (randomly or in a systematic way)? What will be the right way to balance the load?

+4
source share
1 answer

The setting you are looking for is called read-preference. If you look at the doc here , you will find:

hibernate.ogm.mongodb.read_preference

Specifies ReadPreference to use when reading data in a MongoDB data warehouse. Possible settings (ReadPreferenceType enumeration values): PRIMARY, PRIMARY_PREFERRED, SECONDARY, SECONDARY_PREFERRED and NEXT.

In this case, you are likely to use SECONDARY_PREFERRED, which in fact means that the read operation will by default be redirected to the subordinate nodes, but sleep mode will return to primary if it is the only available node.

+1
source

Source: https://habr.com/ru/post/1630042/


All Articles