Lab Objective & Architecture Overview
Domain 2 of the AI-200 exam requires you to architect scalable vector stores for AI retrieval pipelines. Candidates often memorize the syntax without understanding the operational characteristics of index algorithms like HNSW, IVFFlat, and DiskANN.
This lab implements a controlled testbed comparing:
- Dataset: 12,500 pages of enterprise technical and cloud architecture documentation.
- Embedding Model: Azure OpenAI
text-embedding-3-small(1,536 dimensions). - Chunking Strategies: 256 tokens (30-token overlap), 512 tokens (50-token overlap), and 1,024 tokens (100-token overlap) using TikToken
cl100k_base. - Database Systems: Azure PostgreSQL Flexible Server (D4ds_v5, 16 GiB RAM) with
pgvector 0.7+vs Azure Cosmos DB for NoSQL (10,000 autoscale RU/s) withdiskANN.
Chunking Benchmark Results
Smaller chunks provide higher cosine similarity scores because the embedding vector is concentrated on a specific topic. However, they lack broad context unless combined with parent-document retrieval.
| Chunk Size (Tokens) | Total Chunks Generated | Avg Index Time (min) | Top-3 Precision @ 10 | Context Sufficiency |
|---|---|---|---|---|
| 256 Tokens | 48,200 | 4.2 min | 89.4% | Low (Frequently truncated multi-step procedures) |
| 512 Tokens (Recommended) | 24,150 | 2.1 min | 92.1% | Optimal (Captures full conceptual paragraphs) |
| 1,024 Tokens | 12,080 | 1.1 min | 84.7% | Diluted (Embedding captures too many competing concepts) |
Query Latency: pgvector (HNSW) vs Cosmos DB (DiskANN)
We executed 1,000 concurrent vector queries with top_k = 5 over a 50,000 chunk vector index. Latency was measured from an Azure Container App running in the same East US region over private networking.
| Engine & Index Configuration | Avg Latency (P50) | P95 Latency | P99 Latency | Throughput (QPS) |
|---|---|---|---|---|
| pgvector (HNSW, m=16, ef_search=40) | 3.8 ms | 7.2 ms | 12.4 ms | 245 QPS |
| pgvector (HNSW, m=16, ef_search=100) | 6.4 ms | 11.8 ms | 18.1 ms | 160 QPS |
| pgvector (IVFFlat, lists=100, probes=10) | 8.2 ms | 16.5 ms | 24.0 ms | 115 QPS |
| Cosmos DB NoSQL (diskANN, single-partition) | 6.1 ms | 10.4 ms | 15.2 ms | 190 QPS |
| Cosmos DB NoSQL (diskANN, cross-partition) | 14.8 ms | 28.5 ms | 42.0 ms | 72 QPS |
Single-partition vector search in Cosmos DB is fast (6ms) and economical (~3–5 RUs). However, cross-partition vector search fans out to all physical partitions, tripling latency and multiplying RU consumption. For global unpartitioned vector search, pgvector or Azure AI Search is significantly more cost-predictable.
Lab Code: Parameterized pgvector Query with PgBouncer
Below is the production query pattern tested in the lab using async Python with connection pooling:
import asyncpg
async def search_pgvector(pool, query_embedding, company_id, top_k=5):
# Operator <=> is Cosine Distance (range 0 to 2)
# Cosine Similarity = 1 - distance
query = """
SELECT id, chunk_content,
1 - (embedding <=> $1::vector) AS similarity_score
FROM technical_docs
WHERE tenant_id = $2
ORDER BY embedding <=> $1::vector ASC
LIMIT $3;
"""
async with pool.acquire() as conn:
# Tune recall dynamically per session
await conn.execute("SET LOCAL hnsw.ef_search = 64;")
rows = await conn.fetch(query, str(query_embedding), company_id, top_k)
return [dict(r) for r in rows] Key Takeaways for AI-200 Exam Day
- Operator Memorization: pgvector uses
<=>for cosine distance,<->for L2 Euclidean, and<#>for negative inner product. - HNSW vs IVFFlat: HNSW delivers higher recall and doesn't require upfront clustering on non-empty tables; IVFFlat requires less memory but suffers lower recall.
- Cosmos DB DiskANN: Defined in the container's
vectorEmbeddingPolicyandindexingPolicy. Must specify dimensions (e.g. 1536) and distance metric (cosine). - Cost Optimization: Always scope vector searches to a partition key in Cosmos DB whenever possible to avoid cross-partition RU explosions.