Question

My Rails 3.2 app contains a health check route for an upstream load balancer:

# routes.rb
get 'health' => lambda { |env| [200, {"Content-Type" => 'application/json'}, [ 'Alive.' ] ] }

I am also using Dalli and memcached to cache various objects in my application. I have been using the Dalli debugging output recently and notice that my logs are flooded with health checks:

# application.rb
config.cache_store = :dalli_store, "localhost:11211", { :compress => true, :expires_in => 1.day }
Dalli.logger = Logger.new("#{Rails.root}/log/#{Rails.env}_cache.log")
Dalli.logger.level = Logger::DEBUG

# development_cache.log
[DEBUG] Cache read: http://mysite.com/health/?
[DEBUG] Cache read: https://mysite.com:80/health/?
[DEBUG] Cache read: http://mysite.com/health/?
[DEBUG] Cache read: https://mysite.com:80/health/?
[DEBUG] Cache read: http://mysite.com/health/?
[DEBUG] Cache read: https://mysite.com:80/health/?
....

Are these health checks being served from the cache for some reason? Where is that configured? I have obviously not set up page caching. This route is the only code whatsoever associated with that URL.

Was it helpful?

Solution

That's probably the Rack::Cache middleware that you have. The most simple way to fix it (and have a benefit of going through most layers of the application), is to add cache related header in your rack endpoint:

Cache-Control: no-cache

This will prevent Rack::Cache from caching the result of this action. BTW, the text you're returning is not a valid json, so either change the Content-Type to text/plain, or return something like {"result":"alive"}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top