Week 10 • Day 5Week 10

Day 5:Week 10, Day 5: Metadata & RBAC Security — Why Post-Filtering Fails (`Zero-Results Cliff`) vs Pre-Filtering

Battle-tested daily lesson note • 100% runnable code, interview Q&A, test suite & interactive quiz

Week 10, Day 5: Metadata & RBAC Security — Why Post-Filtering Fails (Zero-Results Cliff) vs Pre-Filtering


1. The Multi-Tenant & RBAC Security Requirement in Enterprise RAG

In any real company, a user from tenant_id="acme_corp" or role="junior_support" must NEVER see chunks belonging to tenant_id="globex" or role="executive_board". Every vector query has both a Vector Similarity Search AND a Boolean Metadata / ACL Predicate:

sql
WHERE tenant_id = 'acme_corp' AND allowed_role IN ('public', 'junior_support')

2. The "Zero-Results Cliff" of Naive Post-Filtering

What happens if you run a global HNSW search for top_k = 10 across the entire 1,000,000-chunk database first, and then filter those 10 results in Python (Post-Filtering)? Suppose tenant_id = 'acme_corp' owns 0.5% (5,000 chunks) of the database:

  • The expected number of acme_corp chunks inside the global unfiltered top_k = 10 is $10 \times 0.005 = 0.05$!
  • 95% of the time, Post-Filtering returns 0 results ([]) even though acme_corp has 5,000 matching documents!
code
Naive Post-Filtering (BROKEN on Selective Filters!): [1,000,000 Total Vectors] ──► Global Top-10 ANN ──► Filter by tenant='acme' (0.5%) ──► 0 Results! Pre-Filtering / Single-Stage Filtered ANN (PRODUCTION STANDARD): [1,000,000 Total Vectors] ──► Inverted Bitmask for tenant='acme' (5,000 IDs) ──► Rank Allowed IDs ──► Full Top-10!

How Production Vector DBs (Qdrant, Milvus, pgvector) Execute Filtered Search

Maintain an Inverted Metadata Bitmask Index mapping each metadata key-value pair (tenant_id="acme_corp") to a roaring bitmap of valid integer vector IDs:

  • If the filter selectivity is small (e.g., < 5,000 matching IDs), directly score the bitmask-selected vectors via SIMD (Pre-Filtering)—which is actually faster than graph traversal and guarantees 100% recall!
  • If the filter matches a larger subset, evaluate the bitmask predicate during HNSW graph neighbor expansion (Single-Stage Filtered HNSW).

In Simple English: Why Filtering Security Permissions AFTER Search Fails

Imagine a company with 10,000 HR Payroll PDFs (Admin-only) and 200 Engineering FAQs (Public):

  • Post-Filtering Disaster ("Zero-Results Cliff"): Suppose an intern asks "vacation policy". Vector search grabs the Top 5 overall matches — all 5 turn out to be Admin HR PDFs! Then the security filter deletes all 5, handing the intern 0 results even though 200 public FAQs existed!
  • Pre-Filtering Fix: First mask down to ONLY documents the user is allowed to see (tenant_id + role_acl) using roaring bitmaps / B-Tree metadata indexes, THEN run vector search inside that authorized slice! Never leak unauthorized docs OR return empty screens!