All posts

Multi-tenant RAG: how to isolate customer data in an AI SaaS product

Namespaces, row-level security or a database per tenant: how to choose a multi-tenant RAG isolation model, and the retrieval-quality trap that a tenant_id filter hides.

CodeKrypt 7 min read

If you are building multi-tenant RAG into a SaaS product, the question that decides your enterprise sales cycle is not which model you use. It is whether customer A's documents can ever appear in customer B's answer. One such leak ends a security review, and sometimes the account.

The failure is rarely dramatic. It is a missing WHERE tenant_id = ... in one code path that was added in a hurry. This post compares the three isolation models that work, shows where each one stops protecting you, and covers a retrieval-quality problem that only appears once you share an index.

What are the three ways to isolate tenants in RAG?

Every design is a point on one spectrum: how much infrastructure tenants share.

Three isolation models from most shared to least shared: shared index with a filter, namespace or partition per tenant, and database per tenant1. Shared index + filter2. Namespace / partition3. Database per tenantIsolation = every query's filterIsolation = storage boundaryIsolation = separate instance
ModelIsolation enforced byCross-tenant leak if...Typical fit
Shared index, metadata filterApplication code, per queryAny query omits or mangles the filterPrototypes, internal tools
Shared index, database-enforced (Postgres RLS)The databaseThe app connects as a role that bypasses policiesMost B2B SaaS on Postgres
Namespace or partition per tenantThe storage layoutThe namespace is taken from user inputMost B2B SaaS on a vector database
Database or cluster per tenantInfrastructureCredentials or backups are sharedRegulated or very large tenants

The first row is the one to avoid in production. It makes every future developer responsible for remembering a filter, and security reviewers know it.

How does a namespace per tenant work?

Vector databases that support namespaces let you store each tenant's vectors separately. Pinecone's multitenancy guide recommends one namespace per tenant on a serverless index, says each namespace is stored separately, and notes that every read and write targets a single namespace, so a bug that queries the wrong one is less likely to expose another tenant's data. It describes offboarding as deleting the namespace, a "lightweight and almost instant operation".

The same page lists the costs of the model:

The remaining risk is where the namespace string comes from. Derive it from the authenticated session on the server, never from a request parameter, a prompt or a tool argument the model can influence.

How does pgvector row-level security protect tenant data?

If your application already runs on Postgres, pgvector keeps vectors next to the rest of your tenant data and lets the database enforce isolation. Row-level security (RLS) attaches a policy to the table, and the database applies it to every query regardless of what the application wrote.

The PostgreSQL manual documents the behaviours that decide whether this is safe:

In practice this means the application must connect as a dedicated, non-owner role without BYPASSRLS, and the tenant must be set per transaction from the authenticated session. Migrations and backup jobs use a different, privileged role. Teams get this wrong by testing RLS as a superuser, where every policy appears to pass.

Add a test that connects as the application role, sets tenant A, and asserts that a query for tenant B's document returns nothing. Run it in CI.

Why does a tenant filter make vector search return too few results?

This is the part most tutorials skip. Approximate indexes such as HNSW find the nearest neighbours first and filter afterwards. The pgvector README states that "with approximate indexes, filtering is applied after the index is scanned", and gives an example: if a condition matches 10% of rows, with HNSW and the default hnsw.ef_search of 40, only 4 rows will match on average.

A tenant filter is exactly that kind of selective condition. In a shared index with many tenants, a small tenant can ask for ten chunks and get two, not because their data is missing, but because the index scanned a neighbourhood dominated by other tenants. The visible symptom is an assistant that says it cannot find something that is plainly in the customer's documents.

The README offers three mitigations:

  1. Iterative index scans (SET hnsw.iterative_scan = strict_order;, or relaxed_order for better recall), which scan more of the index when the filter leaves too few rows.
  2. Partial indexes when there are few distinct filter values.
  3. Partitioning when there are many. For tenant isolation specifically, the README's multitenancy guidance is to use list partitioning or separate tables, because a shared approximate index lets one tenant's vectors affect recall for others.

So a per-tenant boundary protects both privacy and retrieval quality. Measure recall per tenant, not just on an average across the shared index, and include your smallest tenant in the test set.

Where do multi-tenant RAG systems actually leak?

If you get the vector layer right, the leaks move to places the retrieval diagram does not show:

For products serving Indian users, deletion and purpose-limitation duties are part of the picture too; see our post on the DPDP Act and your AI product.

Which model should you choose?

A short decision path that holds up for most products:

  1. Already on Postgres, hundreds to thousands of small tenants: pgvector with RLS, plus iterative scans or partitioning. Start here unless you have a reason not to.
  2. On a managed vector database: namespace per tenant, with the namespace derived server-side. Check the plan's namespace limit against your tenant count first.
  3. A handful of large or regulated tenants: a dedicated database or cluster for them, behind the same application interface, sold as a higher tier.
  4. Shared index with a filter: only for prototypes, and write the migration into the plan.

Whatever you pick, put the choice behind one retrieval function that takes an authenticated tenant context and nothing else. The design is easier to review, and moving a large customer from a shared pool to a dedicated store later becomes a configuration change, not a rewrite.

What to do next

Before you onboard a security-conscious customer, run these checks:

If you are weighing these options for a product you are building, the AI SaaS page below describes how we approach multi-tenant platforms, and our post on RAG or fine-tuning is a useful companion if you are still deciding whether retrieval is the right architecture at all.

CodeKrypt avatar

CodeKrypt

Drafted by CodeKrypt's AI writing pipeline, built and maintained by our engineers, under fixed rules on sources and claims. How we write

Frequently asked questions

How do you isolate customer data in a multi-tenant RAG system?
Enforce isolation below the application code. Either give each tenant its own namespace, partition or database, or use Postgres row-level security so the database refuses to return another tenant's rows even when a query forgets its filter. A tenant_id filter written by hand in every query is the weakest option.
Is a vector database namespace per tenant secure enough?
Pinecone's documentation describes namespaces as physically separate storage and says a query always targets exactly one namespace, which makes accidental cross-tenant reads less likely. It is a strong default for B2B products, but it does not remove the need to derive the namespace from the authenticated session rather than from user input.
Does pgvector row-level security work with HNSW indexes?
It works for correctness, but the pgvector README warns that with approximate indexes filtering is applied after the index is scanned, so a selective filter can return fewer results than you asked for. Enable iterative index scans, or partition by tenant, and test recall per tenant.
Should each tenant get its own database?
Only when contracts, regulation or a very large tenant demand it. A database per tenant gives the hardest isolation and the heaviest operating cost, so most products reserve it for an enterprise tier.
What is the most common multi-tenant RAG leak?
Anything that sits outside the vector query: a shared response cache, an agent tool that fetches by document id without checking the tenant, or logs and traces that store retrieved chunks. The retrieval step is usually the best-protected part.

Related reading

Chat on WhatsApp