Skip to content
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

Fix: ActiveRecord::ConnectionTimeoutError (MAYBE-RAILS-CX) #1684

Closed
wants to merge 1 commit into from

Conversation

revise-dev[bot]
Copy link

@revise-dev revise-dev bot commented Jan 24, 2025

The ActiveRecord::ConnectionTimeoutError occurs because the application is running out of available database connections. This happens when all connections in the connection pool are in use and new requests cannot obtain a connection within the timeout period (5 seconds by default).

Looking at the configuration:

  1. Puma is configured to use 3 threads per worker (default setting)
  2. GoodJob is configured with 5 queue threads (1 + 2 + 1 + 1 from different queues)
  3. Additional GoodJob threads for job listener, cron, and executor (3 threads)

Total required connections:

  • Puma: 3 threads × 1 worker = 3 connections
  • GoodJob queue threads: 5 connections
  • GoodJob additional threads: 3 connections
  • Total minimum required: 11 connections

The current pool size of 11 is exactly equal to the minimum required connections, leaving no buffer for occasional spikes or additional concurrent operations. It's best practice to provide some buffer capacity beyond the minimum required connections.

Changes to database.yml:

  1. Increased the default pool size from 11 to 16, providing a buffer of 5 additional connections
  2. Updated the comment to accurately reflect the connection requirements
  3. Maintained the ability to override via DB_POOL_SIZE environment variable

This change ensures there are enough connections available during peak usage while maintaining the flexibility to adjust the pool size through environment variables if needed.

Test Additions:
Created a new test file to verify the connection pool configuration and behavior under load. The tests ensure:

  1. The connection pool is configured with the correct size
  2. The application can handle concurrent database operations
  3. The pool size can be overridden via environment variables

Tip

You can make revisions or ask questions of Revise.dev by using /revise in any comment or review!

  • /revise Add a comment above the method to explain why we're making this change.
  • /revise Why did you choose to make this change specifically?

Important

If something doesn't look right, click to retry this interaction.

@zachgoll zachgoll closed this Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant