Timeouts & Retries
Timeouts, Deadlines & Safe Retries
Connecting stateless serverless functions to relational databases over HTTP requires careful handling of network interruptions. A failed HTTP request does not guarantee that PostgreSQL aborted the statement.
The Timeout Hierarchy
Multiple independent timeouts govern a single query request. Always size outer timeouts slightly larger than inner timeouts:
flowchart TD
T1["Client Platform Timeout (40s - 60s)"]
T2["Client fetch() AbortController (36s)"]
T3["Reverse Proxy / Ingress Timeout (38s)"]
T4["Gateway Response Deadline (35s)"]
T5["Pool Checkout Timeout (10s)"]
T6["PostgreSQL statement_timeout (30s)"]
T1 --> T2
T2 --> T3
T3 --> T4
T4 --> T5
T4 --> T6
Why Timeouts Must Be Staggered
If your client fetch() timeout is 30 seconds, but the gateway's PostgreSQL statement timeout is 30 seconds, a client-side abort will trigger before the gateway can receive PostgreSQL's response and construct a clean error envelope. Setting the client timeout to 36 seconds allows the gateway's 35-second deadline to return a structured error response with actionable diagnostic codes.
The Danger of "Outcome Unknown"
When an HTTP client aborts a request, or if a network disconnect occurs while a query is running:
Identifying Unknown Outcomes
The gateway explicitly surfaces when an outcome cannot be confirmed:
- Immediate Queries: Returns HTTP
500with codeQUERY_OUTCOME_UNKNOWN. The underlying cause (such asEXECUTION_DEADLINE_EXCEEDEDor client disconnect) is provided inerror.cause. - Transactions: A status of
"unknown"indicates thatCOMMITwas issued to PostgreSQL, but the gateway lost connection before receiving the confirmation.
When encountering an unknown outcome:
- Do NOT blindly retry the write.
- Inspect application state (e.g., query by a natural unique identifier, idempotency token, or timestamp).
- If the record already exists, treat the operation as successful.
- If the record does not exist, safe retry can proceed.
Safe Retry Decision Tree
| Returned Error Code | Phase | Safe to Retry? | Reason |
|---|---|---|---|
QUERY_POOL_BUSY (503) | Pre-dispatch | Yes | Connection was never acquired; SQL never sent |
QUERY_CONNECTION_LIMIT (503) | Pre-dispatch | Yes | Global connection limit reached; SQL never sent |
INVALID_QUERY_TYPE (400) | Validation | No | Schema error; fix client request |
INSUFFICIENT_PRIVILEGE (403) | PostgreSQL | No | Key lacks database/table permissions |
QUERY_OUTCOME_UNKNOWN (500) | Post-dispatch | Conditional | Check application state first before retrying |
Transaction unknown status | Commit phase | Conditional | Check application state first before retrying |
IDEMPOTENCY_KEY_CONFLICT (409) | Pre-dispatch | No | Reusing the same key with different SQL |
cf.cacheTtl). Automatic retries should only apply to idempotent GET requests.Idempotent Writes with Idempotency-Key
For multi-statement write transactions (POST /transaction/execute), always include a unique Idempotency-Key header:
curl -X POST http://localhost:8080/transaction/execute \
-H 'x-api-key: <api-key>' \
-H 'x-database: billing' \
-H 'Idempotency-Key: ord_checkout_9182374' \
-H 'content-type: application/json' \
-d '{
"queries": [
{"query": "INSERT INTO orders (id, total) VALUES ($1, $2)", "parameters": ["ord_checkout_9182374", 49.99]}
]
}'Idempotency Mechanics
- First Submission: The gateway normalizes the SQL and parameters, computes a cryptographic fingerprint, and creates a record with status
pending. - Replay on Network Drop: If the client loses connection and resubmits the exact same request with the same
Idempotency-Key, the gateway detects the match and returns200 OKwith the existing transaction status without re-executing the statements. - Conflict Detection: If the client reuses an
Idempotency-Keywith different SQL or parameters, the gateway rejects it immediately with409 IDEMPOTENCY_KEY_CONFLICT.
Serverless Production Checklist
Before rolling out serverless functions to production, verify each item:
- Function invocation timeout is comfortably larger than 36 seconds.
- Client
fetch()uses anAbortControllerset to 36 seconds. - Reverse proxies and HTTP clients have automatic POST retries disabled.
- Gateway base URL, database name, and API keys are stored in encrypted secret managers.
- Scoped API keys are restricted to specific tables and CRUD operations.
- All dynamic query values use
$1, $2parameters rather than string concatenation. - Batch write transactions provide stable
Idempotency-Keyheaders. - Application logic reconciles
QUERY_OUTCOME_UNKNOWNbefore triggering manual retries.
