Multi-Tenancy & Quotas
Physical tenant namespace partitioning, byte/row quotas, and per-workload admission budgets.
SpydDB enforces physical namespace partitioning alongside per-tenant storage and query resource budgets so multi-tenant SaaS workloads cannot inspect or starve neighboring tenants.
1. Enabling Physical Tenant Isolation#
Configure security.multi_tenant_enabled = true at initial store creation in server.json:
{
"security": {
"multi_tenant_enabled": true
},
"tenant_quotas": [
{
"tenant": 1001,
"max_bytes": 10737418240,
"max_rows": 5000000
}
],
"workload_quotas": [
{
"workload": "analytics",
"max_connections": 32,
"max_active_queries": 16,
"max_query_memory_bytes": 268435456,
"max_query_wall_ms": 15000,
"max_result_rows": 50000
}
]
}Physical Isolation Guarantees#
- Every row key, secondary index key, encryption AAD, change-stream record, and columnar segment family carries an immutable 8-byte
tenant_idprefix. - The
tenant_idbinds directly from the authenticated token, SCRAM credential, or JWTclaim_tenantclaim; no SQL predicate or HTTP request header can switch tenants. - Sessions lacking a tenant ID are rejected fail-closed on data reads and writes when multi-tenancy is active.
2. Storage & Row Quotas#
Set per-tenant byte (max_bytes) and row (max_rows) limits (0 disables a limit):
- Every
insert,update,delete, andupsertupdates the durable tenant ledger inside the exact same WAL atomic batch as the data write. - Writes exceeding
max_bytesormax_rowsfail atomically before committing, leaving both row storage and usage accounting untouched. - Index rebuilds,
drop collection,truncate, and tiering maintenance automatically recompute the tenant ledger accurately.
3. Per-Statement Workload Admission Control#
Assign credentials a workload attribute (such as "oltp" vs "analytics") and configure bounded per-workload resource ceilings:
max_connections— max concurrent open client sockets for that workload classmax_active_queries— max simultaneous executing queriesmax_query_memory_bytes— per-statement working memory cap (hash tables, sorts, vectors)max_query_wall_ms— per-statement execution timeoutmax_result_rows— max returned rows before truncation/error
4. Fail-Closed Tenant Ownership & Record Sharing#
For shared-schema multi-tenant collections where a single collection holds rows owned by different tenants — and selected individual records must be shared across tenants or with external auditors — declare explicit tenant owned by and record identity clauses:
create collection invoices {
tenant: u64 not null,
id: u64 not null,
rec_id: uuid not null,
code: string(40),
title: string(120),
vec: vector(384),
primary key (tenant, id),
unique index idx_inv_code on (tenant, code),
index idx_inv_fts on (title) using fulltext,
index idx_inv_vec on (vec) using hnsw,
tenant owned by (tenant),
record identity (rec_id)
}Tenant-Scoped Role Grants & Record-Level Sharing#
-- Grant read/write/share for tenant 1 and read-only access for tenant 2
create role multi_tenant_ops {
grant read, write, share on collection(invoices) for tenant 1
grant select on collection(invoices) for tenant 2
}
-- Share a single invoice record with an external auditor (optionally expiring at a microsecond timestamp)
grant select on collection(invoices) record "550e8400-e29b-41d4-a716-446655440000" until 1893456000000000 to external_auditorEnforcement Guarantees#
- Fail-closed scans & writes: Sessions without an explicit tenant grant or matching unexpired record grant cannot read, update, or upsert rows in a
tenant owned bycollection. - Accelerated index pushdown: Pure tenant-owner scans push the
tenantbound directly into primary keys, secondary B-trees, HNSW vector indexes, BM25 full-text indexes, R-Tree geo indexes, PAX columnar scans, and graphMATCHtraversals. - Cross-subsystem enforcement: Tenant ownership and record grant revisions are enforced identically across interactive 2PC transactions, shard splits, SSE realtime subscriptions (emitting
access_revokedon expiry/revocation), and/v1/catalog.