Skip to content

Force read queries redirect to replicas for real

In gitlab-org/gitlab!56476 (merged), we introduce use_replica_if_possible so that all ambiguous queries (transaction, custom queries, etc.) are redirected to the replicas. That name is quite misleading. The new interface is defensive, it obeys automatic primary stickiness after a write and manual primary stickiness (use_primary). It means that the inside queries work the same as normal read queries, except for a broader range of queries. Hence, the name use_replica_if_possible is confusing. We also want a new interface to forcefully redirect all read queries to the replicas, regardless of primary stickiness. That interface can be used to wrap around queries which are able to tolerate the replica lags.

In a dicussion on Slack, we come to conclusion that:

  • Rename use_replica_if_possible to fallback_to_replicas_for_ambiguous_queries. This one implies that it has special treatments for ambiguous queries, but still follow load balancing stickiness, nothing different from a normal read query.
  • Implement use_replicas_for_all_reads to redirect all reads to the replicas, regardless of primary stickiness. Write and ambiguous queries go to primary.
Edited by Quang-Minh Nguyen