Redis Vector Sets
Modern AI applications describe text, images and products as embeddings: lists of numbers (vectors) produced by a machine-learning model, where similar things get vectors that point in similar directions. "Find the products most similar to this one" or "find the documents closest to this question" then becomes a nearest-neighbour search over vectors. Redis 8 includes a data type made for this: the vector set, created by Salvatore Sanfilippo, the original author of Redis.
A vector set is like a sorted set whose members have a vector instead of a score. Instead of asking "what is ranked between 10 and 20", you ask "which members are most similar to this vector".
Adding vectors with VADD
VADD key VALUES dim v1 v2 ... element adds an element with its vector. All vectors in a set must have the same dimension. Real embeddings have hundreds or thousands of dimensions; for readability, our toy "movie embeddings" have three: science fiction, romance and family.
VADD movies VALUES 3 0.9 0.1 0.0 inception
VADD movies VALUES 3 0.8 0.2 0.1 interstellar
VADD movies VALUES 3 0.1 0.9 0.1 notebook
VADD movies VALUES 3 0.2 0.8 0.3 lalaland
VADD movies VALUES 3 0.1 0.1 0.9 coco
VCARD movies
VDIM movies
TYPE movies
VADD movies VALUES 2 0.5 0.5 brokenredis> VADD movies VALUES 3 0.9 0.1 0.0 inception
(integer) 1
redis> VADD movies VALUES 3 0.8 0.2 0.1 interstellar
(integer) 1
redis> VADD movies VALUES 3 0.1 0.9 0.1 notebook
(integer) 1
redis> VADD movies VALUES 3 0.2 0.8 0.3 lalaland
(integer) 1
redis> VADD movies VALUES 3 0.1 0.1 0.9 coco
(integer) 1
redis> VCARD movies
(integer) 5
redis> VDIM movies
(integer) 3
redis> TYPE movies
vectorset
redis> VADD movies VALUES 2 0.5 0.5 broken
(error) ERR Vector dimension mismatch - got 2 but set has 3
The last command fails because its vector has the wrong dimension.
Similarity search with VSIM
VSIM key ELE element finds the members most similar to an existing element; VSIM key VALUES dim ... searches with any vector, for example the embedding of a user's question. WITHSCORES adds the similarity, from 1 (same direction) down to 0 (opposite direction), and COUNT limits the number of results:
VADD movies VALUES 3 0.9 0.1 0.0 inception
VADD movies VALUES 3 0.8 0.2 0.1 interstellar
VADD movies VALUES 3 0.1 0.9 0.1 notebook
VADD movies VALUES 3 0.2 0.8 0.3 lalaland
VADD movies VALUES 3 0.1 0.1 0.9 coco
VSIM movies ELE inception WITHSCORES COUNT 3
VSIM movies VALUES 3 0.1 0.8 0.2 COUNT 2 WITHSCORESredis> VADD movies VALUES 3 0.9 0.1 0.0 inception
(integer) 1
redis> VADD movies VALUES 3 0.8 0.2 0.1 interstellar
(integer) 1
redis> VADD movies VALUES 3 0.1 0.9 0.1 notebook
(integer) 1
redis> VADD movies VALUES 3 0.2 0.8 0.3 lalaland
(integer) 1
redis> VADD movies VALUES 3 0.1 0.1 0.9 coco
(integer) 1
redis> VSIM movies ELE inception WITHSCORES COUNT 3
1) "inception"
2) "0.9999521672725677"
3) "interstellar"
4) "0.9919387996196747"
5) "lalaland"
6) "0.6641140580177307"
redis> VSIM movies VALUES 3 0.1 0.8 0.2 COUNT 2 WITHSCORES
1) "notebook"
2) "0.9955261945724487"
3) "lalaland"
4) "0.9947602450847626"
Inception is most similar to itself (a score just below 1, because of the compression explained below), then to Interstellar, the other science-fiction film. A query vector leaning towards romance finds The Notebook and La La Land. The similarity measure is the cosine of the angle between vectors, mapped to the range 0 to 1, so only the direction matters, not the length.
Attributes and filters
Each element can carry a JSON attribute object (SETATTR on VADD, or VSETATTR later). VSIM ... FILTER then restricts the search with an expression on those attributes, such as .year >= 2014 or .genre == "scifi":
VADD movies VALUES 3 0.9 0.1 0.0 inception SETATTR "{\"year\":2010,\"genre\":\"scifi\"}"
VADD movies VALUES 3 0.8 0.2 0.1 interstellar SETATTR "{\"year\":2014,\"genre\":\"scifi\"}"
VADD movies VALUES 3 0.1 0.9 0.1 notebook SETATTR "{\"year\":2004,\"genre\":\"romance\"}"
VADD movies VALUES 3 0.2 0.8 0.3 lalaland SETATTR "{\"year\":2016,\"genre\":\"romance\"}"
VADD movies VALUES 3 0.1 0.1 0.9 coco SETATTR "{\"year\":2017,\"genre\":\"animation\"}"
VSIM movies VALUES 3 0.5 0.5 0.0 FILTER ".year >= 2014" WITHSCORES
VSIM movies VALUES 3 0.5 0.5 0.0 FILTER ".genre == \"romance\"" COUNT 1
VGETATTR movies cocoredis> VADD movies VALUES 3 0.9 0.1 0.0 inception SETATTR "{\"year\":2010,\"genre\":\"scifi\"}"
(integer) 1
redis> VADD movies VALUES 3 0.8 0.2 0.1 interstellar SETATTR "{\"year\":2014,\"genre\":\"scifi\"}"
(integer) 1
redis> VADD movies VALUES 3 0.1 0.9 0.1 notebook SETATTR "{\"year\":2004,\"genre\":\"romance\"}"
(integer) 1
redis> VADD movies VALUES 3 0.2 0.8 0.3 lalaland SETATTR "{\"year\":2016,\"genre\":\"romance\"}"
(integer) 1
redis> VADD movies VALUES 3 0.1 0.1 0.9 coco SETATTR "{\"year\":2017,\"genre\":\"animation\"}"
(integer) 1
redis> VSIM movies VALUES 3 0.5 0.5 0.0 FILTER ".year >= 2014" WITHSCORES
1) "interstellar"
2) "0.9262985289096832"
3) "lalaland"
4) "0.9035459756851196"
5) "coco"
6) "0.5770039260387421"
redis> VSIM movies VALUES 3 0.5 0.5 0.0 FILTER ".genre == \"romance\"" COUNT 1
1) "lalaland"
redis> VGETATTR movies coco
"{\"year\":2017,\"genre\":\"animation\"}"
This is how a recommendation engine says "similar movies, but only recent ones" or how a retrieval system says "similar documents, but only from this customer's account".
Quantization: smaller vectors
By default Redis stores each vector component as an 8-bit integer instead of a 32-bit float (Q8 quantization), which uses about four times less memory at a tiny cost in precision. VEMB returns the stored vector, so you can see the rounding:
VADD movies VALUES 3 0.9 0.1 0.0 inception
VEMB movies inception
VADD exact VALUES 3 0.9 0.1 0.0 inception NOQUANT
VEMB exact inceptionredis> VADD movies VALUES 3 0.9 0.1 0.0 inception
(integer) 1
redis> VEMB movies inception
1) "0.8999999761581421"
2) "0.09921259433031082"
3) "0"
redis> VADD exact VALUES 3 0.9 0.1 0.0 inception NOQUANT
(integer) 1
redis> VEMB exact inception
1) "0.8999999761581421"
2) "0.10000000149011612"
3) "0"
0.1 came back as about 0.0992 with Q8. With NOQUANT the values are stored as 32-bit floats (still not exactly 0.1, since binary floats can't represent it). BIN quantization stores just one bit per component: extremely compact, for very large sets where approximate results are fine.
Removing elements and how the search works
VREM deletes an element, and VISMEMBER checks for one:
VADD movies VALUES 3 0.9 0.1 0.0 inception
VADD movies VALUES 3 0.1 0.1 0.9 coco
VREM movies coco
VISMEMBER movies coco
VCARD moviesredis> VADD movies VALUES 3 0.9 0.1 0.0 inception
(integer) 1
redis> VADD movies VALUES 3 0.1 0.1 0.9 coco
(integer) 1
redis> VREM movies coco
(integer) 1
redis> VISMEMBER movies coco
(integer) 0
redis> VCARD movies
(integer) 1
Comparing a query with every vector would be too slow for millions of elements. Vector sets use HNSW (Hierarchical Navigable Small World graphs): every element is linked to some of its nearest neighbours on several levels, and a search walks the graph from a coarse level to finer ones. It is approximate: on big sets a result can occasionally miss a true nearest neighbour, which you can trade against speed with the EF option of VSIM. On tiny sets like ours the answer is exact.
Where the embeddings come from
Redis stores and searches vectors; an embedding model creates them. A typical retrieval pipeline in Python:
import redis
from openai import OpenAI # or any embedding model, local or hosted
r = redis.Redis()
client = OpenAI()
def embed(text):
return client.embeddings.create(model="text-embedding-3-small", input=text).data[0].embedding
for doc_id, text in documents.items():
vec = embed(text)
r.execute_command("VADD", "docs", "VALUES", len(vec), *vec, doc_id)
question = embed("How do I reset my password?")
ids = r.execute_command("VSIM", "docs", "VALUES", len(question), *question, "COUNT", 5)
# fetch those documents and pass them to an LLM as context (retrieval-augmented generation)Test yourself
What does VSIM movies ELE inception COUNT 3 return?
- The three oldest elements
- The three elements most similar to
inception - Three random elements
- The vector of inception
Why does VEMB return 0.0992 for a component that was added as 0.1?
- A bug
- The default Q8 quantization stores components as 8-bit integers
- Vectors are normalised to length 2
- VEMB adds noise for privacy
Which option restricts a similarity search to elements whose attribute year is at least 2014?
- WHERE year >= 2014
- FILTER ".year >= 2014"
- BYSCORE 2014 +inf
- SETATTR year 2014