Pure Python Stdlib · SQLite WAL · 20-Thread Stress Verified

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 DomainStorage PatternWrite FrequencyConcurrency Guarantee
Quota Sliding WindowsTimestamped dispatch ledger (60s / 86,400s / 2,592,000s)Per live upstream attemptAtomic count + commit prevents RPM overshoot
Circuit Breaker StatePer-upstream row (state, open_trips, open_until_ts, in_flight)On failure / probe / resetSingle-probe HALF_OPEN lock under thread contention
EWMA TelemetryPer-upstream success_rate, latency_ms, fake_200_rateEvery completed attemptAlpha 0.25 updates serialized cleanly without drift
Cross-Tenant ArchiveCacheCanonical SHA-256 keyed payload + adaptive TTLOn verified live hitConcurrent readers never block on WAL writers
Creem Webhook LedgerUnique event_id idempotency tableOn paid pack settlementDuplicate 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

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