Skip to content

Database capacity

Definition

Database capacity is the number of MariaDB connections the gateway may hold at one time. Every gateway worker process keeps two small, fixed-size pools of connections. A request that needs a connection when its pool is full waits briefly and, if no connection frees up in time, is refused instead of opening an extra connection.

Function

The limit exists to keep the total below the per-user connection quota of the database (MAX_USER_CONNECTIONS). Without it, a burst of slow queries would open more and more connections until the database answered Too many connections to every worker at once.

Each worker has two pools:

Pool Used by Size setting Default
Request pool Everything on the request path, including the request log write and admin saves that take a tenant lock mysql.pool_size 10
Background pool Timers and housekeeping: audit chain, knowledge promotion, secret re-wrap, sweepers mysql.bg_pool_size 2
Background pool of worker 0 The same, on the one worker that runs the singleton background jobs mysql.bg_pool_size_worker0 16

Background work never takes a request connection, so a slow sweeper cannot starve live traffic, and live traffic cannot starve the sweepers.

Sizing

For W workers, one deployment holds at most:

W × pool_size + (W − 1) × bg_pool_size + bg_pool_size_worker0

With the production defaults (16 workers) that is 16 × 10 + 15 × 2 + 16 = 206 connections. Two deployments running side by side, as during a blue/green switch, use 412, which stays under a quota of 450 and leaves 38 connections of headroom. Raising pool_size by one adds one connection per worker, 32 across two deployments, so check the quota before changing it.

The one-off migration session (its own aig_migrate pool, same database user) is not part of this count; it exists only while a migration runs, and while it runs it uses part of the headroom and counts against the same MAX_USER_CONNECTIONS quota. The budget covers the gateway backend containers only. Any other client that uses the gateway database user (an operator or maintenance session, an ad-hoc query) comes out of the 38-connection headroom.

Worker 0 runs the singleton background jobs. Its background pool is larger (bg_pool_size_worker0) because it holds the job locks for all of them. The database work of those jobs goes through worker 0's request pool, so it shares worker 0's request slots with its HTTP traffic.

A few background jobs are also started by an HTTP call from the scheduler, for example knowledge promotion (POST /run-promotion). Such a request still takes its job lease from the background pool, which has only 2 slots on a worker other than worker 0. If that pool is full the lease is refused and the job reports an error and backs off; it is not reported as "busy", which is reserved for a lease held by another job instance.

When a pool is full

The pool size is a hard limit per worker: connections in use plus idle connections never exceed pool_size (or bg_pool_size). A request that finds its pool full waits for a connection to be returned, for up to 2 seconds (less when the caller sets a shorter timeout). Waiting requests are served in arrival order, on a best-effort basis; strict first-in-first-out is not guaranteed. About four times pool_size requests may wait per worker (the limit can be passed briefly while connections are being handed over). A request beyond that limit is refused at once; a waiting request is refused when its wait runs out. The gateway logs one warning per refusal:

mysql connect capacity refusal on request pool (size 10): failed to connect: too many waiting connect operations
mysql connect capacity refusal on request pool (size 10): timeout
mysql connect capacity refusal on request pool (size 10): failed to connect: timeout
mysql connect capacity refusal on request pool (size 10): failed to receive packet header: timeout
  • failed to connect: too many waiting connect operations: the wait queue was full. The same text is used when the worker cannot wait at all (its wait mechanism is unavailable or fails); the gateway then also logs one warning per worker naming that fault.
  • A bare timeout: the request waited and no connection was returned in time.
  • failed to connect: timeout: the TCP connect to the database timed out.
  • Any other text ending in : timeout, such as failed to receive packet header: timeout: the database accepted the connection but a step of the handshake timed out; the text names the step.

The line names the pool (request or bg). Because the limit is hard, the sizing formula above is a true upper bound under load, not only in the steady state.

The 2 seconds are one deadline for the whole attempt: after a wait, the TCP connect and each step of the database handshake may only use the time that is left. A refused request therefore answers within about 2 seconds. It takes longer only if the database accepts the connection but then stalls at several handshake steps, because each step is capped separately. There is no retry ladder on top of a capacity refusal.

All of these timeouts count as a capacity refusal after one attempt. The trade-off: a lost SYN or a slow tunnel fails after one 2-second attempt instead of being retried.

