Multi-Tenant AI Backend
SaaS AI backend with strict tenant isolation for vector stores, databases, API keys, and per-tenant configuration.
Project walkthrough
Multi-Tenant AI Backend
Project Goal
SaaS AI backend where tenant data never leaks across boundaries.
- →API key auth resolving to tenant context
- →PostgreSQL RLS + Pinecone namespaces
- →Per-tenant config, quotas, and usage metering
- →Stripe integration for usage-based billing
Use ← → arrow keys or buttons to navigate the walkthrough
Time breakdown (50h)
Each phase maps to the estimated hours — follow in order for a realistic build schedule.
Tenant model & auth
10h- •Tenant, user, api_key tables with bcrypt-hashed keys
- •Auth middleware: key → tenant_id → request.state
- •Tenant signup and API key rotation endpoints
Data isolation
12h- •PostgreSQL RLS policies on all tenant-scoped tables
- •Pinecone namespace per tenant for vector data
- •Integration tests proving cross-tenant access fails
Per-tenant config & billing
10h- •Tenant config: model preference, chunk size, quota limits
- •Usage meter: tokens in/out per request → Redis counters
- •Stripe webhook for usage-based invoicing
API gateway & quotas
10h- •Per-tenant rate limits and monthly token quotas
- •429 responses with upgrade CTA when quota exceeded
- •Admin API for tenant management and usage reports
Security audit & tests
8h- •Pen-test: attempt cross-tenant data access (must fail)
- •Audit log for all admin actions
- •Load test 50 concurrent tenants
Architecture
Every request is authenticated via API key, which resolves to a tenant context injected into all downstream queries. PostgreSQL RLS enforces row isolation, Pinecone namespaces separate vector data, and a usage meter tracks tokens per tenant for billing and quota enforcement.
Scroll inside the frame to explore · use + / − to zoom up to 200%
Prerequisites
- PostgreSQL row-level security (RLS) concepts
- Multi-tenancy patterns: shared DB vs schema-per-tenant
- FastAPI middleware and dependency injection
- Pinecone namespaces or metadata filtering
- API key hashing and rotation best practices
Setup steps
- Design tenant schema: tenants, users, api_keys, usage_logs tables
- Enable PostgreSQL RLS policies scoped to tenant_id
- Configure Pinecone with per-tenant namespaces
- Build tenant provisioning API (create tenant → namespace + RLS role)
- Add API key auth middleware extracting tenant context
- Integrate Stripe for usage-based billing webhooks
Features to build
- Tenant isolation
- Per-tenant vector namespaces
- API key management
- Usage metering
- Row-level security
Expected result
Provision two tenants, ingest different document sets for each, demonstrate that Tenant A cannot retrieve Tenant B's data even with a crafted query, and show per-tenant usage dashboards with token counts and billing status.
Resume bullet points
- →Architected multi-tenant AI SaaS backend with strict data isolation for 100+ tenants
- →Implemented row-level security and per-tenant vector namespaces
