seamless_database_pool icon indicating copy to clipboard operation
seamless_database_pool copied to clipboard

SELECT queries randomly going to master

Open gaurish opened this issue 8 years ago • 1 comments

Consider the following setup of database.yml:

development:
  database: my_development
  adapter: seamless_database_pool
  password: my_secret_password
  pool_adapter: mysql2
  pool: 25
  username: root
  master:
    weight: 0
    password: my_production_password
  read_pool:
    - host: xyz.rds.amazonaws.com
      database: my_production_database
      weight: 8

Now, I am running simple select queries, trying to fetch from read replica:

Loading development environment (Rails 4.2.7.1)
2.3.3 :001 > SeamlessDatabasePool.use_random_read_connection { User.last.id }
  User Load (0.4ms)  SELECT  `users`.* FROM `users`  ORDER BY `users`.`id` DESC LIMIT 1
7549411 # went to master
2.3.3 :002 > SeamlessDatabasePool.use_random_read_connection { User.last.id }
  User Load (0.5ms)  SELECT  `users`.* FROM `users`  ORDER BY `users`.`id` DESC LIMIT 1
7549411 # went to master
2.3.3 :003 > SeamlessDatabasePool.use_random_read_connection { User.last.id }
  User Load (341.8ms)  SELECT  `users`.* FROM `users`  ORDER BY `users`.`id` DESC LIMIT 1
50420982 # went to slave as expected
2.3.3 :004 > SeamlessDatabasePool.use_random_read_connection { User.last.id }
  User Load (328.8ms)  SELECT  `users`.* FROM `users`  ORDER BY `users`.`id` DESC LIMIT 1
50420982 # went to slave as expected
2.3.3 :005 > SeamlessDatabasePool.use_random_read_connection { User.last.id }
  User Load (301.3ms)  SELECT  `users`.* FROM `users`  ORDER BY `users`.`id` DESC LIMIT 1
50420982 # went to slave as expected
2.3.3 :006 > SeamlessDatabasePool.use_random_read_connection { User.last.id }
  User Load (0.4ms)  SELECT  `users`.* FROM `users`  ORDER BY `users`.`id` DESC LIMIT 1
7549411 # went to master

As you may see from above it is randomly choosing to use master, even if I have pass SeamlessDatabasePool.use_random_read_connection block. I writed the same with use_persistent_read_connection it has the same behavior. During this test the replica was available & was not down.

Expected behaviour If we tell it to use the replica connection, it should not switch to master. Is this a bug or this is how the gem works?

SeamlessDatabasePool version 1.0.18

gaurish avatar Jun 06 '17 09:06 gaurish

@gaurish I think the problem is that you should use pool_weight not weight for that

alxgsv avatar Sep 26 '17 12:09 alxgsv