Rails Async Database Queries: Stop Making Your Users Wait for the Database
Get a 2X boost on your requests nearly for free!
You know that feeling when your Rails app is technically fast, but a page still takes forever to load because it’s firing off six separate database queries one after another? Yeah, we’ve all been there. Rails 7.0 introduced load_async, and Rails 7.1 expanded it to aggregates and raw SQL - and honestly, it’s one of the most underappreciated features in modern Rails. Let’s dig into why it’s awesome and what’s actually happening behind the scenes.
The Big Win: Overlapping “Wait Time”
Here’s the thing: the magic isn’t about running Ruby code in parallel. On CRuby, the Global VM Lock means your Ruby code still executes one thing at a time. What async queries actually let you do is overlap I/O wait time - all those precious milliseconds your app spends twiddling its thumbs while the database thinks about its answer.
Picture a typical dashboard action loading several independent pieces of data:
@users = User.active.load_async
@orders = Order.recent.load_async
@revenue = Order.async_sum(:total)
Without async, these run one after another, each one blocking until the database responds. With async enabled, the first two get kicked off onto a background thread pool immediately. Each line hands you back an ActiveRecord::Promise. Your main thread keeps chugging along with Ruby code until you actually need the values: when the view iterates over @users or displays @revenue.
The payoff is real. If a query takes 300ms of database time, overlapping it with other work can shave hundreds of milliseconds off your response. One benchmark showed seven aggregate queries dropping from ~817ms synchronous to ~410ms asynchronous: a clean 2X improvement just from overlapping wait time. That’s the kind of win your users feel.
Async queries shine brightest for:
- Dashboards and reporting pages with multiple independent aggregates.
- Controller actions loading several top-level associations before rendering.
- Any endpoint where you can say “I need this data eventually, but not this exact instant.”
What’s Actually Happening Under the Hood
When you call load_async on a relation, Rails doesn’t magically make your database driver non-blocking. Instead, it schedules the query onto a Concurrent::ThreadPoolExecutor configured via config.active_record.async_query_executor.
The method returns the relation itself, but now it’s marked as having a scheduled query. The actual SELECT gets dispatched to a worker thread from the pool. That worker checks out a database connection, runs the query, and stashes the result. When you later iterate the relation or call a method that needs the records, Rails checks whether the background work is done. If yes, instant results! If not, your foreground thread waits until the worker finishes.
For aggregate methods like async_count, async_sum, or async_find_by_sql, you get back an ActiveRecord::Promise. Grab the value with .value and it returns immediately if the promise is already resolved, or blocks if it isn’t.
You can actually see this happening in your logs: async queries show up with an ASYNC prefix, and the db time reflects the real database latency even when foreground time reads 0.0ms. Pretty neat, right?
One important caveat: If the thread pool is saturated, if you’re inside a transaction, or if async_query_executor is left as nil (the default!), Rails quietly falls back to running the query in the foreground. Async queries are an optimization, not a guarantee: you’ve got to explicitly configure an executor in production for them to do anything at all.
The Sharp Edges to Watch For
Async queries aren’t a free lunch, and it pays to know where the rough edges are.
Connection pool pressure. Every async query checks out a database connection for as long as its worker thread is running. Schedule ten async queries in a request with a pool of five, and you’re in for a bad time. The async thread pool concurrency and your database connection pool size are tightly coupled, so tune them together.
Transactions are a no-go. You can’t use async queries inside a transaction. Rails will raise or fall back, because the async worker uses a different connection than the one holding the transaction. So async is off the table for workflows that need read consistency within a transactional block.
Tests quietly fall back. In tests with transactional fixtures enabled, load_async runs in the foreground. Your tests pass, queries work, but you’re never actually exercising the async path. That’s intentional - it keeps test isolation intact - but it means you’ll want staging or integration validation if async behavior is critical to your performance story.
Configuration and Observability
To actually turn async queries on, drop one of these into your environment config:
# Global thread pool (simplest)
config.active_record.async_query_executor = :global_thread_pool
config.active_record.global_executor_concurrency = 5
Or go per-database with :multi_thread_pool and max_threads in database.yml. Remember: the default is nil, which means every async call silently runs synchronously. Don’t get fooled into thinking you’ve enabled something you haven’t!
Observability is refreshingly simple. Just look for ASYNC in your Active Record logs. If you see ASYNC but the request time hasn’t budged, the query wasn’t your bottleneck, rather your pool was too small to overlap anything useful. If you see no ASYNC at all, check your executor config and make sure you’re not inside a transaction.
The Bottom Line
Async database queries in Rails are a surgical, I/O-bound latency optimization, not a general-purpose concurrency model. They’re fantastic when you’ve got multiple independent queries that can overlap, and they vanish into the background (literally) when misconfigured. Try them on your dashboard endpoints, aggregate-heavy admin pages, and controllers that load several top-level objects. Keep an eye on the ASYNC log prefix and your request timings. And size your connection pool and async executor concurrency together, because the promise of overlap only pays off when there are enough worker threads and database connections to go around.
Your users will thank you. And honestly? So will your p95 latency graph. 🚀