Overview
The SQLite persistence adapter provides distributed locks, circuit breaker state management, and caching using SQLite as the backend. Ideal for single-node deployments, testing, or embedded applications where a lightweight database is preferred.Installation
Features
- In-Memory Locks: Fast in-memory locks with TTL tracking
- Circuit Breaker State: Persistent failure tracking with SQLite storage
- Caching: Database-backed cache with TTL support
- Checkpoints: Full checkpoint support for task recovery
- Idempotency: Prevent duplicate operations (via
SQLiteIdempotencyAdapter) - Auto Schema: Automatic table creation on connect
- Single File: All data in a single SQLite database file
- Zero Dependencies: No external services required
Basic Usage
Configuration
sqlite3 Database instance
Configuration options
Database Schema
The adapter automatically creates the following tables onconnect():
go_goscope_circuit
go_goscope_cache
go_goscope_checkpoints
Lock Provider Methods
acquire(key, ttl, owner?)
Acquires an in-memory lock with TTL tracking. Locks are not persisted to the database for performance.Lock identifier
Time-to-live in milliseconds
Optional lock owner identifier (auto-generated if not provided)
Promise<LockHandle | null> - Lock handle or null if already locked
Locks are stored in-memory for performance. They are not persisted across application restarts. For persistent locks, use PostgreSQL or MySQL adapters.
extend(key, ttl, owner)
Extends the TTL of an existing lock.Lock identifier
New time-to-live in milliseconds
Lock owner identifier
Promise<boolean> - True if extended successfully
forceRelease(key)
Forces release of a lock regardless of owner.Lock identifier
Cache Provider Methods
get(key)
Retrieves a cached value from SQLite.Cache key
Promise<T | null> - Cached value or null if not found/expired
set(key, value, ttl?)
Stores a value in the cache (JSON serialized).Cache key
Value to cache (must be JSON-serializable)
Time-to-live in milliseconds (optional)
delete(key)
Removes a cached value.Cache key
has(key)
Checks if a key exists and is not expired.Cache key
Promise<boolean>
clear()
Clears all cached values (respects key prefix if set).keys(pattern?)
Lists all cache keys matching an optional pattern.Optional SQL LIKE pattern with wildcards (e.g.,
'user:*' becomes 'user:%')Promise<string[]> - Array of matching keys
Circuit Breaker Methods
getState(key)
Retrieves circuit breaker state from SQLite.Circuit breaker identifier
Promise<CircuitBreakerPersistedState | null>
setState(key, state)
Updates circuit breaker state.Circuit breaker identifier
New state (state, failureCount, lastFailureTime, lastSuccessTime)
recordFailure(key, maxFailures)
Records a failure and potentially opens the circuit.Circuit breaker identifier
Maximum failures before opening circuit
Promise<number> - Current failure count
recordSuccess(key)
Records a success and closes the circuit.Circuit breaker identifier
Connection Example
In-Memory Database
For testing or temporary data:Bun Runtime Support
For Bun runtime, use the specialized Bun adapter:Best Practices
SQLite is single-writer. For high-concurrency scenarios with multiple processes, consider PostgreSQL or MySQL.
Performance Considerations
- Locks are in-memory for maximum performance
- Circuit breaker state and cache are persisted to SQLite
- Use WAL mode for better concurrency:
PRAGMA journal_mode=WAL; - Enable foreign keys if needed:
PRAGMA foreign_keys=ON; - Consider connection pooling for Node.js clusters
- Use indexes for checkpoint queries on large datasets
WAL Mode Example
Use Cases
- Single-node applications: Desktop apps, CLI tools, serverless functions
- Development and testing: Fast iteration with zero external dependencies
- Embedded systems: IoT devices, edge computing
- Local caching: Fast local cache with persistence
- Prototyping: Quick proof-of-concept without infrastructure
Related
PostgreSQL Adapter
PostgreSQL-based persistence
MySQL Adapter
MySQL-based persistence