Embeddings from Training to Serving
An embedding is a fixed-length vector that represents an item (a query, a document, a product, a user, an image) so that items that are similar in the way you care about end up close together. Embeddings are everywhere in ML systems: candidate retrieval in recommenders, semantic search, RAG, near-duplicate detection, visual search, clustering, and as dense features fed into ranking models.
Most interview answers stop at "embed it and put it in a vector database." That's the easy part. The hard parts come before and after: training an embedding model that captures your notion of similarity, and operating it over time, when a new model version makes every stored vector incompatible and a billion items need re-encoding without downtime. This post covers the life cycle end to end:
- What embeddings are for, and what "similar" means.
- Where embeddings come from: off the shelf, fine-tuned, or trained from scratch.
- Contrastive training and the loss most systems use.
- Negatives, especially hard negatives, and their traps.
- Evaluating embeddings offline and online.
- Encoding and serving at scale.
- Versioning, backfills and re-indexing.
- Drift and monitoring.
The index side (HNSW, IVF, product quantization, filtered search) is covered in Vector Databases, and chunking text for embeddings in RAG Fundamentals. Here we focus on the model and its operations.
What Embeddings Are For
An embedding model f maps an input to a vector f(x) in, typically, 64 to 4,096 dimensions. Similarity is a simple function of two vectors: cosine similarity (the angle between them) or the dot product. If vectors are normalized to unit length, the two are the same, and that's the usual choice, because it makes scores comparable across items and keeps the index's distance metric simple.
The key fact is that "similar" is defined by training data, not by the model architecture. The same text encoder can be trained so that:
- "running shoes" is close to "sneakers for jogging" (semantic similarity),
- "running shoes" is close to the specific products people clicked after that query (relevance),
- a user is close to items they'll engage with (personalization),
- two photos are close if they show the same product, regardless of background (instance matching).
These are different spaces. A general-purpose sentence embedding puts "cheap flights to Paris" and "expensive flights to Paris" close together, because they're about the same thing. For a travel search engine, that's wrong. Choosing positive pairs is choosing what similarity means.
Two families of use:
- Retrieval: a query embedding is compared with millions to billions of item embeddings using approximate nearest neighbor (ANN) search. This is the classic two-tower setup: a query tower and an item tower, trained together, with item vectors precomputed and indexed.
- Features: an embedding is fed into another model (a GBDT or a neural ranker) as a dense feature: a user's interest vector, an item's content vector. Here the requirement is stability and information content, not ANN-friendliness.