An embedding is a list of numbers (a vector) that an embedding model such as text-embedding-3-small (1,536 dimensions) produces from text, so that texts with similar meaning get vectors that sit close together. Vector search embeds the user's question with the same model, then asks the database for the stored vectors with the smallest distance (usually cosine), and an index such as DiskANN or HNSW makes that fast. On Azure you can store and search vectors in Cosmos DB for NoSQL, PostgreSQL with pgvector and Azure Managed Redis.
- An embedding model turns text into a vector; similar meaning gives close vectors.
- text-embedding-3-small returns 1,536 dimensions by default; text-embedding-3-large returns 3,072.
- Embed documents and questions with the same model and the same dimensions.
🎯 Exam tip: "Find items with similar meaning, not matching words" = vector search over embeddings.
⚠️ Watch out: Embedding the query with a different model, or different dimensions, from the stored documents.
You connect with the azure-cosmos Python SDK by creating a CosmosClient from the account endpoint and DefaultAzureCredential, then getting a database and a container. Write items with upsert_item, fetch one item cheaply with a point read (read_item with its id and partition key, about 1 RU for a 1 KB item), and use query_items with parameters for everything else. Pass partition_key to keep a query inside one logical partition; a query without it fans out across partitions and costs more Request Units.
- CosmosClient(url, credential=DefaultAzureCredential()) signs in with Microsoft Entra ID.
- Entra ID data access needs a Cosmos DB data plane role such as Cosmos DB Built-in Data Contributor.
- A container's partition key is set at creation and can't be changed.
🎯 Exam tip: "Retrieve one item with the lowest RU cost" = read_item with id and partition key.
⚠️ Watch out: Running SELECT * FROM c WHERE c.id = @id instead of a point read.
Every Cosmos DB operation costs Request Units (RUs): about 1 RU for a 1 KB point read, more for writes and queries. By default the indexing policy indexes every property, which makes filters cheap but writes expensive, so you exclude paths you never filter on (such as a big /embedding/* array) and add composite indexes for multi-property ORDER BY. Consistency levels trade freshness for cost and latency: Strong, Bounded staleness, Session (the default), Consistent prefix and Eventual. Strong and Bounded staleness reads cost about twice the RUs of the weaker levels.
- A point read of 1 KB costs about 1 RU; writes and queries cost more.
- The default indexing policy indexes every path (/*) and excludes /_etag.
- Exclude large paths you never filter on (like /embedding/*) to cut write RUs.
🎯 Exam tip: "Reduce RU cost of writes" = exclude paths in the indexing policy.
⚠️ Watch out: Leaving a 1,536-number embedding array in the range index.
To search vectors in Azure Cosmos DB for NoSQL, you create the container with a vector embedding policy (the vector path, dataType such as float32, dimensions such as 1536, and distanceFunction such as cosine) and add a vector index (flat, quantizedFlat or diskANN) to its indexing policy, excluding the vector path from the range index. You store each embedding inside the item, then run SELECT TOP k ... ORDER BY VectorDistance(c.embedding, @q) to get the most similar items first.
- Vector embedding policy: path, dataType (float32, float16, int8, uint8), dimensions (default 1536), distanceFunction (cosine default, dotproduct, euclidean).
- Vector index types: flat (exact, max 505 dims), quantizedFlat (max 4,096), diskANN (approximate, max 4,096).
- quantizedFlat and diskANN need at least 1,000 vectors before the index is used; below that it's a full scan.
🎯 Exam tip: "Exact results, small vectors (≤ 505 dimensions)" = flat.
⚠️ Watch out: Using a flat index for 1,536-dimension vectors.
The change feed is a persistent, ordered record of the inserts and updates in a Cosmos DB container. The change feed processor reads it for you: it watches the monitored container, hands each batch of changes to your delegate, and stores its progress in a lease container, so several instances can share the work and resume after a crash with at-least-once delivery. The default latest version mode doesn't capture deletes; all versions and deletes mode does. An Azure Functions Cosmos DB trigger gives you the same pattern without hosting the processor yourself.
- Four parts: monitored container, lease container, compute instance, delegate.
- The processor gives at-least-once delivery; make the delegate idempotent.
- By default it starts from now; WithStartTime reads from a time or from the beginning (latest version mode only).
🎯 Exam tip: "Track progress and share work across instances" = the lease container.
⚠️ Watch out: Expecting deletes to appear in latest version mode.
You connect to Azure Database for PostgreSQL flexible server from Python with psycopg (version 3). Instead of a stored password, you get a Microsoft Entra token from DefaultAzureCredential for the scope https://ossrdbms-aad.database.windows.net/.default and pass it as the password, with your Entra user or managed identity name as the user and sslmode=require. Then you run SQL with parameterised queries (%s placeholders), never by pasting user input into the SQL string.
- Use psycopg 3 (import psycopg) with host, dbname, user, password, port 5432 and sslmode=require.
- For passwordless sign-in, DefaultAzureCredential gets a token for
https://ossrdbms-aad.database.windows.net/.default and you pass it as the password. - An Entra admin on the server must create a database role for each Entra user, group or managed identity.
🎯 Exam tip: "Connect without storing a password" = Microsoft Entra token from DefaultAzureCredential used as the password.
⚠️ Watch out: Building SQL with f-strings or + from user input, which opens the door to SQL injection.
A good AI table in Azure Database for PostgreSQL keeps each fact in the right type: uuid or bigint keys, text for chunks, jsonb for flexible metadata, timestamptz for times and vector(1536) (from the pgvector extension) for embeddings. You first add vector to the azure.extensions server parameter, then run CREATE EXTENSION vector. Then you index for the way you query: B-tree for equality and ranges, GIN for jsonb, and HNSW or IVFFlat for vectors, and you prove it works with EXPLAIN ANALYZE.
- Allow-list vector in azure.extensions, then CREATE EXTENSION vector in each database that needs it.
- Pick types on purpose: uuid or bigint keys, text, jsonb, timestamptz, vector(n) or halfvec(n).
- B-tree for equality, ranges and sorting. GIN for jsonb and full-text. HNSW or IVFFlat for vectors.
🎯 Exam tip: "Extension not allowed" error = add it to azure.extensions first.
⚠️ Watch out: Running CREATE EXTENSION vector before adding vector to azure.extensions.
With pgvector you store each chunk's embedding in a vector(1536) column, embed the user's question with the same model, and run ORDER BY embedding <=> %s LIMIT 5 to get the closest chunks by cosine distance. For retrieval-augmented generation (RAG) you add a metadata filter in the WHERE clause (such as tenant_id or lang), send those chunks to the model as context, and tell it to answer only from them. An HNSW index keeps the search fast, and hnsw.ef_search trades speed for recall.
- Embed documents and questions with the same model and dimensions.
- Nearest chunks: ORDER BY embedding <=> query_vector LIMIT k (smaller distance = closer).
- Put metadata filters (tenant_id, lang, product) in the WHERE clause of the same query.
🎯 Exam tip: "Return the 5 most similar items" = ORDER BY embedding <=> @q LIMIT 5.
⚠️ Watch out: Treating <=> as similarity and sorting DESC, which returns the least similar chunks first.
Vector search on Azure Database for PostgreSQL is hungry for memory: HNSW indexes are fastest when they fit in RAM, so busy vector workloads usually run on General Purpose or Memory Optimized compute, not Burstable. Raise maintenance_work_mem for index builds, tune m and ef_construction (HNSW) or lists (IVFFlat, built after loading data), and size IOPS with Premium SSD v2. For throughput, reuse connections with psycopg_pool in the app and the built-in PgBouncer on port 6432.
- Burstable is for dev and test; vector workloads belong on General Purpose or Memory Optimized.
- HNSW is fastest when the index fits in memory. Memory Optimized gives the most RAM per vCore.
- Load data first, then build the index, with a higher maintenance_work_mem and parallel workers.
🎯 Exam tip: "Speed up CREATE INDEX for HNSW" = raise maintenance_work_mem (and parallel maintenance workers).
⚠️ Watch out: Building an IVFFlat index on an empty table, so its clusters are learned from no data.
Azure Managed Redis is an in-memory store based on Redis Enterprise. With cache-aside, your app checks Redis first; on a miss it reads the database (or calls the model), then saves the result with a TTL using SET key value EX 300. When the source data changes, you invalidate by deleting the key so the next read loads fresh data. In Python you connect with redis-py on port 10000 over TLS, using Microsoft Entra ID through redis-entraid (or an access key if enabled), and an eviction policy decides what goes when memory fills.
- Azure Managed Redis is based on Redis Enterprise; tiers are Memory Optimized, Balanced, Compute Optimized and Flash Optimized.
- Connect on port 10000 with TLS. Entra ID via redis-entraid is preferred; access keys are disabled by default on new caches.
- Cache-aside: GET, on a miss load from the source, then SET with EX seconds.
🎯 Exam tip: "Load into the cache only when requested" = cache-aside.
⚠️ Watch out: Caching without a TTL, so stale data and memory use grow forever.
Azure Managed Redis does vector search through the RediSearch module, which you must select when you create the cache (with Redis 7.4) along with the Enterprise or Non-clustered clustering policy. You store embeddings as FLOAT32 bytes in hashes, create an index with FT.CREATE ... SCHEMA ... VECTOR HNSW (or FLAT) TYPE FLOAT32 DIM 1536 DISTANCE_METRIC COSINE, and query with FT.SEARCH using =>[KNN 3 @embedding $vec] and DIALECT 2. A common use is a semantic cache that returns a saved answer when a new question means the same thing.
- RediSearch must be selected at cache creation (Redis 7.4) and needs Enterprise or Non-clustered clustering, not OSS.
- Store vectors as raw FLOAT32 bytes, for example numpy.astype(np.float32).tobytes().
- FT.CREATE idx ON HASH PREFIX 1 doc: SCHEMA ... embedding VECTOR HNSW 6 TYPE FLOAT32 DIM 1536 DISTANCE_METRIC COSINE
🎯 Exam tip: "Vector index in Azure Managed Redis" = RediSearch module, chosen at creation.
⚠️ Watch out: Creating the cache without RediSearch, or with the OSS clustering policy, then trying to run FT.CREATE.