LESSON 140 / 215·2 min readIntermediate

Multi-tenancy and comparison with PostgreSQL + pgvector

Must knowConcept lesson
01 Understand02 Recall03 Prepare

Words made simple

A few useful meanings before you read the explanation.

AI
Software techniques for tasks such as recognizing patterns, understanding language or generating content.
Vector
An ordered list of numbers. An embedding is a vector used to represent content for tasks such as similarity search.
Retrieval
Finding and fetching information that is useful for the current question.
More words from this lesson
Schema
A description of the fields and value types that data should contain. It helps check that an answer has the expected shape.
Checkpoint
A saved snapshot of workflow progress that can help it continue later.
Authorization
Checking what that identity is allowed to read or do. Being logged in does not grant access to every record.
Tenant
A customer or organization whose data must stay separate from other customers in a shared application.
Trace
A linked record of the steps in one request, including model calls, retrieval and tools. It helps locate delays or failures.

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

PAUSE & RECALL

Can you explain it without the notes?

Multi-tenancy and comparison with PostgreSQL + pgvector

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

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…

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Scalability, availability, reliability, and latency

Adapted from 13_AI.MD · “Multi-tenancy and comparison with PostgreSQL + pgvector”. Examples are study fragments; framework behavior can depend on your version.