All posts

How to Fix Timeouterror in Ruby on Rails In Production

Struggling with Timeouterror in Ruby on Rails in production? This guide explains why it happens and how to resolve it quickly.

Understanding TimeoutError in Rails Production

When your Rails app throws Timeout::Error in production, requests are taking longer than your server allows. This commonly surfaces as 502 or 504 errors in your load balancer.

Why This Happens

  • ActiveRecord queries running without limits on large tables
  • External HTTP calls without configured timeouts
  • Background job queues backing up
  • Connection pool starvation under traffic spikes

The Fix

Configure timeouts at every boundary:

# config/initializers/timeouts.rb
Rails.application.config.after_initialize do
  ActiveRecord::Base.connection_pool.checkout_timeout = 5

  Faraday.default_connection_options = {
    request: { timeout: 8, open_timeout: 3 }
  }
end

For individual slow queries, add statement timeouts:

ActiveRecord::Base.connection.execute("SET statement_timeout = '5s'")

Prevention Tips

To avoid this issue recurring, add automated checks to your CI/CD pipeline. Write integration tests that exercise the failure path — not just the happy path. Use linting rules to enforce best practices across your team. Consider adding health checks that detect this class of error early in staging before it reaches production.

Tracking with Bugsly

Bugsly groups timeout errors by endpoint and shows you the p95 response times leading up to each failure. This lets your team prioritize the slowest code paths before they become outages.

Try Bugsly Free

AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.

Get Started Free