Lesson 4 of 4 Embeddings

Keeping Embeddings Current

This lesson compares the methods for regenerating embeddings when source data changes.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain why the model should not be called in the write path.
  • Compare triggers, Change Tracking, CDC, Azure Functions, Logic Apps and Foundry-based pipelines.
  • Describe a queue-based maintenance pattern.

The maintenance problem

When a description is edited, its old embedding still represents the old text, so search returns outdated matches until it is regenerated. A maintenance method must detect inserts, updates of the embedded columns and deletes, call the model reliably, and do so without slowing the application's writes. Calling the model synchronously in the write path couples the application's availability to the model's.

Methods

The methods differ in latency, reliability and where the logic runs, as the following table summarizes.

MethodHow it worksConsiderations
Table triggerMarks rows stale or queues keys in the same transactionNever call the model in the trigger; keep it tiny
Change TrackingA scheduled job reads changed keys since its last versionLow overhead; latency equals the schedule
CDCA job reads before and after valuesCan skip changes that do not touch embedded columns; more overhead
Azure Functions SQL triggerServerless function receives changed rows (uses Change Tracking)Near real time; code outside the database
Azure Logic AppsPolling workflow calls the model and writes backLow-code; minutes of latency
Microsoft FoundryApplication or agent pipelines built on Foundry generate embeddings with the deployed modelLogic lives in the application; Foundry hosts the models all methods use
Match the pairs

Embedding maintenance methods

Exam skills 3.1.3

Match each requirement with the embedding maintenance method that fits it.

Log in to try this activity Interactive activities are free with an account.

A queue-based pattern

A robust in-database pattern uses a small trigger that records keys in a queue table, and a background job, such as an elastic job, an Azure Function on a timer or a SQL Agent job on SQL Server, that processes the queue in batches and calls AI_GENERATE_EMBEDDINGS. Writes stay fast, model outages only delay embeddings, and a hash comparison skips rows whose embedded text did not change.

CREATE TABLE Support.EmbeddingQueue (ArticleID int PRIMARY KEY, QueuedUtc datetime2 NOT NULL DEFAULT SYSUTCDATETIME());
GO
CREATE OR ALTER TRIGGER Support.trg_Article_QueueEmbedding ON Support.Article
AFTER INSERT, UPDATE
AS
BEGIN
    SET NOCOUNT ON;
    INSERT INTO Support.EmbeddingQueue (ArticleID)
    SELECT i.ArticleID FROM inserted AS i
    WHERE NOT EXISTS (SELECT 1 FROM Support.EmbeddingQueue AS q WHERE q.ArticleID = i.ArticleID);
END;

The following diagram follows a change through the queue to a new embedding.

A queue-based pattern for keeping embeddings current. A small trigger records the keys of inserted or updated rows in a queue table, so writes stay fast. A background job, such as an elastic job, a timer-triggered Azure Function or a SQL Agent job, processes the queue in batches, skips rows whose text hash did not change, and calls AI_GENERATE_EMBEDDINGS. A model outage only delays embeddings.
The trigger stays tiny; the job does the slow work.
Select all that apply

The queue pattern

Exam skills 3.1.3

Identify the benefits of the trigger-plus-queue pattern for embeddings.

Log in to try this activity Interactive activities are free with an account.
Worked example

Choosing a method

Support articles are edited a few hundred times a day. Embeddings must be updated within five minutes, the team prefers to keep all logic in the database, and article saves must not fail when the model endpoint is unavailable. Which method fits best?

Use a lightweight trigger (or Change Tracking) to record changed article keys, and a scheduled job every few minutes that re-embeds queued articles in batches with AI_GENERATE_EMBEDDINGS. The logic stays in the database, saves never wait for the model, and the schedule meets the five-minute target. A trigger that calls the model directly would fail saves during outages, and an Azure Function moves logic outside the database, which the team wants to avoid.

Check yourself

Why should not a trigger call AI_GENERATE_EMBEDDINGS directly?

Show answer

It would slow every write and fail inserts and updates whenever the model endpoint is slow or unavailable.

Exam Tips

Remember for the exam

  • Never call the model synchronously in the write path; queue the work.
  • Schedule-based with low overhead: Change Tracking or a trigger-fed queue table.
  • Serverless near real time: Azure Functions SQL trigger.
  • Skip unchanged text with a hash comparison.