Overview
The MySQL persistence adapter provides distributed locks, circuit breaker state management, caching, and checkpoint support using MySQL or MariaDB as the backend. It uses application-level locks with TTL tracking and InnoDB storage engine for ACID guarantees.Installation
Features
- Distributed Locks: Application-level locks with TTL and automatic expiration
- Circuit Breaker State: Persistent failure tracking with JSON storage
- Caching: Database-backed cache with TTL support
- Checkpoints: Full checkpoint support for task recovery
- Idempotency: Prevent duplicate operations (via
MySQLIdempotencyAdapter) - Auto Schema: Automatic table creation with InnoDB engine
- Transactional Safety: ACID guarantees for lock operations
- MariaDB Compatible: Works with both MySQL and MariaDB
Basic Usage
Configuration
mysql2 Pool instance for MySQL connections
Configuration options
Database Schema
The adapter automatically creates the following tables onconnect():
go_goscope_locks
go_goscope_circuit
go_goscope_cache
go_goscope_checkpoints
Lock Provider Methods
acquire(key, ttl, owner?)
Acquires a distributed lock with automatic TTL-based expiration. Uses transactions withFOR UPDATE for strong consistency.
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
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 using JSON storage.Cache key
Promise<T | null> - Cached value or null if not found/expired
set(key, value, ttl?)
Stores a value in the cache using JSON.Cache key
Value to cache (must be JSON-serializable)
Time-to-live in milliseconds (optional)
delete(key)
Removes a cached value.Cache key
clear()
Clears all cached values (respects key prefix if set).keys(pattern?)
Lists all cache keys matching an optional pattern.Optional pattern with wildcards (e.g.,
'user:*')Promise<string[]> - Array of matching keys
Checkpoint Provider Methods
save(checkpoint)
Saves a checkpoint for task recovery.Checkpoint data including taskId, sequence, progress, and data
load(checkpointId)
Loads a specific checkpoint by ID.Checkpoint identifier
Promise<Checkpoint<T> | undefined>
loadLatest(taskId)
Loads the most recent checkpoint for a task.Task identifier
Promise<Checkpoint<T> | undefined>
list(taskId)
Lists all checkpoints for a task.Task identifier
Promise<Checkpoint<unknown>[]>
cleanup(taskId, keepCount)
Deletes old checkpoints, keeping only the most recent N.Task identifier
Number of checkpoints to keep
deleteAll(taskId)
Deletes all checkpoints for a task.Task identifier
Connection Example
Best Practices
Lock acquisition uses
FOR UPDATE which provides strong consistency. The adapter automatically cleans up expired locks on each acquire() call.MariaDB Compatibility
The adapter is fully compatible with MariaDB. The schema uses standard SQL features supported by both MySQL 8.0+ and MariaDB 10.5+.Performance Considerations
- Lock operations use transactions with row-level locking
- InnoDB engine provides ACID guarantees
- JSON columns require MySQL 5.7+ or MariaDB 10.2+
- Connection pooling is handled by the mysql2 Pool
- Indexes on
expires_atoptimize cleanup queries
Related
PostgreSQL Adapter
PostgreSQL-based persistence
SQLite Adapter
SQLite-based persistence