Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a barebones Rails controller block if the database is down?

I have a simple Rails controller which does not depend on the database.

class PingController < ActionController::Base
  def ping
    render text: 'The service is up'
  end
end

However, when the database goes down, this controller action blocks. Why does this happen?

like image 906
Debajit Avatar asked Sep 16 '15 00:09

Debajit


1 Answers

I think there could be three culprits.

A fresh Rails application has the following middleware (Source):

use Rack::Sendfile
use ActionDispatch::Static
use Rack::Lock
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x000000029a0838>
use Rack::Runtime
use Rack::MethodOverride
use ActionDispatch::RequestId
use Rails::Rack::Logger
use ActionDispatch::ShowExceptions
use ActionDispatch::DebugExceptions
use ActionDispatch::RemoteIp
use ActionDispatch::Reloader
use ActionDispatch::Callbacks
use ActiveRecord::Migration::CheckPending
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash
use ActionDispatch::ParamsParser
use Rack::Head
use Rack::ConditionalGet
use Rack::ETag
run Rails.application.routes

Each request to your controller essentially passes through each one of these middlewares in a chain, one after the other.

Three of these are related to the database and thus depend on the database for every request. You'll notice they're all part of ActiveRecord which is a big tip-off the database is involved.

  1. ActiveRecord::Migration::CheckPending

    This checks if there are pending migrations in the database before passing the request through. From what I can tell looking at the source code it checks for pending migrations in all environments.

  2. ActiveRecord::ConnectionAdapters::ConnectionManagement

    Except in the testing enviornment, this middleware purges active db connections each request. Perhaps the database connectivity in this middleware is blocking your controller action.

  3. ActiveRecord::QueryCache

    This could also be blocking database requests. It is active for all environments from what I can tell.

like image 196
madcow Avatar answered Oct 21 '22 04:10

madcow