What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

The error QueuePool limit of size 5 overflow 10 reached, connection timed out means SQLAlchemy could not provide a connection to a waiting operation before the pool’s timeout expired. It does not, by itself, prove that the database has run out of connections: the application’s pool may be saturated because connections are held too long, not returned, or insufficient for legitimate concurrent work. Find the cause before raising limits.

What “QueuePool limit of size 5 overflow 10 reached, connection timed out” means

A SQLAlchemy connection pool keeps database connections available for reuse. When an operation checks out a connection, that connection is temporarily unavailable to other operations using the same pool. If the pool has reached its configured capacity, another checkout waits for pool_timeout seconds. If no connection is returned during that wait, SQLAlchemy raises the QueuePool timeout error. SQLAlchemy describes this as application workload exceeding a configured limit: SQLAlchemy 2.1 error messages.

For a QueuePool, the ordinary maximum in-play connections for one Engine is pool_size + max_overflow. Thus, a configuration with pool_size=10 and max_overflow=20 allows up to 30 connections in play for that Engine. SQLAlchemy 2.1 documents defaults of pool_size=5 and max_overflow=10 for the default QueuePool, which explains the numbers in the example error. Check your installed SQLAlchemy version, database dialect, and Engine arguments; defaults and pool behavior are not universal. See Engine Configuration and Connection Pooling.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A Session and a pooled connection are related but not interchangeable. A Session is the ORM unit of work; it may acquire a connection when database work occurs and retain it while a transaction is active. Closing the Session ends its use of resources, but a healthy DBAPI connection may be returned to the pool for reuse rather than physically disconnected. The scarce resource in this error is the connection checked out from that Engine’s pool.

SQLite configurations can use different pool behavior, and SQLAlchemy async Engines use AsyncAdaptedQueuePool rather than the regular QueuePool. Confirm the actual engine and driver before applying QueuePool assumptions: SQLAlchemy pool types.

Why FastAPI can run out of pooled connections

FastAPI does not automatically make a pool larger or guarantee that application code releases a connection promptly. A timeout can result from several different conditions, and the error alone does not identify which one applies to your service.

  • Sessions or transactions live too long. A request may keep a connection checked out while it performs unrelated work, waits on another service, or handles a slow query.
  • Cleanup is missing on some path. Exceptions, background tasks, streaming responses, or custom dependency behavior can make resource lifetime harder to reason about.
  • Engine creation is repeated. Creating Engines per request or connection creates extra pools instead of reusing a stable application-level pool.
  • Concurrency exceeds the pool’s planned capacity. A burst of simultaneous database work can fill a correctly functioning pool.
  • The database is slow to serve work. Long queries or lock waits can extend connection checkout time, causing other requests to queue.

These are diagnostic possibilities, not conclusions about your service. SQLAlchemy recommends one Engine per database per application, and describes the Engine as a long-lived application fixture: Engine Configuration and Working with Engines and Connections.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How to investigate the timeout before changing settings

  1. Capture the full failure and configuration. Record the complete error, configured pool_size, max_overflow, and pool_timeout, SQLAlchemy version, dialect, and whether the application uses a synchronous or asynchronous Engine.
  2. Measure pool occupancy and wait time. SQLAlchemy exposes Pool.status() as a brief description of pool state. Use it alongside metrics for checked-out connections, checkout wait duration, request duration, and transaction duration; status snapshots alone may not reveal short-lived saturation. See Connection Pooling.
  3. Inspect where Engines are created. Verify that each database has a stable Engine for the application rather than a new Engine for each request. Also account for separate worker processes: each process may own its own Engine and pool.
  4. Trace Session cleanup on every path. Review normal returns and exceptions, plus background and streaming work. FastAPI’s SQL tutorial shows a Session dependency using yield; its dependency documentation explains cleanup timing and function-scoped dependencies: SQL (Relational) Databases, Dependencies with yield, and Advanced Dependencies.
  5. Find what extends checkout time. Correlate slow requests with query duration, database lock waits, and any network or CPU work performed while a transaction is open. Treat each as a hypothesis until tracing or logs confirm it.
  6. Calculate total possible connection demand. Multiply each Engine’s capacity by the number of processes and replicas that may run it, then include other applications and clients sharing the database. Compare that demand with the database’s actual configured or managed connection capacity before tuning.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How to fix SQLAlchemy QueuePool limit reached

Close Sessions reliably and shorten connection use

Use a context-managed Session or a FastAPI dependency that yields the Session and closes it after the request’s database work. The FastAPI SQL tutorial demonstrates the per-request yield pattern and a context-manager alternative: FastAPI SQL database tutorial. Keep transactions focused on database work. When safe for the operation, do external network calls or CPU-heavy work before opening a transaction or after it has ended, so the connection is not held while the application waits elsewhere.

Keep the Engine long-lived

Create the Engine as an application-lifetime object and reuse it for database access. A new Engine per request creates another pool and can multiply possible connections instead of resolving saturation. SQLAlchemy’s Engine and connection guidance explains pool ownership and intended lifecycle: Working with Engines and Connections.

Tune bounded pool capacity using deployment measurements

pool_size sets the pool’s persistent capacity, max_overflow permits additional simultaneous connections, and pool_timeout controls how long a checkout waits. Increasing the first two can help when measured, legitimate database concurrency exceeds current capacity and the database has headroom. It can instead shift the failure downstream if the added connections, multiplied across workers and replicas, exceed what the database can support. SQLAlchemy documents these controls and the capacity calculation in its QueuePool error guidance.

Do not choose a limit from a single worker’s demand alone. The per-Engine capacity is only one part of the aggregate: deployment process count, replica count, other services, and database connection limits all matter. Without those figures, there is no defensible universal pool-size recommendation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a timeout as a waiting policy, not a capacity fix

Increasing pool_timeout gives waiting requests more time for a connection to be returned; it does not add connections or fix a leak. Longer waits can turn a fast failure into slower request latency and may conflict with application or upstream deadlines. Set it as part of an intentional queueing and failure policy.

Avoid unlimited overflow as a leak workaround

Setting max_overflow=-1 removes the pool’s overflow limit, but does not create unlimited database capacity. Under pressure, the database’s own connection limit or system resources can become the failure point. SQLAlchemy explicitly warns about this trade-off in its error documentation.

Use stale-connection controls only for stale connections

pool_pre_ping and pool_recycle address disconnected or stale server-side connections. They are not remedies for ordinary pool saturation caused by too many simultaneous checkouts or slow returns. Use them when evidence points to stale connections, following SQLAlchemy’s pooling guidance.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.