AI EngineeringInterview preparation
Intermediate·Concept·2 min read

Multi-tenancy and comparison with PostgreSQL + pgvector

Practice this question

Overview

  • Multi-tenancy supports several organizations while preventing cross-organization access.
  • MongoDB often fits flexible document records; PostgreSQL provides relational constraints, joins, transactions, and optional row-level security with pgvector.
  • Both require explicit permission design and workload tuning.

Why this matters

Client-facing AI products may serve separate insurers, departments, or businesses.

An everyday example

Shared office infrastructure must still keep each company's records private.

A technical example

Compound keys and scoped queries include tenant ID; SQL schemas may use tenant-aware relationships and row policies.

How it works

  1. Choose isolation model
  2. Enforce tenant in services/indexes/cache keys
  3. Add object permissions
  4. Test adversarial requests
  5. Monitor and revoke access consistently.

Architecture and flow

Verified tenant/user → scoped data + search + memory + cache → authorized results.

The insurance assistant example

Two insurers can both have a policy called “Motor Plus” without sharing retrieved clauses or customer data.

When to use it

Shared infrastructure with tested policy boundaries; stronger physical separation where required.

When to avoid it

Do not assume database-per-tenant removes the need for secure routing, caching, or provider controls.

Interview answer

“I choose MongoDB or PostgreSQL around data relationships, integrity, operational fit, and retrieval performance, with tenant isolation enforced across every derived store.”

Benefits, trade-offs, and production considerations

  • PostgreSQL row policies require correct roles and connection handling; MongoDB tenant filters require consistent application enforcement.
  • Neither product prevents cross-tenant prompt, cache, checkpoint, or trace leaks automatically.
DimensionMongoDB Vector SearchPostgreSQL + pgvector
Data fitFlexible document/chunk metadata and existing MERN recordsRelational data and SQL-heavy business constraints
IntegrityDesign appropriate constraints and transactional boundariesRelational constraints and transaction-oriented operations
Access controlsService-enforced tenant/object rules and supported search filtersService rules plus carefully configured row policies where useful
Retrieval operationsSearch index lifecycle and deployment configurationExtension/index tuning, filtered ANN behavior, resource contention
ChoicePrefer when existing MongoDB operations and benchmarks fitPrefer when existing SQL relationships and benchmarks fit

Adapted from 13_AI.MD · “Multi-tenancy and comparison with PostgreSQL + pgvector”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.