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

Merged
merged 12 commits into from
Jan 22, 2025

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:

resolver := bun.NewReadWriteConnResolver(
  bun.WithReadOnlyReplica(follower1),
  bun.WithReadOnlyReplica(follower2),
)

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

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
Copy link

github-actions bot commented Jan 6, 2025

This pull request has been automatically marked as stale because it has not had activity in the last 30 days. If there is no update within the next 7 days, this pr will be closed. Please feel free to give a status update now, ping for review, when it's ready. Thank you for your contributions!

@github-actions github-actions bot added the stale label Jan 6, 2025
@j2gg0s j2gg0s added no stalebot and removed stale labels Jan 6, 2025
@vmihailenco vmihailenco marked this pull request as ready for review January 18, 2025 07:45
@vmihailenco
Copy link
Member Author

This is ready for review. Please speak up if there are any concerns.

db.go Outdated Show resolved Hide resolved
db.go Outdated Show resolved Hide resolved
db.go Outdated Show resolved Hide resolved
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

healthy := make([]*sql.DB, 0, len(r.replicas))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to declare two slices and always use the one not utilized by r.healthyReplicas to avoid memory allocation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds like you suggest to declare 2 slices and re-cycle them in the monitor.

It is not totally safe since we don't exactly know when the slice becomes free, but more importantly I believe it is not worth it since a slice allocation every 3-5 seconds is not going to change much.

db.go Outdated Show resolved Hide resolved
@vmihailenco vmihailenco merged commit 3d8666a into master Jan 22, 2025
4 checks passed
@vmihailenco vmihailenco deleted the feat/read-only-replica branch January 22, 2025 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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