Skip to content

Multi-Tenancy

Fullfinity supports multi-tenant deployments where each tenant has its own isolated database.

┌─────────────────────────────────────────────────────────┐
│ Single Codebase │
├─────────────────────────────────────────────────────────┤
│ tenant1.example.com tenant2.example.com tenant3.com │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ DB: t1 │ │ DB: t2 │ │ DB: t3 │ │
│ │ Modules:│ │ Modules:│ │ Modules:│ │
│ │ - core │ │ - core │ │ - core │ │
│ │ - crm │ │ - crm │ │ - crm │ │
│ │ - inv │ │ │ │ - inv │ │
│ └─────────┘ └─────────┘ └─────────┘ │
└─────────────────────────────────────────────────────────┘
  • Data Isolation - Each tenant’s data is in a separate database
  • Independent Modules - Tenants can install different modules
  • Customization - Views can be customized per tenant
  • Security - Complete data separation
  • Scalability - Databases can be on different servers

Every request resolves to one database — by host or subdomain (through DB_FILTER, below), the database selector, or the db cookie — and runs entirely against it. Connection pools are kept per database and reused across requests; nothing in a request can reach another database’s pool.

There is no multi_tenant config block. Multi-tenancy is inherent: each database is a fully isolated tenant. The one relevant config key is DB_FILTER in config.yaml, which constrains which databases a deployment will serve. It is a regular expression matched from the start of the database name, so anchor it to pin a single database. %h is replaced by the request’s host name and %s by its first label (the subdomain):

config.yaml
DB_FILTER: ^acme$ # serve only the database named acme
# DB_FILTER: ^%s$ # tenant1.example.com serves the database named tenant1
# DB_FILTER: none # (default) serve any database; let host/selector choose

An unanchored acme also matches acme_staging.

Create each tenant’s database through Fullfinity — the database selector in the browser, or create_database from Python — never with a bare CREATE DATABASE. Creating it this way installs the base module, mints the database’s own filestore id, and revokes public connect access (see Security below):

from fullfinity.engine.api.database import create_database
await create_database("tenant_acme")

Each tenant can install different modules. Module operations on the command line act on the one database named by -db:

Terminal window
fullfinity-server -c config.yaml -db tenant_acme -i crm --init-only

The module’s:

  • Models are created in the tenant’s database
  • Views are stored in the tenant’s ui_view table
  • Security rules are added to the tenant’s model_access table

A shipped view is reloaded from its module on every update, so an edit made to its stored arch is overwritten by the next -u. Customise a tenant by installing a module of your own in that tenant’s database that inherits the view (inherited_view: with arch.operations — see View Inheritance). The change then survives updates and applies only where that module is installed.

Use the database selector’s Backup and Restore. A backup is one archive holding the database dump and its filestore, and a restore gives the new database a filestore id of its own. A bare pg_dump/psql copy carries neither: the files stay behind, and the copy shares the original’s filestore id (see the caution under Which files belong to which database).

To duplicate a database from Python — seeding a sandbox from a prepared master, standing up a throwaway copy — use clone_database, which copies both halves of a tenant:

from fullfinity.engine.api.database import clone_database, delete_database
await clone_database("tenant_master", "tenant_sandbox")
...
await delete_database("tenant_sandbox")

It issues CREATE DATABASE ... TEMPLATE, so PostgreSQL copies the source’s files directly — seconds where a dump piped into a restore takes minutes — and it duplicates the source’s filestore. That second half is not optional: attachments are files on disk, not rows, so a copy made with pg_dump alone resolves every image and document to a 404 while the database itself looks perfectly healthy.

How the files are duplicated depends on where they live, but both routes leave the clone owning its own files — so afterwards the two behave identically: the source can be dropped while the clone keeps everything, and deleting from either touches only its own.

On local disk they are hardlinked rather than copied: one set of bytes, a second set of names, refcounted by the filesystem. A multi-gigabyte filestore therefore costs no extra disk and no measurable time. Where the filesystem cannot link — a filestore root on a different device — it falls back to a real copy automatically.

In object storage the objects are copied server-side and concurrently, so the bytes never travel through the application. Both are safe for the same reason: files are content-addressed — a name is the checksum of its contents, nothing is ever rewritten in place — so storing a new file in the copy writes a new checksum, and deleting one there removes only that copy’s name.

A file’s key begins with one segment identifying the database that owns it:

<store_id>/ab/cd/<sha256-of-the-contents>

store_id is not the database’s name. It is minted when the database is created, stored inside that database, and never derived from anything renameable — because a name is not an identity. It can be changed by restoring under a different one, reused by a later database, and is embedded in every key already written. Keyed by name, restoring acme as acme_staging put every byte where the restored database would never look; two nodes each holding a database called production and sharing one bucket were a single prefix, each serving the other’s files.

An id is minted as <database name>_fs<random hex> — acme_fs01861b610f1e44f78a9656fe768ac919. The hex is the identity and the whole of it; the name in front is a label, so that a filestore root full of prefixes can be read at a glance instead of by connecting to every database in turn. Nothing resolves anything through the label, and two databases sharing a name mint two different ids, so none of the problems above come back through it. It is frozen at mint: renaming a database outside this application leaves the label saying what it was created as, which is cosmetic.

The label is guaranteed, not merely usual: every prefix leads with the name of the database it was minted for, and a name that cannot be one is refused rather than minted without it. So a directory or bucket listing can always be read straight down — a prefix that does not start with a database’s name is not one this application produced.

Create, clone and restore each mint a new id, because each produces a database that must own its files rather than share another’s. Reading it is await env.filestore_id(); nothing else should construct a key — and nothing should read the label, which is for humans.

  • Connection pools are per-database
  • Cache keys include database name
  • Queries are isolated per tenant
  • No cross-tenant data access
  • Tenant context verified on each request
  • Database credentials can differ per tenant
  • Which databases are this deployment’s: those owned by DB_USERNAME. The database selector, the job scheduler and --all-databases operations consider only those, so a Postgres server can be shared with other applications without Fullfinity ever connecting to their databases.
  • New databases admit only their owner. Postgres lets every role connect to a new database by default; Fullfinity revokes that on each database it creates or clones, so other applications’ roles on a shared server cannot open it. A database created outside Fullfinity keeps whatever access it was given — revoke it yourself (REVOKE CONNECT ON DATABASE <name> FROM PUBLIC).
  • Migrations run per database
  • A module update applies to the one database named by -db; pass --all-databases instead to update every database this deployment owns, each in its own transaction
  • Database backups are independent

For data shared across tenants (e.g., countries, currencies):

  1. Store in a shared database
  2. Replicate to tenant databases on sync
  3. Or use read-only access to shared database
from fullfinity.engine.api.database import clone_database, create_database
async def create_tenant(name: str, admin_email: str, admin_password: str):
# A fresh database with the base module installed and its first user
await create_database(f"tenant_{name}", username=admin_email, password=admin_password)
async def create_tenant_from_master(name: str):
# Or a copy of a prepared master (idle — see Copying a tenant above)
await clone_database("tenant_master", f"tenant_{name}")

Install further modules per tenant with -db tenant_<name> -i <modules>.