ADR-053: Database Degradation Posture — Bounded Statements and a Typed Answer for Each Phase of a Database Failure

Status: Accepted (2026-11-20) with plan database-degradation-posture
Issue: #1599
Ruling: the maintainer, 2026-11-20, quoted in the Decision below.
Relates to: ADR-001 (one database per service), ADR-020 (the fault legs that measured this)

Context

A service's database can fail a request in three places: while the request waits for a connection, while a statement runs, and while a commit's answer is on its way back. Before this decision canopy answered them as follows.

  • Waiting for a connection. Every pool canopy-db builds gives up after 5 s. ApiError::database and ApiError::internal_database turn that into a 503 (#1296), and canopy-rules's evaluate route does the same by its own mapping. The answer carries no Retry-After, so a caller cannot tell how long to wait.

  • A statement that does not finish. The pool canopy_api::bootstrap gives every service sets no statement timeout. A statement stuck behind a lock or a sick database runs until the caller gives up (service clients do at 30 s), and goes on holding its connection after the caller has gone. A few places bound their own statements by hand: the identity issuer pool at 5 s, and several background jobs through a SET LOCAL statement_timeout string each wrote for itself.

  • A commit whose answer is lost. An error from commit() became an ordinary 500. The fault leg a_commit_whose_answer_is_lost_errors_although_the_row_is_committed shows that such an error does not mean the transaction was not committed: the server had committed the row, and the client still got an I/O error.

Two measurements bear on the numbers below. The pool-contention rig (#1598) shows that a full pool lends exactly what it has and times every other caller out between 5,000 and 5,002 ms after the acquire began. It does not measure how soon a waiter is served once a connection returns: its winners hold past the bound on purpose. The fault legs of #1593 show a severed connection fails a statement at once, and that only the unanswered commit is ambiguous.

Decision

  1. Every service bounds its statements. "Introduce a fleet-wide statement timeout with route-class ceilings."

  2. The bound is a default on the service's pool; a long job sets its own. "Pool default of 30 s; long jobs raise it per transaction."

  3. Each phase has its own typed answer. A caller can tell "wait and retry" from "this statement is too slow" from "the write may have happened".

  4. Every commit goes through one helper, and a gate keeps it so. "Yes: fleet edit to the helper, plus the lint."

The answers

Phase What happened Status Answer

Acquire

The pool lent no connection within its 5 s bound (sqlx::Error::PoolTimedOut).

503

code db_pool_exhausted, header Retry-After: 1

Statement

The server cancelled the statement (SQLSTATE 57014), at its budget or by an explicit cancel.

504

code db_statement_timeout, no Retry-After

Commit

commit() returned without the server's answer having been read (I/O, protocol, a closed connection).

500

code db_commit_outcome_unknown

  • Each is a variant of ApiError with a fixed status, a fixed RFC 9457 code and fixed detail text. No database message reaches a client.

  • Retry-After is one second, a constant. It is a chosen value, not a measured one: one second is the smallest delay the header can state and matches the hint the idempotency middleware already gives for an in-flight request. How long a full pool takes to free a connection depends on what its holders are doing, which the pool cannot know.

  • A 504 carries no hint. A statement that ran out its budget may be slow for a reason a retry repeats. The retry loop in canopy-api does not retry a 504; a caller that wants to retries deliberately.

  • The commit answer says "unknown", not "failed". A commit the server refused (it answered with an error) is a different case: nothing was committed, and it is answered like any other database refusal.

  • The integrity-constraint answers (409 and 422) and the redacted 500 are unchanged. ApiError::internal still never reclassifies its source.

The statement budget

Class Ceiling Where it applies

Request (default)

30 s

every pool canopy_api::bootstrap builds, from the setting db_statement_timeout_secs

Identity issuer

5 s

the issuer pool in canopy-security, as before this decision

Batch

300 s

a background job, inside its own transaction, set explicitly

  • The default equals the longest a service client waits (DATA_PATH_TOTAL_TIMEOUT, 30 s): a statement that outlives its caller is cancelled no later than the caller stops listening. A deployment may set the default anywhere from 1 to 300 seconds; a value outside that range refuses to start.

  • The budget is a statement_timeout session setting sent when a connection starts, so it holds from the first statement and a pooled connection cannot drift from it. A deployment that puts a connection pooler between a service and its database must have the pooler pass startup options.

  • A job that needs longer sets a budget for its own transaction through one canopy-db function. The hand-written SET LOCAL statement_timeout strings in services go through it, their values unchanged.

  • The migration pool carries no budget: a migration may run long by design.

  • lock_timeout is not decided here.

Alternatives considered

  • Type only the failures that can occur today, and set no timeout. Not chosen by ruling. A statement with no budget still holds its connection after its caller has gone, which is how one slow statement becomes pool exhaustion for every other request.

  • Two pools per service, a tight one for requests and a looser one for background work. Not chosen by ruling. It doubles each service's connections against a database with a fixed connection limit, and background workers are handed the application pool in many places.

  • The typed commit for new code only, with existing sites listed as exceptions and converted over time. Not chosen by ruling: an answer that is reliable at some commit sites and not others cannot be relied on by a caller.

  • A computed Retry-After, from the pool's queue depth. Rejected: sqlx does not report how long the queue will take to clear.

  • A different status for the unknown commit (a 503 or a 409). Rejected: the caller did nothing wrong, and "try again" is exactly what an unkeyed write must not do. A 500 with a code the caller can read says what is known.

Consequences

  • A caller gets three different signals where it had one and a half. The retry loop waits the hinted second on a pool-exhausted 503 and stops on a 504.

  • A statement that ran for minutes unnoticed now fails at 30 s. Every background worker's statements are inventoried before the default lands, and the ones that need the batch ceiling set it.

  • An explicit cancel and a timeout share one SQLSTATE, so the answer's detail says "cancelled or timed out". Nothing in canopy cancels statements today.

  • Roughly 570 commit calls change to the helper, and a bare .commit() in production code fails the db-pools gate from then on.

  • A retry under the same idempotency key after db_commit_outcome_unknown still re-runs the handler. Replaying the first run's outcome instead needs its own design (#3487). Until then the code tells a caller the truth, and the convention is to retry a write only under a key.

  • A service that calls another and gets a 503 from it (the rules client does, from canopy-rules) answers its own caller 503 without the hint. Forwarding the hint is not part of this decision.

Verification

The plan's units carry the tests. The ones this ADR depends on: the response tests that pin each code, status and the Retry-After header; the devstack test that a pool built with a budget reports it from the server and that a statement past it fails with 57014 and frees its connection; the fault leg extended to assert the unknown-commit case; the db-pools test that the workspace holds no bare commit; and the pool rig's route test, which holds a service's pool full and reads the 503 and its hint off the wire.

Edit this page · default