Skip to content

Allow setting ReadPreference at a Repository and Query level [DATAMONGO-2103] #2971

Closed
@spring-projects-issues

Description

@spring-projects-issues

João Carvalho opened DATAMONGO-2103 and commented

Currently, setting the ReadPreference can only be done at the MongoTemplate level. This forces us to move down an abstraction layer to specify custom ReadPreferences for our entities and queries.

We would like to be able to specify the ReadPreference for an entity at the Repository and Query level. Our current use case involves a specific entity which can be accessed from a secondary node node in the majority of the use cases, but requires primary reads for other use cases. As our applications scale, it becomes critical to distribute load across all nodes in the MongoDB cluster, and currently, we can either change the global level (thus sacrificing correctness) or providing custom implementations on a repository-by-repository basis.

This could be achieved using an annotation, like such:

@ReadPreference(SECONDARY_PREFERRED)
class FooRepository : CrudRepository<Foo, String> {

  fun findByName(name: String): Foo?

  @ReadPreference(PRIMARY)
  fun strongFindByName(name: String): Foo?

}

In this example, all queries to this entity (both builtin an custom) would use the 'Secondary Preferred' level, except for strongFindByName, which would use the 'Primary' level.

 

 


1 votes, 2 watchers

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions