Can I use Redis as a Vector Database?

Last updated 22, Apr 2024

Question

Can I use Redis as a Vector Database?

Answer

Redis isn't just for caching data—it can also serve as a powerful Vector Database. If you're new to this concept, here's a brief rundown on how to use Redis as a Vector Database. Note that you'll need Redis Stack, Redis Enterprise, or Redis Cloud in your development setup; the basic Redis OSS version lacks this Vector Database capability. When Redis is used as a Vector Database, it offers support for:

  1. Storing Vectors: Redis can store arrays of floating-point numbers within Hash or JSON documents.
  2. Indexing Vectors: You can index vectors using methods like FLAT or HNSW, which support various distance measures like L2, IP, and COSINE.
  3. Vector Search: Redis can retrieve similar vectors and associated documents, with options for score-based range searches.
  4. Hybrid Queries: Redis supports hybrid queries, allowing you to filter documents using full-text, numeric, tags, and geographical data. You can even partition the vector space based on these factors.

If you're already using Redis, you'll find that it enables modern use cases requiring vector support.

Redis Stack Overview

Redis Stack is a combination of Redis along with additional features and RedisInsight. It provides extended search and query features, making it easier to work with vectors. Installing Redis Stack gets you these vector capabilities out of the box.

Installation Options

You can install Redis Stack in various ways:

  • Docker: An official Redis Stack image is available on Docker Hub. A simple command can set up a fresh Vector Database.
  • Platform-Specific: Redis Stack is installable on Linux, Windows, and MacOS. Refer to the documentation for the right installation method.
  • Redis Cloud: A managed Redis Stack instance is free on Redis Cloud. While limited in size (30MB), it's suitable for basic use cases. For larger databases, Redis Cloud offers scaling options.

The supported client libraries

Utilize standard client libraries like redis-py, Jedis, node-redis or NRedisStack. As an example, creating an index on a vector specified by the field content_embedding in Python is as easy as following this syntax:

index_def = IndexDefinition(prefix=["data:"])
schema = (TagField("url"),
TagField("category"),
TextField("content"),
VectorField("content_embedding", "HNSW", {"TYPE": "FLOAT32", "DIM": 384, "DISTANCE_METRIC": "L2"}))
conn.ft('data_idx').create_index(schema, definition=index_def)

Learn more about Vector Search if working with Python.

Vector Database in production

Transitioning from your development environment to the production stage using Redis Stack is feasible. Nevertheless, if you aim to capitalize on a production-quality, managed, and multi-tenant atmosphere—scaling up to support hundreds of millions of operations per second with automatic scalability built-in—it might be advantageous to migrate your workload to Redis Enterprise or Redis Cloud. These options serve as seamless substitutes for Redis Stack, enabling you to seamlessly direct your application to Redis Enterprise or Redis Cloud databases without requiring any adjustments. Detailed instructions for setting up a production configuration can be located within the documentation.

References

Learn more about using Redis as a Vector Database in production at the following link: https://redis.com/solutions/use-cases/vector-database/