To distribute load and improve the database performance, I am trying to split read-write queries with some custom modification. I am using makara(https://github.com/instacart/makara) gem for the same in my Rails application. The same is working fine for splitting read-write queries to master and slave. I need to modify the master db selection by checking the replication lag with some custom condition.
I have defined a custom proxy class(as mentioned in documentation) in config/initializers directory but same does not seem to work.
class MyAwesomeSqlProxy < ::Makara::Proxy
hijack_method :select, :ping
send_to_all :connect, :reconnect, :disconnect, :clear_cache
def connection_for(config)
::Sql::Client.new(config)
end
def needs_master?(method_name, args)
return false if args.empty?
sql = args.first
sql !~ /^select/i
end
end
When I make any changes in needs_master?
method, same is not reflecting while running the query. I am not sure if I am defining it at the wrong place or this class needs to be loaded somewhere explicitly.