Capacity refusals are not retried at once; the next attempt would meet the same full pool. Other connection failures keep the usual short retry. What the caller sees depends on the path:

  • Request log. The entry is held in the spill area and written again once connections free up. No entry is lost while the area has room.
  • Inference requests whose token or provider-key lookup cannot get a connection answer 503 with code service_unavailable and a Retry-After: 1 header; a client may retry after that delay. The same applies when the database reports a self-clearing state (a lock wait timeout, a deadlock, a server read-only switchover). The request is always refused: a failed token lookup never falls through to an anonymous request, even on a gateway that does not require authentication, an unknown token stays 401, and a lookup that fails is 503 (transient) or 500 (permanent). On a token miss, a failing revocation or sibling check still answers the generic 401. A permanent database error (for example a missing table or a decrypt failure) stays 500 internal_error.
  • Gateway lookups. The gateway lookup that every inference request starts with (/v1/<tenant>/<gateway>/...), the gateway checks of the MCP proxy and of the asynchronous-result poll, and the model catalog (the model list and the Copilot action catalog) answer the same 503 with code service_unavailable and Retry-After: 1 when the lookup cannot get a connection or the database reports a self-clearing state. A permanent database error stays 500. A fault is never turned into success: it is not answered as an unknown gateway, an anonymous request or an empty model list, and an empty list from a fault is not remembered, so the next request asks the database again.
  • Agent and MCP lanes. The authentication and visibility checks of the agent run lane (the owner, department, sharee and feature-gate reads, the bridge-token check), of the MCP proxy (token, agent and gateway lookups) and of the document-AI Entra lane answer 503 with code service_unavailable and Retry-After: 1 when the read cannot get a connection or the database reports a self-clearing state. For the Entra lane the gateway-row read answers 503 without Retry-After on any other fault, and a gateway configuration resolve that fails otherwise keeps the 403 "not fully configured" answer. The inbound webhook trigger answers 503 temporarily_unavailable with Retry-After only when the workflow feature-flag read hits a retryable database fault; any other fault on that read stays 500. The MCP OAuth authorize endpoint answers by redirect with error=temporarily_unavailable, and the protected-resource metadata answers 503 with Retry-After. A permanent database error stays 500 on the agent and MCP lanes, and a fault is never turned into an allow or an unknown agent there. The same holds for the later reads of the run lane (agent load, knowledge areas, provider pin, project and MCP connector resolution, task and trigger loads, and the deliver-email lookups of scheduled tasks): a retryable fault answers 503 with Retry-After: 1 and does nothing; once a stream has opened, the fault ends it with a service_unavailable error event instead, as the headers are already sent.
  • Writes that wrote nothing. A gateway write that fails with a connection loss, a deadlock, a lock wait timeout or a statement the database refused before applying it answers 503 with code service_unavailable and Retry-After: 1 only when the write is known not to have happened, so the same call can be sent again. This covers the start of an agent run (the run record), the creation of the result conversation of a scheduled-task delivery, the persisting of a delivered turn pair, and the claim that lets an approved held tool call resume. Any other write fault, and any fault whose outcome is unknown, stays 500: a write after the claim has landed (recording the hold) and the workflow-run latch of a response are never answered 503.
  • Admin requests that hit a transient storage fault answer 503 ("temporarily unavailable; retry later") with Retry-After: 1, readable cross-origin. The public sign-up routes answer the same retryable-fault 503, with the same retry hint; see the sign-up database faults table.
  • Wallet charges and settles. A wallet charge or settle refused for capacity is held and replayed by the wallet holding area, not lost.
  • Background jobs skip the cycle and run again on the next timer tick.

A tenant-lock admin save holds one request slot while it waits up to 5 seconds for the lock, and its critical section takes further slots from the same pool. Under heavy concurrent saves on one worker the inner checkouts queue and fail after 2 seconds with no connection; this is bounded and cannot deadlock.

A refusal that never clears points to a pool that is too small for the load, or to queries that hold connections for too long. The warnings are the signal; they appear in the gateway error log.

The database refusing

If the database itself refuses the connection (Too many connections, or the user quota being hit), the gateway also closes the half-open socket, so a refused handshake does not use up a slot in the pool.

Verified under load

The behaviour above has been verified under heavy concurrent load, with the default pool sizes and with a deliberately tiny pool, including a database fault in which the instance's own database connections are killed repeatedly for a few seconds:

  • no request on the covered paths answers 500; overload answers 503 with Retry-After: 1 and code service_unavailable;
  • after the holding area has drained, the request log has exactly one row per answered request, and the wallet has moved exactly once per charged request.

The number of database sessions of one gateway instance stayed within the sum of the per-worker pool limits in these runs, including at up to 400 concurrent users on the chat path and 200 on the admin read path.

Pricing of answers that are served while the database refuses connections is still being fixed: under such a refusal a small number of answers can be saved without a price. The wallet figures above hold for the answers that were priced.

This describes behaviour observed on a development machine. It is not a production capacity guarantee.