tenanted.dev v0.2.2
laravel-tenanted 0.2.2 on crates.io

Database-per-tenant
for Laravel for Rust.

Structural multi-tenancy by default — every customer gets an isolated database, with vanilla laravel-rust models and fail-closed safety.

Try Live Demo Read the Docs

One line to turn it on.
Zero lines in your models.

.with_tenancy() wires the resolver, TrustHosts, the fail-closed database guard, a tenant-prefixed cache store, and five tenant:* Artisan commands. Your controllers, models and Route Model Binding stay exactly as laravel-rust wrote them.

  • Subdomain, header, signed-session, or custom resolution
  • Tenant and Option<Tenant> extractors
  • Route Model Binding returns 404 across tenants
Getting started
src/main.rs
use std::process::ExitCode;
use laravel_tenanted::WithTenancy;

fn main() -> ExitCode {
    laravel::Application::configure()
        .with_tenancy()
        .with_routing(|routing, _| {
            routing.routes(|route| {
                route.get("/invoices", list_invoices);
                route.get("/invoices/{invoice}", show_invoice);
            });
        })
        .run()
}
How isolation works

Every customer gets their own database. Nothing else changes.

01

Physical database per tenant

Each tenant gets a named pool (tenant_acme) registered on first touch. DatabaseManager::using_connection routes every #[model], db::table() and transaction inside the request's async task, with no global mutation, so concurrent tenants never race.

Under the hood
02

Fail-closed by default

require_tenant() points the default connection at an undefined sentinel. A query that escapes a tenant scope errors immediately instead of reading the wrong database. Tenant IDs are sanitized before they touch a URL or a path.

Security model
03

Full-stack scoping

Cache keys become tenant:{id}:…, files land under tenants/{id}/…, queued jobs restore their tenant via laravel::context, and every structured log line carries tenant="acme".

Queues, cache & storage
04

SQLite-first, cloud-ready

An idle SQLite tenant costs 0 RAM and 0 connections, provisions in ~1 ms, and deletes in one file. Run locally on disk, pair with LiteBucket for stateless S3/R2 containers with per-tenant encryption, or use Postgres schemas on one DB.

SQLiteLiteBucket R2/S3Postgres schemasMySQL
Why SQLite?
Battle-tested

Verified end-to-end against real infrastructure. No mocks.

SQLite tenants on 16 LRU pools
500
Random-access throughput, p50 3.4 ms
2,653 req/s
Live LRU evictions under load
5,840
Resident memory, 65 open FDs
43.8 MiB
Errors and leaks, 500/500 verified
0 / 0
SIGKILL mid-load169/169 acked rows survived; integrity_check = ok on all 11 DBs
Two processes, one directory640 cross-process ops, 0 SQLITE_BUSY
Verified live onPostgreSQL 15.1 · MySQL 8.0.31 · Redis/Valkey · S3 (RustFS) · SQLite · LiteBucket
Live demo

Click through it before you cargo add it.

A laravel-rust + Inertia + React app running on Laravel Cloud, with an org switcher built from the Inertia guide. Sign in as any persona in one click and watch each organization's data stay in its own database.

Open demo.tenanted.dev
Central data

Shared tables name their connection once.

Users, plans and memberships live in a central database. Pin the model and it ignores the tenant override everywhere.

Models, migrations & central data
src/models.rs
#[model(connection = "central")]
pub struct BillingPlan { pub id: i64, pub name: String }
Thesis & inspiration

Structural isolation by default.

Separate databases, not WHERE tenant_id = ?

Shared-table multi-tenancy puts every customer in the same tables and relies on global scopes, composite indexes and code review to prevent leaks. laravel-tenanted takes the opposite stance: isolate customer data structurally so a forgotten filter can never cross tenant boundaries.

Want shared-table row-level scoping or Postgres RLS? That is a different model and belongs in a separate package.

Why SQLite changes the math

Database-per-tenant used to mean burning server connection limits and paying for idle clusters. With SQLite, an inactive tenant costs 0 RAM and 0 connections, provisions in ~1 ms, runs reads in-process with zero noisy neighbors, and backs up or deletes as a single file.

Deploying on stateless containers? Pair with laravel-litebucket to store every tenant SQLite database directly on S3, Cloudflare R2, or Laravel Cloud Object Storage.

Inspired by activerecord-tenanted

Built in the spirit of Basecamp's activerecord-tenanted ("Everybody Gets a Database") — proving that structural isolation can feel effortless when the framework handles routing, fail-closed guards, migrations, cache, queues and files natively.

Designed from the ground up for laravel-rust's async Tokio runtime using DatabaseManager::using_connection and laravel::context.