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
Multiple SELECT DATABASE() being triggered in MySQL #228
Comments
Update here for other parties reading along. We have found root cause inside our application and are taking steps to fix it. The root cause is:
The Apartment gem is not doing any kind of thread-local, puma-worker-local, or sidekiq-worker local caching or memo-ization of this value. Every time Our application code assumed that was a cheap, local in-RAM call. Ooops on our part, and a bad assumption. The solution we are deploying is application specific but amounts to replacing every call to If you're having this issue then look at your app. How often are you invoking module CustomApartmentUtils
class CurrentTenant
def current
Thread.current[:apartment_current_tenant] ||= Apartment::Tenant.current
end
end
end Every time you Good luck! |
We are seeing a lot of
SELECT database()
being shown at the MySQL logs.Steps to reproduce
Configuration
Using the "Switching Tenants per request" configuration proposed on documentation.
Run a HTTP request to a specific tenant. In our case, we are just doing a generic HTTP request to retrieve some data and doing some basic validations in the model.
Expected behavior
Make a single tenant change per request.
Actual behavior
I came to realize that there has been a very large consumption of the query database "SELECT database()" in a multi-tenant environment, where an HTTP request is made at a generic endpoint and this request triggers this large number of selects.
MySQL Logs:
Possible fix
Analyzing and comparing the PostgreSQL adapter with mysql2, it is seen that the PostgreSQL adapter contains a way to set the current tenant, in which it is not necessary multiple selects database()
When adapting the same function in the mysql2 adapter, is noticed a reduction in
SELECT database()
calls:System configuration
Ruby version
The text was updated successfully, but these errors were encountered: