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

Introduce least-outstanding-connections load balancing #282

Merged
merged 7 commits into from
Jan 17, 2023

Conversation

drdrsh
Copy link
Collaborator

@drdrsh drdrsh commented Jan 14, 2023

Least outstanding connections load balancing can improve the load distribution between instances but for Pgcat it may also improve handling slow replicas that don't go completely down. With LoC, traffic will quickly move away from the slow replica without waiting for the replica to be banned.

If all replicas slow down equally (due to a bad query that is hitting all replicas), the algorithm will degenerate to Random Load Balancing (which is what we had in Pgcat until today).

This may also allow Pgcat to accommodate pools with differently-sized replicas.

Comment on lines +369 to +372
candidates.sort_by(|a, b| {
self.busy_connection_count(b)
.partial_cmp(&self.busy_connection_count(a))
.unwrap()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is actually, the least_checked_out_connections which can be skewed by clients that are idle in transactions, I think we should be fine though.

@levkk
Copy link
Contributor

levkk commented Jan 14, 2023

This is really cool, can't wait. This is also called least connections load balancing afaik.

@drdrsh drdrsh changed the title Introduce least-outstanding-queries load balancing Introduce least-outstanding-connections load balancing Jan 16, 2023
@drdrsh drdrsh marked this pull request as ready for review January 16, 2023 18:58
@drdrsh drdrsh requested a review from levkk January 16, 2023 18:58
@drdrsh
Copy link
Collaborator Author

drdrsh commented Jan 17, 2023

I created a load balancer simulator that runs in the browser. Had fun tinkering with that
https://drdrsh.github.io/load-balancer-playground/

src/pool.rs Outdated
@@ -565,6 +579,20 @@ impl ConnectionPool {
pub fn server_info(&self) -> BytesMut {
self.server_info.clone()
}

fn busy_connection_count(&self, address: &Address) -> u32 {
let state = self.databases[address.shard][address.address_index].state();
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

@levkk levkk left a comment

Choose a reason for hiding this comment

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

Awesome! 💯

@drdrsh drdrsh merged commit 7894bba into postgresml:main Jan 17, 2023
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.

2 participants