Files
Beyhan Oğur 880f412e2c first commit
2026-04-26 21:52:23 +03:00

242 lines
8.0 KiB
Plaintext

---
title: "Redis / Valkey"
description: "Redis and Valkey vector store integration for semantic caching in Bifrost."
icon: "database"
---
## Redis
Redis provides high-performance in-memory vector storage using RediSearch-compatible APIs, ideal for applications requiring sub-millisecond response times and fast semantic search capabilities. Valkey deployments that expose compatible `FT.*` commands are supported through the same configuration.
### Key Features
- **High Performance**: Sub-millisecond cache retrieval with Redis's in-memory storage
- **Cost Effective**: Open-source solution with no licensing costs
- **HNSW Algorithm**: Fast vector similarity search with excellent recall rates
- **Connection Pooling**: Advanced connection management for high-throughput applications
- **TTL Support**: Automatic expiration of cached entries
- **Streaming Support**: Full streaming response caching with proper chunk ordering
- **Flexible Filtering**: Advanced metadata filtering with exact string matching
### Setup & Installation
**Redis Cloud:**
- Sign up at [cloud.redis.io](https://cloud.redis.io)
- Create a new database with RediSearch module enabled
- Get your connection details
**Local Redis with RediSearch:**
```bash
# Using Docker with Redis Stack (includes RediSearch)
docker run -d --name redis-stack -p 6379:6379 redis/redis-stack:latest
```
**Local Valkey Bundle:**
```bash
# Example Valkey bundle with search/vector support
docker run -d --name valkey-bundle -p 6379:6379 valkey/valkey-bundle:9.0.0
```
### Configuration Options
<Tabs group="redis-config">
<Tab title="Go SDK">
```go
// Configure Redis-compatible vector store (Redis or Valkey endpoint)
vectorConfig := &vectorstore.Config{
Enabled: true,
Type: vectorstore.VectorStoreTypeRedis, // Keep type as "redis" for Valkey too
Config: vectorstore.RedisConfig{
Addr: "localhost:6379", // Redis/Valkey server address - REQUIRED
Username: "", // Optional: Redis username
Password: "", // Optional: Redis password
DB: 0, // Optional: Redis database number (default: 0)
// Optional: TLS and cluster settings
UseTLS: false, // Enable TLS for encrypted connections
InsecureSkipVerify: false, // Skip TLS cert verification
ClusterMode: false, // Use Redis Cluster client for cluster endpoints
// Optional: Connection pool settings
PoolSize: 10, // Maximum socket connections
MaxActiveConns: 10, // Maximum active connections
MinIdleConns: 5, // Minimum idle connections
MaxIdleConns: 10, // Maximum idle connections
// Optional: Timeout settings
DialTimeout: 5 * time.Second, // Connection timeout
ReadTimeout: 3 * time.Second, // Read timeout
WriteTimeout: 3 * time.Second, // Write timeout
ContextTimeout: 10 * time.Second, // Operation timeout
},
}
// Create vector store
store, err := vectorstore.NewVectorStore(context.Background(), vectorConfig, logger)
if err != nil {
log.Fatal("Failed to create vector store:", err)
}
```
</Tab>
<Tab title="config.json">
```json
{
"vector_store": {
"enabled": true,
"type": "redis",
"config": {
"addr": "localhost:6379",
"username": "",
"password": "",
"db": 0,
"use_tls": false,
"insecure_skip_verify": false,
"ca_cert_pem": "",
"cluster_mode": false,
"pool_size": 10,
"max_active_conns": 10,
"min_idle_conns": 5,
"max_idle_conns": 10,
"dial_timeout": "5s",
"read_timeout": "3s",
"write_timeout": "3s",
"context_timeout": "10s"
}
}
}
```
**For Redis Cloud or Valkey service endpoints:**
```json
{
"vector_store": {
"enabled": true,
"type": "redis",
"config": {
"addr": "your-redis-host:port",
"username": "your-username",
"password": "your-password",
"db": 0,
"use_tls": true,
"ca_cert_pem": "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----",
"cluster_mode": false,
"context_timeout": "10s"
}
}
}
```
**For managed Redis Cluster endpoints:**
```json
{
"vector_store": {
"enabled": true,
"type": "redis",
"config": {
"addr": "your-cluster-endpoint:6379",
"username": "your-username",
"password": "your-password",
"db": 0,
"use_tls": true,
"ca_cert_pem": "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----",
"cluster_mode": true,
"context_timeout": "10s"
}
}
}
```
</Tab>
</Tabs>
### Redis-Specific Features
**Vector Search Algorithm:**
Redis uses the **HNSW (Hierarchical Navigable Small World)** algorithm for vector similarity search, which provides:
- **Fast Search**: O(log N) search complexity
- **High Accuracy**: Excellent recall rates for similarity search
- **Memory Efficient**: Optimized for in-memory operations
- **Cosine Similarity**: Uses cosine distance metric for semantic similarity
**Connection Pool Management:**
Redis provides extensive connection pool configuration:
```go
config := vectorstore.RedisConfig{
Addr: "localhost:6379",
UseTLS: true, // Enable TLS
ClusterMode: true, // Enable cluster mode
PoolSize: 20, // Max socket connections
MaxActiveConns: 20, // Max active connections
MinIdleConns: 5, // Min idle connections
MaxIdleConns: 10, // Max idle connections
ConnMaxLifetime: 30 * time.Minute, // Connection lifetime
ConnMaxIdleTime: 5 * time.Minute, // Idle connection timeout
DialTimeout: 5 * time.Second, // Connection timeout
ReadTimeout: 3 * time.Second, // Read timeout
WriteTimeout: 3 * time.Second, // Write timeout
ContextTimeout: 10 * time.Second, // Operation timeout
}
```
### Performance Optimization
**Connection Pool Tuning:**
For high-throughput applications, tune the connection pool settings:
```json
{
"vector_store": {
"config": {
"pool_size": 50, // Increase for high concurrency
"max_active_conns": 50, // Match pool_size
"min_idle_conns": 10, // Keep connections warm
"max_idle_conns": 20, // Allow some idle connections
"conn_max_lifetime": "1h", // Refresh connections periodically
"conn_max_idle_time": "10m" // Close idle connections
}
}
}
```
**Memory Optimization:**
- **TTL**: Use appropriate TTL values to prevent memory bloat
- **Namespace Cleanup**: Regularly clean up unused namespaces
**Batch Operations:**
Redis supports efficient batch operations:
```go
// Batch retrieval
results, err := store.GetChunks(ctx, namespace, []string{"id1", "id2", "id3"})
// Batch deletion
deleteResults, err := store.DeleteAll(ctx, namespace, queries)
```
### Production Considerations
<Info>
**TLS and Cluster Mode**: Set `use_tls: true` to enable TLS encryption for the Redis connection, and `insecure_skip_verify: true` if using self-signed certificates. Set `cluster_mode: true` when connecting to a Redis Cluster endpoint. When cluster mode is enabled, the `db` field must be `0` (Redis Cluster does not support database selection).
</Info>
<Info>
**Search Module Required**: Redis/Valkey integration requires a search module/API that supports `FT.*` commands (index creation and vector search). If `FT.INFO` or `FT.SEARCH` is unavailable, semantic caching will not work.
</Info>
<Warning>
**Production Considerations**:
- Use Redis AUTH for production deployments
- Configure appropriate connection timeouts
- Monitor memory usage and set appropriate TTL values
</Warning>
For the VectorStore interface API and usage examples, see [Vector Store Architecture](/architecture/framework/vector-store). For semantic caching setup, see [Semantic Caching](/features/semantic-caching).