Redis Vector Store
This notebook covers how to get started with the Redis vector store.
Redis is a popular open-source, in-memory data structure store that can be used as a database, cache, message broker, and queue. It now includes vector similarity search capabilities, making it suitable for use as a vector store.
What is Redis?โ
Most developers are familiar with Redis
. At its core, Redis
is a NoSQL Database in the key-value family that can used as a cache, message broker, stream processing and a primary database. Developers choose Redis
because it is fast, has a large ecosystem of client libraries, and has been deployed by major enterprises for years.
On top of these traditional use cases, Redis
provides additional capabilities like the Search and Query capability that allows users to create secondary index structures within Redis
. This allows Redis
to be a Vector Database, at the speed of a cache.
Redis as a Vector Databaseโ
Redis
uses compressed, inverted indexes for fast indexing with a low memory footprint. It also supports a number of advanced features such as:
- Indexing of multiple fields in Redis hashes and
JSON
- Vector similarity search (with
HNSW
(ANN) orFLAT
(KNN)) - Vector Range Search (e.g. find all vectors within a radius of a query vector)
- Incremental indexing without performance loss
- Document ranking (using tf-idf, with optional user-provided weights)
- Field weighting
- Complex boolean queries with
AND
,OR
, andNOT
operators - Prefix matching, fuzzy matching, and exact-phrase queries
- Support for double-metaphone phonetic matching
- Auto-complete suggestions (with fuzzy prefix suggestions)
- Stemming-based query expansion in many languages (using Snowball)
- Support for Chinese-language tokenization and querying (using Friso)
- Numeric filters and ranges
- Geospatial searches using Redis geospatial indexing
- A powerful aggregations engine
- Supports for all
utf-8
encoded text - Retrieve full documents, selected fields, or only the document IDs
- Sorting results (for example, by creation date)
Clientsโ
Since Redis
is much more than just a vector database, there are often use cases that demand the usage of a Redis
client besides just the LangChain
integration. You can use any standard Redis
client library to run Search and Query commands, but it's easiest to use a library that wraps the Search and Query API. Below are a few examples, but you can find more client libraries here.
Project | Language | License | Author | Stars |
---|---|---|---|---|
jedis | Java | MIT | Redis | |
redisvl | Python | MIT | Redis | |
redis-py | Python | MIT | Redis | |
node-redis | Node.js | MIT | Redis | |
nredisstack | .NET | MIT | Redis |
Deployment optionsโ
There are many ways to deploy Redis with RediSearch. The easiest way to get started is to use Docker, but there are are many potential options for deployment such as
- Redis Cloud
- Docker (Redis Stack)
- Cloud marketplaces: AWS Marketplace, Google Marketplace, or Azure Marketplace
- On-premise: Redis Enterprise Software
- Kubernetes: Redis Enterprise Software on Kubernetes
Redis connection Url schemasโ
Valid Redis Url schemas are:
redis://
- Connection to Redis standalone, unencryptedrediss://
- Connection to Redis standalone, with TLS encryptionredis+sentinel://
- Connection to Redis server via Redis Sentinel, unencryptedrediss+sentinel://
- Connection to Redis server via Redis Sentinel, booth connections with TLS encryption
More information about additional connection parameters can be found in the redis-py documentation.
Setupโ
To use the RedisVectorStore, you'll need to install the langchain-redis
partner package, as well as the other packages used throughout this notebook.
%pip install -qU langchain-redis langchain-huggingface sentence-transformers scikit-learn
Note: you may need to restart the kernel to use updated packages.
Credentialsโ
Redis connection credentials are passed as part of the Redis Connection URL. Redis Connection URLs are versatile and can accommodate various Redis server topologies and authentication methods. These URLs follow a specific format that includes the connection protocol, authentication details, host, port, and database information. The basic structure of a Redis Connection URL is:
[protocol]://[auth]@[host]:[port]/[database]
Where:
- protocol can be redis for standard connections, rediss for SSL/TLS connections, or redis+sentinel for Sentinel connections.
- auth includes username and password (if applicable).
- host is the Redis server hostname or IP address.
- port is the Redis server port.
- database is the Redis database number.
Redis Connection URLs support various configurations, including:
- Standalone Redis servers (with or without authentication)
- Redis Sentinel setups
- SSL/TLS encrypted connections
- Different authentication methods (password-only or username-password)
Below are examples of Redis Connection URLs for different configurations:
# connection to redis standalone at localhost, db 0, no password
redis_url = "redis://localhost:6379"
# connection to host "redis" port 7379 with db 2 and password "secret" (old style authentication scheme without username / pre 6.x)
redis_url = "redis://:secret@redis:7379/2"
# connection to host redis on default port with user "joe", pass "secret" using redis version 6+ ACLs
redis_url = "redis://joe:secret@redis/0"
# connection to sentinel at localhost with default group mymaster and db 0, no password
redis_url = "redis+sentinel://localhost:26379"
# connection to sentinel at host redis with default port 26379 and user "joe" with password "secret" with default group mymaster and db 0
redis_url = "redis+sentinel://joe:secret@redis"
# connection to sentinel, no auth with sentinel monitoring group "zone-1" and database 2
redis_url = "redis+sentinel://redis:26379/zone-1/2"
# connection to redis standalone at localhost, db 0, no password but with TLS support
redis_url = "rediss://localhost:6379"
# connection to redis sentinel at localhost and default port, db 0, no password
# but with TLS support for booth Sentinel and Redis server
redis_url = "rediss+sentinel://localhost"