bdurand / seamless_database_pool

Add support for master/slave database clusters in ActiveRecord to improve performance.
http://rdoc.info/projects/bdurand/seamless_database_pool
MIT License
224 stars 58 forks source link

SELECT queries randomly going to master #42

Open gaurish opened 7 years ago

gaurish commented 7 years ago

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

alxgsv commented 7 years ago

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