apalis-libsql
Native libSQL storage backend for Apalis background job processing.
Problem: Apalis uses sqlx for SQLite, but that doesn't work with Turso's embedded replica model or edge deployment.
Solution: This crate provides a native libSQL driver that enables:
- Local-first development with SQLite
- Embedded replicas that sync with Turso Cloud
- Edge deployment (Cloudflare Workers, Fly.io, etc.)
- Automatic cloud sync without managing connections
Performance
Benchmarks run on bare metal (not containerized):
| Spec | Value |
|---|---|
| CPU | AMD Ryzen 9 5950X (16-core, 4.6GHz) |
| RAM | 128GB DDR4 |
| Storage | Samsung MZQL2 NVMe (enterprise SSD) |
| OS | Linux (bare metal) |
Results:
Raw write IOPS: ~117K/sec (single INSERTs, worst case)
Batched writes: ~578K/sec (transaction batching, best case)
Read IOPS: ~272K/sec (primary key lookups)
Transaction TPS: ~78K/sec (BEGIN/UPDATE x2/COMMIT)
What this means:
- Your mileage will vary based on hardware
- NVMe storage is critical for write performance
- These are local SQLite numbers, not network-bound Turso Cloud
- Containerized/VM performance will be lower
Run your own benchmarks: cargo test --test perf_test --release -- --nocapture
Architecture
+-----------------+ +------------------+ +-----------------+
| Your App |---->| Local Replica |<----| Turso Cloud |
| | | (SQLite file) | | (Distributed) |
+-----------------+ +------------------+ +-----------------+
| |
v v
+------------------+ +------------------+
| Apalis Workers | | Other Replicas |
| Process Tasks | | Edge Locations |
+------------------+ +------------------+
How it works: Your app writes to a local SQLite file. libSQL automatically syncs changes with Turso Cloud, which distributes to other replicas. Works offline, syncs when connected.
When to Use This vs apalis-sqlite
Use apalis-sqlite:
- Standard SQLite deployment
- Compile-time SQL query validation
- Single-machine applications
- Traditional server environments
Use apalis-libsql:
- Turso Cloud integration needed
- Embedded replica model (local-first)
- Edge deployment (Cloudflare Workers, Fly.io)
- Multi-region applications
- Offline-first requirements
Quick Start
Local Development
use LibsqlStorage;
use Builder;
use ;
use TaskSink;
async
Turso Cloud (Embedded Replica)
use LibsqlStorage;
use Builder;
use ;
use TaskSink;
async
Task Processing
use LibsqlStorage;
use Builder;
use ;
use ;
use WorkerContext;
use StreamExt;
async
Configuration
use Duration;
use ;
use Builder;
async
Key settings:
buffer_size: Tasks fetched per poll (affects memory usage)poll_interval: How often to check for new tasks (affects latency)keep_alive: Worker heartbeat interval (affects failure detection)reenqueue_orphaned_after: When to retry tasks from crashed workers
Turso Setup
- Create database:
# Install Turso CLI
|
# Login
# Create database
# Get database URL
- Get auth token:
- Use in your app:
use LibsqlStorage;
use Builder;
async
Edge Deployment
Works on Cloudflare Workers, Fly.io, and other edge platforms:
// This is a conceptual example - actual Cloudflare Workers integration
// would require the worker crate and proper WASM setup
use LibsqlStorage;
use Builder;
async
Database Schema
The storage creates these tables:
-- Workers table (worker registration and heartbeats)
(
id TEXT PRIMARY KEY,
worker_type TEXT NOT NULL,
storage_name TEXT NOT NULL,
layers TEXT,
last_seen INTEGER NOT NULL
);
-- Jobs table (task storage)
(
job BLOB NOT NULL, -- serialized task data
id TEXT PRIMARY KEY, -- task ID (ULID)
job_type TEXT NOT NULL, -- queue name
status TEXT NOT NULL, -- Pending, Running, Done, Failed
attempts INTEGER NOT NULL,
max_attempts INTEGER NOT NULL,
run_at INTEGER NOT NULL, -- scheduled execution time
last_error TEXT, -- error message on failure
lock_at INTEGER, -- when task was locked
lock_by TEXT, -- worker that locked the task
done_at INTEGER, -- completion time
priority INTEGER NOT NULL,
metadata TEXT -- additional JSON metadata
);
Installation
[]
= "0.1.0"
Feature flags:
tokio-comp(default): Tokio runtime supportasync-std-comp: async-std runtime support
Testing
# Run all tests
# Run performance benchmarks
# Test with Turso (requires env vars)
TURSO_AUTH_TOKEN=xxx TURSO_DATABASE_URL=xxx
License
MIT