# laravel-litebucket > Durable SQLite directly on S3, Cloudflare R2, and Laravel Cloud Object Storage for Laravel for Rust (`laravel-rust 0.2`) — in-process Rust VFS, conditional-PUT fencing, per-tenant HKDF AES-256-GCM encryption, instant PITR, zero-download clones, and 1-line `laravel-tenanted` pairing. - Crate: https://crates.io/crates/laravel-litebucket - CLI Crate: https://crates.io/crates/litebucket - Docs: https://litebucket.dev - Full LLM reference: https://litebucket.dev/llms-full.txt - GitHub: https://github.com/cole-robertson/litebucket - Live R2 Demo: https://demo.litebucket.dev - Agent Skill: `npx skills@latest add cole-robertson/litebucket --skill laravel-litebucket` ## Quick Install ```sh cargo add laravel-litebucket # Optional: standalone ops CLI cargo install litebucket ``` ## Bundled Docs - `guide.md` — Complete reference: 1-line setup, `LiteBucketBuilder`, Laravel Cloud auto-setup, `t.litebucket()` multi-tenancy, encryption & HKDF key rotation, PITR & clones, write forwarding, Prometheus metrics, env vars, and all 11 Artisan commands. --- # Laravel LiteBucket — Complete Reference `laravel-litebucket` integrates **LiteBucket** (an in-process Rust SQLite VFS backed by S3, Cloudflare R2, and Laravel Cloud Object Storage) into `laravel-rust 0.2` with zero sidecars, zero FUSE mounts, and zero database server. --- ## 1. Quick Start (1 Line) ```rust use laravel_litebucket::prelude::*; laravel::Application::configure() .with_litebucket() .with_routing(|routing, _| { routing.web(routes::web::routes).health("/up"); }) .run(); ``` Or tune explicitly with `LiteBucketBuilder`: ```rust use laravel_litebucket::prelude::*; laravel::Application::configure() .with_litebucket_using(|b| { b.sync_mode(SyncMode::Batched) .disk_cache_mb(64) .write_lease(true) .compact_every(32); }) .with_routing(...) .run(); ``` ### `LiteBucketBuilder` Methods | Method | Default | Purpose | |---|---|---| | `sync_mode(SyncMode)` | `Batched` | `Batched` (~1 ms local commits, periodic flush) or `Strict` (durable in bucket before `COMMIT` returns). Env: `LITEBUCKET_SYNC_MODE`. | | `write_lease(bool)` | `true` | Fenced single-writer lease in `write_lock.json`. Env: `LITEBUCKET_WRITE_LEASE`. | | `disk_cache_mb(usize)` | `64` | L2 NVMe CRC32C-verified page cache budget (`0` disables). Env: `LITEBUCKET_DISK_CACHE_MB`. | | `refresh_interval_ms(u64)` | `500` | Reader catch-up poll interval. Env: `LITEBUCKET_REFRESH_MS`. | | `compact_every(u64)` | `32` | Auto-compact LTX history into snapshot chunks every N txids (`0` disables). Env: `LITEBUCKET_COMPACT_EVERY`. | | `shared_pool(bool)` | `true` | Share L1/L2 caches across pooled connections in one process. | | `default_vfs(bool)` | env (`LITEBUCKET_VFS`) | Force process-default VFS on/off. | | `write_forwarding(bool)` | env (`LITEBUCKET_ADVERTISE_ADDR`) | Enable/disable `WriteForwardingMiddleware`. | | `write_forwarding_prefix(prefix)` | `LITEBUCKET_PREFIX` | Scope write forwarding to one prefix. | | `without_shutdown_flush()` | flush on | Disable `SIGTERM` `flush_all_open_dbs` listener. | --- ## 2. Laravel Cloud Object Storage Auto-Setup (Zero Postgres, Zero Disk) 1. Attach a **Laravel Cloud Object Storage (R2)** bucket to your environment. Laravel Cloud injects `FILESYSTEM_ENDPOINT`, `FILESYSTEM_BUCKET`, `FILESYSTEM_ACCESS_KEY_ID`, `FILESYSTEM_SECRET_ACCESS_KEY`, and `FILESYSTEM_REGION` automatically — `laravel-litebucket` discovers all of them with zero manual S3 credentials. 2. Set two env vars in Laravel Cloud: ```dotenv LITEBUCKET_VFS=1 LITEBUCKET_ENCRYPTION_KEY=pass:replace-with-strong-passphrase ``` 3. Wire hybrid strict-central + batched-tenant storage: ```rust // src/config/database.rs let central_url = if env.get::("LITEBUCKET_VFS").as_deref() == Ok("1") { laravel_litebucket::database_url_strict("central/main") } else { "sqlite://storage/database/central.sqlite3?mode=rwc".to_owned() }; // src/app.rs use laravel_litebucket::prelude::*; use laravel_tenanted::WithTenancy; laravel::db::migrations!(fn migrations = "database/migrations"); laravel::Application::configure() .with_litebucket() .with_migrations(migrations()) .with_tenancy_using(|t| { t.litebucket_from_env() .central_connection("central") .auto_create(false); }) ``` --- ## 3. Multi-Tenancy (`laravel-tenanted` + `laravel-litebucket`) `LiteBucketTenancy` (included in `laravel_litebucket::prelude::*`, enabled by default via the `tenanted` feature) adds 1-line methods onto `laravel_tenanted::TenancyBuilder`: | Method | Prefix | Behavior | |---|---|---| | `t.litebucket()` | `tenants/{tenant}/main` | Wires `sqlx-sqlite`-compatible URL with `tenant_key_id={tenant}`, `check_exists`, `discover_tenants`, and GDPR `on_delete` shred. | | `t.litebucket_prefix("orgs")` | `orgs/{tenant}/main` | Same, under a custom root prefix. | | `t.litebucket_template("apps/{tenant}/db")` | custom | Same, with explicit `{tenant}` template. | | `t.litebucket_from_env()` | `tenants/{tenant}/main` | Activates LiteBucket only when `LITEBUCKET_VFS=1`; keeps local SQLite in dev/tests. | | `t.litebucket_prefix_from_env("orgs")` | `orgs/{tenant}/main` | Env-gated custom prefix variant. | ### URL Format Note (`sqlx-sqlite 0.9+`) Never pass custom `?prefix=` query parameters in `sqlite://` URLs (`sqlx-sqlite 0.9` rejects unknown query parameters). Always use `database_url`, `database_url_strict`, `tenanted_database_url`, or `t.litebucket()`, which emit RFC 3986 matrix parameters inside the path: `sqlite://tenants/acme/main;prefix=tenants/acme/main;sync=batched;write_lease=1;tenant_key_id=acme?mode=rwc&vfs=litebucket`. ### Migrating Many Tenant Databases on S3 / R2 - **Lazy on first touch (default):** Dormant tenants are never downloaded on deploy; the first request opens and migrates that tenant under a per-tenant lock with S3 conditional-PUT fencing preventing multi-pod races. - **Eager rollout:** `cargo run -- tenant:migrate --all` discovers tenants via `s3_discover_tenants` and migrates sequentially within the `TENANT_MAX_OPEN` LRU memory cap. For 10,000+ tenants, fan out `MigrateTenantJob` across a queue and use expand-and-contract additive migrations for rolling deploys. --- ## 4. Encryption & Per-Tenant HKDF - `LITEBUCKET_ENCRYPTION_KEY`: `pass:` (PBKDF2-HMAC-SHA256, 100k iterations) or 64-char hex / 44-char base64 256-bit key. - `LITEBUCKET_PREVIOUS_ENCRYPTION_KEY`: Old key during zero-downtime `sqlite:rekey` rotation. - Every 4 KB page is encrypted with AES-256-GCM + CSPRNG 12-byte random nonce before leaving the container. - Multi-tenant DBs derive unique per-tenant 256-bit keys via RFC 5869 HKDF-SHA256 (salt `litebucket-tenant-v1`, info `tenant:`). --- ## 5. S3 / R2 Environment Variable Lookup Order | Setting | Lookup order | |---|---| | Endpoint | `LITEBUCKET_ENDPOINT`, `S3_ENDPOINT`, `LITESTREAM_ENDPOINT`, `FILESYSTEM_ENDPOINT`, `FILESYSTEM_URL`, `AWS_ENDPOINT`, `AWS_ENDPOINT_URL_S3`, `AWS_ENDPOINT_URL`, `AWS_URL` | | Bucket | `LITEBUCKET_BUCKET`, `S3_BUCKET`, `LITESTREAM_BUCKET`, `FILESYSTEM_BUCKET`, `AWS_BUCKET` | | Region | `LITEBUCKET_REGION`, `S3_REGION`, `FILESYSTEM_REGION`, `AWS_DEFAULT_REGION`, `AWS_REGION` (`auto` for R2 / Laravel Cloud) | | Access key | `LITEBUCKET_ACCESS_KEY_ID`, `S3_ACCESS_KEY_ID`, `LITESTREAM_ACCESS_KEY_ID`, `FILESYSTEM_ACCESS_KEY_ID`, `AWS_ACCESS_KEY_ID` | | Secret key | `LITEBUCKET_SECRET_ACCESS_KEY`, `S3_SECRET_ACCESS_KEY`, `LITESTREAM_SECRET_ACCESS_KEY`, `FILESYSTEM_SECRET_ACCESS_KEY`, `AWS_SECRET_ACCESS_KEY` | | Path style | `LITEBUCKET_FORCE_PATH_STYLE`, `S3_FORCE_PATH_STYLE`, `FILESYSTEM_USE_PATH_STYLE_ENDPOINT`, `AWS_USE_PATH_STYLE_ENDPOINT` | --- ## 6. Runtime Helpers, Scaling & Metrics - `laravel_litebucket::flush_db().await?` / `flush_all_open_dbs()` — flush staged Batched commits to the bucket immediately. - `laravel_litebucket::with_retry(3, || async { ... }).await?` — retry transient `SQLITE_BUSY` / HTTP `412` contention in Strict mode. - `LITEBUCKET_ADVERTISE_ADDR=http://10.0.1.42:8080` — enables automatic `WriteForwardingMiddleware` (reads run locally; writes forward over private LAN to the lease owner). - `laravel_litebucket::metrics_prometheus()` — Prometheus exposition text; `all_open_dbs_stats()` — structured stats per open DB. --- ## 7. Artisan Commands (`cargo run -- `) - `sqlite:doctor` — verify bucket access, 412 conditional PUT fencing, Range 206 GETs, RTT - `sqlite:status [--prefix p] [--tenant-key-id t]` — live write-lease & LTX chain status - `sqlite:inspect [--prefix p]` — chunks, logical size, PITR window, encryption - `sqlite:verify [--prefix p]` — `PRAGMA integrity_check` + full CRC64 LTX chain verification - `sqlite:compact [--prefix p] [--retain 32]` — compact history into snapshot chunks & GC old frames - `sqlite:history [--prefix p] [--limit 20]` — list restorable PITR transactions & timestamps - `sqlite:pitr --at=-10m --sql "SELECT ..."` (or `--txid N`) — read-only time-travel query (always use `--at=-10m` with `=`) - `sqlite:restore --at=-10m` (or `--txid N`) — non-destructive PITR restore committed at `head + 1` - `sqlite:rekey [--prefix p] [--tenant-key-id t]` — zero-downtime AES-256-GCM key rotation - `sqlite:clone --from a --to b [--redact scrub.sql] [--force]` — zero-download S3 `CopyObject` or redacted clone - `sqlite:shred --prefix p --yes` — permanently delete every object under a prefix