Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to specify read-only replica for SELECTs #1085

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

vmihailenco
Copy link
Member

@vmihailenco vmihailenco commented Dec 4, 2024

There are some minor API changes. Most notable of them is the removal of GetConn() IConn method, because the internal query conn IConn must be initialized to nil so Bun can resolve to a random read-only connect.

Overwriting the connection using query.Conn(conn) still works.

The usage should be like this:

db := bun.NewDB(sqldb, dialect,
  bun.WithReadOnlyReplica(sqldb2),
  bun.WithReadOnlyReplica(sqldb3))

When/if we want to provide custom logic how to pick a replica (e.g. replica with the lowest latency), it can be done like this:

db := bun.NewDB(sqldb, dialect,
  bun.WithConnResolver(resolver))

Closes #1037

@vmihailenco vmihailenco requested a review from j2gg0s December 4, 2024 13:19
@vmihailenco vmihailenco force-pushed the feat/read-only-replica branch 2 times, most recently from edf633e to 6bf1e97 Compare December 4, 2024 16:24
@vmihailenco vmihailenco force-pushed the feat/read-only-replica branch from 6bf1e97 to 702e525 Compare December 4, 2024 16:39
@Tiscs
Copy link
Collaborator

Tiscs commented Dec 5, 2024

Awesome.

I think variadic function is a good way to allow multiple replicas to be set at once.

db := bun.NewDB(sqldb, dialect,
  bun.WithReadOnlyReplicas(sqldb2),
  bun.WithReadOnlyReplicas(sqldb3, sqldb4),
  bun.WithReadOnlyReplicas(replicas...))

@j2gg0s
Copy link
Collaborator

j2gg0s commented Dec 6, 2024

When we want to use the master for a specific query to avoid master-slave replication delay, how should we proceed?

@vmihailenco
Copy link
Member Author

When we want to use the master for a specific query to avoid master-slave replication delay, how should we proceed?

There is a Conn() method on each query that allow to specify the connection that will be used. So you can just:

db.NewSelect().Conn(db)

@vmihailenco vmihailenco marked this pull request as draft December 6, 2024 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Request] Add multiple DB endpoints for reader/writer
3 participants