Makara doesn't switch to slave when using AR.connection directly
Here you can see that reads are from slave when using model methods... but not connection itself:
production >> SlaveUtils.read_from_slave { User.first }
[slave/1] User Load (0.6ms) SELECT `users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1
production >> SlaveUtils.read_from_slave { User.connection.select_all('show processlist') }
[master/1] (1.4ms) show processlist
production >>
``
This is unrelated to using model methods vs connection methods. You're comparing two different SQL statements. Makara decides which queries to send where based on whether they need the primary db or can use a replica. The SHOW query falls back to the primary connection since it doesn't match any of the regexps.
To verify: If you do a SELECT on the connection first, it'll pick a replica connection and a subsequent SHOW PROCESSLIST should stick to the same connection.
Thanks for clarification. I am not sure I got it at 100%.
Should I do it like this:
production >> SlaveUtils.read_from_slave { c = User.connection; c.select_all('SELECT `users`.* FROM `users` LIMIT 1'); c.select_all('show processlist'); nil }
[slave/1] (0.7ms) SELECT `users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1
[master/1] (1.3ms) show processlist
=> nil