SQLite WAL Thread-Safe Quota Ledger & Concurrency Architecture
Deploying external Redis clusters and broker sidecars adds operational fragility to lightweight agent gateways. FuelRouter achieves deterministic, race-free multi-threaded concurrency using pure Python 3 stdlib and SQLite Write-Ahead Logging (WAL).
SQLite WAL State Tables & Concurrency Guarantees
| State Domain | Storage Pattern | Write Frequency | Concurrency Guarantee |
|---|---|---|---|
| Quota Sliding Windows | Timestamped dispatch ledger (60s / 86,400s / 2,592,000s) | Per live upstream attempt | Atomic count + commit prevents RPM overshoot |
| Circuit Breaker State | Per-upstream row (state, open_trips, open_until_ts, in_flight) | On failure / probe / reset | Single-probe HALF_OPEN lock under thread contention |
| EWMA Telemetry | Per-upstream success_rate, latency_ms, fake_200_rate | Every completed attempt | Alpha 0.25 updates serialized cleanly without drift |
| Cross-Tenant ArchiveCache | Canonical SHA-256 keyed payload + adaptive TTL | On verified live hit | Concurrent readers never block on WAL writers |
| Creem Webhook Ledger | Unique event_id idempotency table | On paid pack settlement | Duplicate webhook replays credit wallet exactly once |
Why WAL Mode Eliminates Reader-Writer Contention
In default rollback-journal mode, every telemetry write stalls concurrent pool-status reads and archive cache lookups. Enabling PRAGMA journal_mode=WAL allows concurrent HTTP threads on ThreadingHTTPServer to serve cache hits and /v1/router/pools reads while a writer commits quota and EWMA deltas.
Combined with in-process synchronization locks and short bounded transactions inside --state-dir, FuelRouter sustains 20-thread concurrent dispatch bursts with zero database-locked failures.
Zero External Runtime Dependencies & Deterministic Recovery
Because FuelRouter relies exclusively on Python 3 stdlib and SQLite WAL, containers start in under 200 ms with zero external cache servers to provision, patch, or authenticate.
All operational metrics reported by GET /v1/router/metrics—including total dispatches, cross-tenant cache hits, failover counts, and saved_upstream_cost_micros—survive clean server restarts intact.
Launch FuelRouter with Isolated SQLite WAL State Directory
python3 -m src.server --host 127.0.0.1 --port 9051 --state-dir ./data/fuelrouter_state
# Verify WAL mode is active:
curl -fsSL "http://127.0.0.1:9051/health"
# {"ok": true, "sqlite_wal": true, "pools_loaded": 5, "zero_secret_mode": true}
SQLite WAL Operations Checklist
- Confirm sqlite_wal: true on GET /health immediately after server boot.
- Mount --state-dir on local SSD storage (avoid network-attached NFS mounts for SQLite WAL files).
- Verify 20-thread concurrent stress runs produce 0 SQLite lock errors and exact quota counts.
- Back up the --state-dir directory cleanly using SQLite online backup APIs when archiving cache history.
Frequently Asked Technical Questions
Does FuelRouter require Redis or PostgreSQL to run in production?
No. FuelRouter uses pure Python 3 stdlib and SQLite WAL for quota ledgers, circuit breakers, archive caching, and webhook idempotency.
How does FuelRouter prevent two threads from simultaneously running HALF_OPEN probes?
The SQLite WAL breaker transaction checks and updates half_open_in_flight atomically so only 1 probe enters HALF_OPEN while peer threads fail over to healthy nodes.
Are Python ResourceWarnings or unclosed SQLite connections emitted on shutdown?
No. Server shutdown closes all SQLite connections cleanly with zero ResourceWarning or DeprecationWarning output.
Live Try-Now Console & Machine Discovery
Test failover routing, dry-run quotes, and circuit breaker metrics on the live gateway or connect via JSON-RPC 2.0 MCP.
Related FuelRouter Architecture Guides
- $0 Exhaustion Billing Guarantee & Redacted Backoff Traces — zero dollar API exhaustion billing and redacted failover trace
- EWMA Dynamic Health Scoring Formula for Upstream Routing — EWMA dynamic health score API upstream routing formula
- HTTP 429 Rate Limit & Retry-After Automatic Upstream Failover — HTTP 429 rate limit Retry-After automatic upstream failover